1

i want to replace colors in image and it's assigned to imageview i have search lots of time in google but still to didn't find any useful resources.i have seen in java rgbimagefilter but, it doesn't used in android so my excepted output below screenshots:

original image

enter image description here

after replace green colors to grey like below image:

enter image description here

i know basic idea like read image each pixel compare rgb value for it's matches replace with new color but i don't know how to do that in android programmatically.

g00dy
  • 6,752
  • 2
  • 30
  • 43
Stack Overflow User
  • 4,052
  • 6
  • 29
  • 47

2 Answers2

2

Here are some suggestions (try searching for Image processing the next time ;-)):

Aviary SDK -> And the code for it.

Here you can find a nice tutorial for all kinds of image processing.

Here you can find some libraries :

And finally this project here.

Have a nice reading :-)

g00dy
  • 6,752
  • 2
  • 30
  • 43
0

If you do not want to use any third party libraries, you can check the below code to get you started:

package pete.android.study;

import android.graphics.Bitmap;

public class ImageProcessor {
    Bitmap mImage;
    boolean mIsError = false;

public ImageProcessor(final Bitmap image) {
    mImage = image.copy(image.getConfig(), image.isMutable());
    if(mImage == null) {
        mIsError = true;
    }
}

public boolean isError() {
    return mIsError;
}

public void setImage(final Bitmap image) {
    mImage = image.copy(image.getConfig(), image.isMutable());
    if(mImage == null) {
        mIsError = true;
    } else {
        mIsError = false;
    }
}

public Bitmap getImage() {
    if(mImage == null){
        return null;
    }
    return mImage.copy(mImage.getConfig(), mImage.isMutable());
}

public void free() {
    if(mImage != null && !mImage.isRecycled()) {
        mImage.recycle();
        mImage = null;
    }
}

public Bitmap replaceColor(int fromColor, int targetColor) {
    if(mImage == null) {
        return null;
    }

    int width = mImage.getWidth();
    int height = mImage.getHeight();
    int[] pixels = new int[width * height];
    mImage.getPixels(pixels, 0, width, 0, 0, width, height);

    for(int x = 0; x < pixels.length; ++x) {
        pixels[x] = (pixels[x] == fromColor) ? targetColor : pixels[x];
    }

    Bitmap newImage = Bitmap.createBitmap(width, height, mImage.getConfig());
    newImage.setPixels(pixels, 0, width, 0, 0, width, height);

    return newImage;
    }
}

This code is not mine and was found on this site from another SO user's answer.

Community
  • 1
  • 1
AggelosK
  • 4,313
  • 2
  • 32
  • 37