How to change the bitdepth of jpg image before saving to internal storage in android?

467 views Asked by At

The image below is taken from Officelens as they convert the bit-depth of the image the make a clear Black&white image which is on the top and original image on bottom:

enter image description here

But when I try to convert I am getting the output given below:

enter image description here

the above result is done using tif convertion library my tried code is given below;

 private void takePicture() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File folderPath = new File(Environment.getExternalStorageDirectory() + "/OCR");

    if (!folderPath.exists()) {
        if (folderPath.mkdir()) ; //directory is created;
    } else {
        File photo = new File(folderPath, "OCRPIC" + UUID.randomUUID().toString().substring(0, 5) + ".jpg");
        imageUri = FileProvider.getUriForFile(MainActivity.this,
                BuildConfig.APPLICATION_ID + ".provider", photo);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, PHOTO_REQUEST);
        }
}

As I save image from camera to internal storage and on while retrieve I will convert for color compression, code given below,

 TiffConverter.ConverterOptions options = new TiffConverter.ConverterOptions();
    options.throwExceptions = false;
    options.availableMemory = 1 * 1024 * 1024;
    options.compressionScheme = CompressionScheme.LZW;
    options.readTiffDirectory = 1;
    TiffConverter.convertToTiff(path, Environment.getExternalStorageDirectory() + "/OCR"+"/"+path.substring(path.lastIndexOf("/")+1).substring(0,11)+".jpg", options,null);

I want to achieve the ouput that produced by Office Lens.

1

There are 1 answers

0
LadyEinstien On
Try below code to decrease color depth 

public static Bitmap decreaseColorDepth(Bitmap src, int bitOffset) {
    // get image size
    int width = src.getWidth();
    int height = src.getHeight();
    // create output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    // color information
    int A, R, G, B;
    int pixel;

    // scan through all pixels
    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            // get pixel color
            pixel = src.getPixel(x, y);
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);

            // round-off color offset
            R = ((R + (bitOffset / 2)) - ((R + (bitOffset / 2)) % bitOffset) - 1);
            if(R < 0) { R = 0; }
            G = ((G + (bitOffset / 2)) - ((G + (bitOffset / 2)) % bitOffset) - 1);
            if(G < 0) { G = 0; }
            B = ((B + (bitOffset / 2)) - ((B + (bitOffset / 2)) % bitOffset) - 1);
            if(B < 0) { B = 0; }

            // set pixel color to output bitmap
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }

    // return final image
    return bmOut;
}