简体   繁体   English

在Android中读取图像的所有像素块的准确颜色

[英]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 . 每个像素占据一个20x20px的网格块。 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. 例如,在黑色情况下,它将选择几乎10种彼此略有不同的黑色。 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. 看看https://developer.android.com/guide/practices/screens_support.html#qualifiers可以看到不同密度的设备来放置各种可绘制对象。 This will need if you need to display it in UI. 如果您需要在UI中显示它,则需要这样做。

Otherwise if you don't want image to display it in UI. 否则,如果您不希望图像在UI中显示。 Put image in drawable-nodpi folder & get height,width it will return correct results. 将图像放在drawable-nodpi文件夹中并获取高度,宽度,它将返回正确的结果。

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 我终于在android中使用Palette类弄清楚了自己

I've used its nested class Palette.Swatch to get all color swatches in the image. 我使用了其嵌套类Palette.Swatch来获取图像中的所有色样。 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 :) 现在,我具有任何块状像素化图像的所有唯一颜色:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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