java - BitmapToBase64 Android Studio
问题描述
我将一个从预览图片转换到服务器的 base64 字符串发布。为了使这个字符串更短,我尝试减小位图的大小。但在那之后,我发现字符串的长度并没有减少。以下是代码。
@Override
protected Object doInBackground(Object[] objects) {
Camera.Parameters parameters = mCamera.getParameters();
int imageFormat = parameters.getPreviewFormat();
int w = parameters.getPreviewSize().width;
int h = parameters.getPreviewSize().height;
// TODO: Arguments?
Rect rect = new Rect(0, 0, w, h);
YuvImage yuvImage = new YuvImage(mData, imageFormat, w, h, null);
Log.e("Image", "width:" + w + " height:" + h);
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
yuvImage.compressToJpeg(rect, 100, outputStream);
Bitmap raw_bitmap = BitmapFactory.decodeByteArray(outputStream.toByteArray(), 0, outputStream.size());
Log.i(TAG, "onPreviewFrame: origin_raw_bitmap:" + ToBase64.bitmapToBase64(raw_bitmap));
// raw_bitmap = BitmapProcess.scaleBitmap(raw_bitmap, 50, 50);
raw_bitmap = BitmapProcess.compressImage(raw_bitmap);
Log.i(TAG, "onPreviewFrame: new_raw_bitmap:" + ToBase64.bitmapToBase64(raw_bitmap));
String base64_global = ToBase64.bitmapToBase64(raw_bitmap);
login(myurl, base64_global, new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG + "Failure", "onFailure:err");
// Toast.makeText(MainActivity.this, "Connection refused!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
assert response.body() != null;
String result = response.body().string();
Log.d(TAG + "Response", "out result===" + result);
Log.d(TAG + "Response", response.code() + "");
if (JudgeAns(result)) {
Log.e("Response", "Blind");
Looper.prepare();
Toast toast = Toast.makeText(SurfaceViewCallback.getInstacne().context, "Get the Blind way.", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
Looper.loop();
} else {
Log.e("Response", "Others");
Looper.prepare();
Toast toast = Toast.makeText(SurfaceViewCallback.getInstacne().context, "Can not find the Blind way.", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
Looper.prepare();
}
}
});
} catch (Exception e) {
Log.e(TAG, "onPreviewFrame: Fail" + e.getLocalizedMessage());
}
return null;
}
public static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
while (baos.toByteArray().length / 1024 > 100) {
baos.reset();
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
options -= 10;
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
return BitmapFactory.decodeStream(isBm, null, null);
}
这些是 Android Studio 中 Logcat 中的信息。
位图缩小前的字符串: 在此处输入图像描述
位图缩小后的字符串: 在此处输入图像描述
解决方案
推荐阅读
- android - 是否可以使用 Andriod.bp 构建 android 应用程序
- c - 使用strtok解析C中的字符串并存储在数组中
- java - Firebase Cloud Firestore 遵循参考
- sql-server - 是否可以创建一个使用现有表的列的“TYPE ... TABLE”?
- r - 使用分组变量按顺序绘制因子
- android - 如何在 Anko 中为 SQLiteDatabase 的 INTEGER 字段设置默认值
- javascript - 如何永久禁用引导按钮?
- ballerina - 芭蕾舞女演员中的`any`和`var`有什么区别
- javascript - 使用现有信息将时间戳添加到单元格而不删除
- angular - 无法访问属性对象 Typescript