如何在Phonegap中将相机uri转换为blob对象?

时间:2015-02-11 07:38:04

标签: android cordova jquery-mobile phonegap-plugins phonegap-build

在我的应用程序中,相机图像保存并从SQLite中检索。我有来自phonegap api的相机uri,但我想将相机uri转换为blob对象和base64字符串。我在网上找到了一些解决方案但我无法解决我的问题。 This is convert Base 64 to Blob object.我已尝试过该链接。我无法转换为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对象。

1 个答案:

答案 0 :(得分:0)

我尝试了canvas to blob apibase 64 string to blob code。但对我不好。所以我做了一些伎俩。我不会在javascript中将相机uri转换为blob。该过程在android native中生成。 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;
    }
}