posted on: 2020-03-29 12:42:28
Spitting an image into three for an andoid device, for distinguishing colors.

The Images

I was thinking there are some simple methods to distinguish colors on a computer that would be handing to have on a phone. Readily available.

I have trouble discerning bright LED's. This is an image of the types of things I have trouble discerning.

For example, figuring out the state of my headlamp.

Finding an open seat on the train.

Ill keep a couple examples in an open album. discerning colors album

If we take the train picture, and split it into RGB we get.

In this case the red and green are pretty well distinguished, Although one of the red lights is bright enough that it appears in both channels.

Android Part

So I'm using android studio, and it does absolutely everything you want and more. My first attempt is to make a screen that shows an original image and two filtered versions.

Loading a 'preview' grade image.

    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(i, GET_STATIC);

GET_STATIC is a static int I created. startActivityForResult causes a callback on the current class. Then the call back is handled via this method. I only use 1 call back currently so the switch statement is a bit overkill.

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            case GET_STATIC:
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");

One way to display the image.

    public void setImage(Bitmap bm){
        ImageView original = findViewById( );

The next issue, how to manipulate the pixels. One way I have found, but is not great for certain issues, is to grab the actual buffer and then manipulate the bytes.

    Bitmap getGreenHighlighted(Bitmap bm){
        Bitmap gm = bm.copy(bm.getConfig(), true);
        ByteBuffer bb = ByteBuffer.allocate(gm.getAllocationByteCount());
        bb.position(0); //not necessary since we use the ba.
        int n = gm.getWidth()*gm.getHeight();
        byte[] bytes = bb.array();
        for(int i = 0; i<n; i++){
            int r = 0xff & bytes[4*i];
            int g = 0xff & bytes[4*i + 1];
            int b = 0xff & bytes[4*i + 2];
            int a = 0xff & bytes[4*i + 3];
                r = 0;
                g = g*2;
            bytes[4*i] = (byte)r;
            bytes[4*i + 1] = (byte)g;
            bytes[4*i + 2] = (byte)b;
            bytes[4*i + 3] = (byte)a;

        return gm;

This could be simplified using Bitmap#getPixel/setPixel.