我正在製作一個自定義相機,它可以拍攝文檔並通過網絡發送。然後圖像被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);