简体   繁体   中英

Read accurate colors of all pixel blocks of an image in android

I've an image and its pixels are in grid blocks. each pixel occupy 20x20px block of a grid . here is the image

在此处输入图片说明

I want to read color of each block of that grid. Here is the code which i tried.

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.abc);
    for(int y=0; y<bmp.getHeight(); y=y+20){
      for(int x=0; x<bmp.getWidth(); x=x+20){  
        pixelColor = bmp.getPixel(x,y);
    }
      }

The problem is now that, colors which are being read are of very slight difference and in result it is picking too many colors. For example in black color case it picks almost 10 black colors which slightly varies from each other. Please help me to pick all unique colors. any help would be much appreciated. Thank you

This is an issue with device density. You should put different size image for different density devices. Since your image read more than the actual width & height. That cause the problem to overload your loop to more than the actual pixels.

Have a look https://developer.android.com/guide/practices/screens_support.html#qualifiers to see different density devices to put various drawable. This will need if you need to display it in UI.

Otherwise if you don't want image to display it in UI. Put image in drawable-nodpi folder & get height,width it will return correct results.

See this Question & am referred from this Solution

UPDATED:

    ImageView imgTop = (ImageView) findViewById(R.id.imgTop);
    ImageView imgBtm = (ImageView) findViewById(R.id.imgBtm);

    Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.pixel);

    Bitmap output = Bitmap.createBitmap(bmp1.getWidth(),bmp1.getHeight(), Bitmap.Config.ARGB_8888);

    int count = 0;
    int[] pixelColor = new int[bmp1.getHeight() * bmp1.getHeight()];
    for(int y=0; y<bmp1.getHeight(); y++){
        for(int x=0; x<bmp1.getWidth(); x++){
           pixelColor[count] = bmp1.getPixel(x,y);

            if(Color.red(pixelColor[count]) <= 30 && Color.green(pixelColor[count]) <= 30 && Color.blue(pixelColor[count]) <= 30)
            {
                pixelColor[count] = Color.BLACK;
            }
            else
            {
               //pixelColor[count] contain other colours..
            }
            output.setPixel(x,y,pixelColor[count]);
            count++;
        }
    }

    imgTop.setImageBitmap(bmp1);
    imgBtm.setImageBitmap(output);

OUTPUT

在此处输入图片说明

I've finally figured out myself by using Palette class in android

I've used its nested class Palette.Swatch to get all color swatches in the image. here is how i did this

ArrayList<Integer> color_vib = new ArrayList<Integer>();
Bitmap bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.abc );
Palette.from( bitmap ).generate( new Palette.PaletteAsyncListener() {
  @Override
     public void onGenerated( Palette palette ) {
            //work with the palette here
             for( Palette.Swatch swatch : palette.getSwatches() ) {
                    color_vib.add( swatch.getRgb() );
                }
        }
    });

Now I've all unique colors of any blocky pixelated image :)

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