0

I am making a custom camera that takes pictures of documents and sends them over the network. The images are then OCRed. For sending them over the network they must be of small size ( less than 100 Kb). I first scale down the image then convert to gray scale and compress to JPG with 100 percent. Still the image size is more that 100Kb. If I reduce the JPG compression percent the quality is really bad. How to solve this issue. Here is the code:

//Image Scaling

 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);
    }

//Image grayscale. Using 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 compression

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
android_eng
  • 1,370
  • 3
  • 17
  • 40

3 Answers3

0

What have you got if you do Bitmap.CompressFormat.PNG instead? And how about trying to shrink the image (always by a factor of 2 according to the docs)

JPEG is full colour, so converting to grayscale do not help at the end, GIF is 8-bit indexed or grayscale, so even that is worth a try.

0

Keep the quality. Compress the images. Use ShortPixel. Even for 10MB/image.

0

There are three things that can help in reducing image size: Scaling down the image while maintaining the aspect ratio:

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);
        }

Remove alpha channel, use the following config:

options.inPreferredConfig = Bitmap.Config.RGB565;

Compressing the image by using the JPEG compression:

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

100 being no compression and 0 being 100% compression

GrayScaling does not reduce size.

android_eng
  • 1,370
  • 3
  • 17
  • 40