fast Replace particular Color of Bitmap in android

-1

I am creating function which can replace particular color of image with threshold value. but it has one problem it does take time 3 seconds for large bitmap. if possible to fast in 100ms or can convert cpp function it will help me for fast response. below i attached code for replace color of image.

protected Bitmap replaceIntervalColor(Bitmap bitmap, int red, int green,
                                      int blue, int tollerence) {
    int[] pix = null;
    Bitmap bm = null;
    int picw, pich;
    int index, cur_pix, red2, green2, blue2;

    if (bitmap != null) {
        picw = bitmap.getWidth();
        pich = bitmap.getHeight();
        if (pix == null) {
            pix = new int[picw * pich];
        }
        bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);

        double distance;
        {
            for (int _index = 0; _index < pix.length; _index++) {
                index = _index;
                cur_pix = pix[index];
                red2 = (int) ((cur_pix & 0x00FF0000) >>> 16); // Color.red(cur_pix);
                green2 = (int) ((cur_pix & 0x0000FF00) >>> 8); // Color.green(cur_pix);
                blue2 = (int) (cur_pix & 0x000000FF); // Color.blue(cur_pix);

                distance = Double
                        .longBitsToDouble(((Double
                                .doubleToRawLongBits((red2 - red)
                                        * (red2 - red) + (green2 - green)
                                        * (green2 - green) + (blue2 - blue)
                                        * (blue2 - blue)) >> 32) + 1072632448) << 31);

                if (distance < 190 - tollerence) {
                    pix[index] = Color.RED;
                } else {
                    pix[index] = Color.TRANSPARENT;
                }
            }
        }


        if (bm == null) {
            bm = Bitmap.createBitmap(picw, pich, Bitmap.Config.ARGB_8888);
        }
        bm.setPixels(pix, 0, picw, 0, 0, picw, pich);
        return bm;
    }
    return null;
}
android
image-processing
android-canvas
asked on Stack Overflow Aug 20, 2018 by Brijesh Lukhi

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0