我正在制作一个自定义相机,它可以拍摄文档并通过网络发送。然后图像被OCR。要通过网络发送它们,它们的尺寸必须很小(小于100 Kb)。我首先缩小图像,然后转换为灰度并以100%压缩为JPG。图像尺寸仍然大于100Kb。如果我降低JPG压缩率,质量真的很差。如何解决这个问题。下面是代码:图像压缩,同时保持质量
//图像缩放
public static Bitmap decodeSampledBitmapFromResource(byte[] data,
int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
return BitmapFactory.decodeByteArray(data, 0, data.length, options);
}
//图像灰度。使用的renderScript
uchar grayscale = pixelIn.r * 0.299 + pixelIn.g * 0.587 + pixelIn.b * 0.114;
uchar4 pixelOut;
pixelOut.a = pixelIn.a;
pixelOut.r = grayscale;
pixelOut.g = grayscale;
pixelOut.b = grayscale;
return pixelOut;
// JPG压缩
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);