我所面临的机器人同样的问题,确切的问题,确保对方什么,当我编码图像到其相应的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);
}
我希望这将有助于其他面临同样问题的朋友......谢谢!
已经做到了这一点,没有运气:(感谢您的帮助 –