2013-01-14 45 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); 

    } 

我希望这将有助于其他面临同样问题的朋友......谢谢!