1

カメラから撮った写真をbase64文字列としてサーバーに送信したい。私の問題は、電話で画像が何らかの形で破損することです。

camera.getPicture の成功関数内に base64 文字列を出力するための console.logs がいくつかあります。文字列を出力して画像をデコードすると、不完全であるかのように上部のみが表示されます。

これが私のコードです:

photo.capturePhoto = function(image_button_id) {
        navigator.camera.getPicture(function(image) {
            photo.onPhotoDataSuccess(image)
        }, onFail, {
            quality : 30,
            destinationType: destinationType.DATA_URL,
            correctOrientation : true
        });
    }

そして成功関数:

photo.onPhotoDataSuccess = function(image) {
        console.log(image); //What this prints is an incomplete image when decoded
    }

このコードの何が問題になっていますか?

これは、http: //www.freeformatter.com/base64-encoder.htmlでデコードした場合の画像の例です。 ここに画像の説明を入力

phonegap 2.2.0 を使用しています

4

3 に答える 3

0

画質を上げてみてください。品質が低く設定されている場合、いくつかのAndroid携帯電話を読むと問題が発生することを覚えています。私はそれがロングショットであることを知っていますが、試してみる価値があります:)

于 2013-01-14T16:43:09.773 に答える
0

私はアンドロイドで同じ問題に直面しました、正確な問題は何ですか_画像を対応するものにエンコードするときBase64&画像サイズがもっと大きい場合(2mb以上...&画質とカメラ品質にも依存します、2MPから取得される可能性がありますまたは5MPまたは8MPカメラの場合もあります)その後、完全な画像をBase64に変換する際に問題が発生します...問題の画像のサイズを小さくする必要があります!私はそれを達成した私の仕事Android codeを与えました_GetBase64image
String

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Bitmap mBitmap= new decodeFile("<PATH_OF_IMAGE_HERE>");
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        int i=b.length;
        String base64ImageString=android.util.Base64.encodeToString(b, 0, i, android.util.Base64.NO_WRAP);

適切なビットマップに変換する

       /**
        *My Method that reduce the bitmap size.
        */
        private Bitmap decodeFile(String  fPath){

        //Decode image size
        BitmapFactory.Options opts = new BitmapFactory.Options();
        //opts.inJustDecodeBounds = true;
        opts.inDither=false;                     //Disable Dithering mode
        opts.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
        opts.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
        opts.inTempStorage=new byte[1024]; 

        BitmapFactory.decodeFile(fPath, opts);

        //The new size we want to scale to
        final int REQUIRED_SIZE=70;//or vary accoding to your need...

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(opts.outWidth/scale/2>=REQUIRED_SIZE && opts.outHeight/scale/2>=REQUIRED_SIZE)
            scale*=2;

        //Decode with inSampleSize
         opts.inSampleSize=scale;

        return BitmapFactory.decodeFile(fPath, opts);

   } 

これが同じ問題に直面している他の仲間に役立つことを願っています...ありがとう!

于 2013-01-19T07:34:15.253 に答える
0

console.log には、出力できる文字数に制限があると思います。次のようなイメージ タグのソースとしてデータを設定するとどうなりますか。

function onSuccess(imageData) {
    var image = document.getElementById('myImage');
    image.src = "data:image/jpeg;base64," + imageData;
}

また、データをファイルに書き出すこともできます。

于 2013-01-14T17:17:59.327 に答える