2013-01-14 51 views
1

我想將從攝像頭拍攝的照片作爲base64字符串發送到服務器。我的問題是圖片在手機中被破壞了。phonegap picture base64 android

我有一些console.logs打印camera.getPicture的成功函數內的base64字符串,每當我打印字符串和解碼圖像時,它只顯示頂部,就好像它不完整。

這裏是我的代碼:

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.htmlenter image description here

我使用的PhoneGap 2.2.0

回答

0

你可以試着加大了圖像質量。我記得如果質量設置的很低,閱讀一些Android手機有問題。我知道這是一個遠射,但值得嘗試:)

+0

已經做到了這一點,沒有運氣:(感謝您的幫助 –

0

我相信console.log對它可以打印的字符數有限制。當您將數據作爲一個圖像標記像源會發生什麼:

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

此外,您還可以嘗試將數據寫入到一個文件。

+0

我已經將數據設置爲背景並且工作完美,如果我將圖像尺寸取消爲100x100,它看起來也是完整的,但是當增加尺寸時位,圖像只是看起來損壞了。 –

+0

增加尺寸是什麼意思? –

0

我所面臨的機器人同樣的問題,確切的問題,確保對方什麼,當我編碼圖像到其相應的Base64 &如果圖像尺寸更(2MB或更多... &還取決於圖像質量&相機的質量,可能來自200萬像素或500萬像素或可能是800萬像素的攝像頭),那麼它會遇到問題,將完整的圖像轉換爲Base64 ...你必須減少關注圖像的大小!我給我的工作Android code,通過它我已經it_
實現獲取Base64編碼的圖像串

 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); 

    } 

我希望這將有助於其他面臨同樣問題的朋友......謝謝!