如何将相机 uri 转换为 Phonegap 中的 blob 对象?
How to convert camera uri to blob object in Phonegap?
在我的应用程序中,相机图像从 SQLite 中保存和检索。我有来自 phonegap api 的相机 uri,但我想将相机 uri 转换为 blob 对象和 base64 字符串。我从网上找到了一些解决方案,但我无法解决我的问题。
This is convert Base 64 to Blob object. 我已经试过了 link。我无法转换为 blob 对象。
使用 phonegap-1.4.1.js
Index.js
function onPhotoFileSuccess(imageData){
console.log(JSON.stringify(imageData));
var smallImage = document.getElementById('imageview'+imageviewcount);
smallImage.style.display = 'block';
smallImage.src = imageData;
// "imageData" parameter is image uri
// I don't know how to convert image uri to blob object
// save to SQLite
var db = window.sqlitePlugin.openDatabase(Mydb , ver, "", size);
db.transaction(function(tx) {tx.executeSql("Insert Into "+ DOPhotoTable +"( RefTranNo,Photo) values(?,?);",["007", blobobject]);});
}
function takeCameraImage(){
navigator.camera.getPicture(onPhotoFileSuccess, onFail, { quality: 30, destinationType: Camera.DestinationType.FILE_URI });
}
在 onPhotoFileSucces 函数中,我想将图像 uri 转换为 blob 对象。
我试过了canvas to blob api and base 64 string to blob code。但对我来说不好。所以我做了一些把戏。我不会在 javascript 中将相机 uri 转换为 blob。该过程使 android 原生。 Android 本机可以轻松将相机 uri 转换为 blob。我从 javascript.
调用了 android 本机代码
Index.js(Phonegap)
function onPhotoFileSuccess(imageData){
var smallImage = document.getElementById('imageview'+imageviewcount);
smallImage.style.display = 'block';
smallImage.src = imageData;
window.PhonegapActivity.insertPhoto(reftranNo,imageData);
}
function btn_Camera_click(){
navigator.camera.getPicture(onPhotoFileSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI });
}
PhonegapActivity(Android 原生)
@Override
public boolean onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setIntegerProperty("loadUrlTimeoutValue", 60000);
//fix browser cache settings
if (super.appView != null) {
super.appView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
super.loadUrl("file:///android_asset/www/index.html");
// *** for send data from javascript
super.appView.addJavascriptInterface(this, "PhonegapActivity");
}
public String insertPhoto(String reftranNo, String photo){
try {
Uri photoUri = Uri.parse(photo);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),photoUri);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
byte[] blob = stream.toByteArray();
dbProvider _db = new dbProvider(this);
_db = _db.openToWrite();
_db.DOPhotoInsert(reftranNo, blob);
_db.close();
return true;
} catch (Exception e) {
// TODO: handle exception
Log.e("Insert DO Photo", e.toString());
return false;
}
}
在我的应用程序中,相机图像从 SQLite 中保存和检索。我有来自 phonegap api 的相机 uri,但我想将相机 uri 转换为 blob 对象和 base64 字符串。我从网上找到了一些解决方案,但我无法解决我的问题。
This is convert Base 64 to Blob object. 我已经试过了 link。我无法转换为 blob 对象。
使用 phonegap-1.4.1.js
Index.js
function onPhotoFileSuccess(imageData){
console.log(JSON.stringify(imageData));
var smallImage = document.getElementById('imageview'+imageviewcount);
smallImage.style.display = 'block';
smallImage.src = imageData;
// "imageData" parameter is image uri
// I don't know how to convert image uri to blob object
// save to SQLite
var db = window.sqlitePlugin.openDatabase(Mydb , ver, "", size);
db.transaction(function(tx) {tx.executeSql("Insert Into "+ DOPhotoTable +"( RefTranNo,Photo) values(?,?);",["007", blobobject]);});
}
function takeCameraImage(){
navigator.camera.getPicture(onPhotoFileSuccess, onFail, { quality: 30, destinationType: Camera.DestinationType.FILE_URI });
}
在 onPhotoFileSucces 函数中,我想将图像 uri 转换为 blob 对象。
我试过了canvas to blob api and base 64 string to blob code。但对我来说不好。所以我做了一些把戏。我不会在 javascript 中将相机 uri 转换为 blob。该过程使 android 原生。 Android 本机可以轻松将相机 uri 转换为 blob。我从 javascript.
Index.js(Phonegap)
function onPhotoFileSuccess(imageData){
var smallImage = document.getElementById('imageview'+imageviewcount);
smallImage.style.display = 'block';
smallImage.src = imageData;
window.PhonegapActivity.insertPhoto(reftranNo,imageData);
}
function btn_Camera_click(){
navigator.camera.getPicture(onPhotoFileSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI });
}
PhonegapActivity(Android 原生)
@Override
public boolean onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setIntegerProperty("loadUrlTimeoutValue", 60000);
//fix browser cache settings
if (super.appView != null) {
super.appView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
super.loadUrl("file:///android_asset/www/index.html");
// *** for send data from javascript
super.appView.addJavascriptInterface(this, "PhonegapActivity");
}
public String insertPhoto(String reftranNo, String photo){
try {
Uri photoUri = Uri.parse(photo);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),photoUri);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
byte[] blob = stream.toByteArray();
dbProvider _db = new dbProvider(this);
_db = _db.openToWrite();
_db.DOPhotoInsert(reftranNo, blob);
_db.close();
return true;
} catch (Exception e) {
// TODO: handle exception
Log.e("Insert DO Photo", e.toString());
return false;
}
}