[英]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.