2016-03-28 119 views
-1

我正在制作一个自定义相机,它可以拍摄文档并通过网络发送。然后图像被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); 

回答

2

看看到this文章,我相信你会得到你想要的东西。

0

你有什么如果你做Bitmap.CompressFormat.PNG呢?如何缩小图像(总是按照文档的2倍)

JPEG是全彩色的,所以转换为灰度并不能帮到底,GIF是8位索引或灰度,所以即使这是值得一试。

0

保持质量。压缩图像。使用ShortPixel。即使是10MB /图像。

0

有三两件事,可以在降低图像的大小帮助: 比例缩小图像,同时保持纵横比:

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.RGB565; 
      return BitmapFactory.decodeByteArray(data, 0, data.length, options); 
     } 

删除alpha通道,使用以下配置:

options.inPreferredConfig = Bitmap.Config.RGB565; 

使用JPEG压缩压缩图像:

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream); 

100没有压缩和 0正在100%压缩

GrayScaling不会减小大小。