简体   繁体   中英

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

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:

在此处输入图片说明

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

在此处输入图片说明

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM