[英]Read accurate colors of all pixel blocks of an image in android
我有一张图像,它的像素在网格块中。 每个像素占据一个20x20px的网格块。 这是图像
我想读取该网格的每个块的颜色。 这是我尝试过的代码。
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);
}
}
现在的问题是,读取的颜色差异很小,结果是选择了太多颜色。 例如,在黑色情况下,它将选择几乎10种彼此略有不同的黑色。 请帮助我挑选所有独特的颜色。 任何帮助将非常感激。 谢谢
这是设备密度的问题。 您应该为不同密度的设备放置不同尺寸的图像。 由于您的图像读取的内容超出了实际的宽度和高度。 这会导致问题使循环过载到超过实际像素。
看看https://developer.android.com/guide/practices/screens_support.html#qualifiers可以看到不同密度的设备来放置各种可绘制对象。 如果您需要在UI中显示它,则需要这样做。
否则,如果您不希望图像在UI中显示。 将图像放在drawable-nodpi文件夹中并获取高度,宽度,它将返回正确的结果。
更新:
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);
输出值
我终于在android中使用Palette类弄清楚了自己
我使用了其嵌套类Palette.Swatch来获取图像中的所有色样。 这是我这样做的方式
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() );
}
}
});
现在,我具有任何块状像素化图像的所有唯一颜色:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.