我所面臨的機器人同樣的問題,確切的問題,確保對方什麼,當我編碼圖像到其相應的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);
}
我希望這將有助於其他面臨同樣問題的朋友......謝謝!
已經做到了這一點,沒有運氣:(感謝您的幫助 –