我在 Android 中使用位图。当我转换为灰度时,我应该期望每个像素的值在 0 到 255 之间。但是对于像素,我将其中一些设为 0,而将其他一些设为非常大的值。我不知道为什么。为什么我没有得到 [0,255] 范围内的值?我如何得到它?
我的代码是:
//Converting to grayscale
int width, height;
height = mybitmap.getHeight();
width = mybitmap.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(mybitmap, 0, 0, paint);
//Done bmpgrayscale is the grayscale image formed
Toast toast23=Toast.makeText(getApplicationContext(),"Grayscale made", Toast.LENGTH_LONG);
toast23.show();
for (int i=0;i<210;i++){
for (int j=0;j<170;j++){
Log.d("gray value is=", String.valueOf(bmpGrayscale.getPixel(i, j)));
}
}
输出的一部分是:
12-07 23:14:28.022: D/gray value is=(958): 0
12-07 23:14:28.022: D/gray value is=(958): 0
12-07 23:14:28.022: D/gray value is=(958): 0
12-07 23:14:28.022: D/gray value is=(958): 0
12-07 23:14:28.022: D/gray value is=(958): 0
12-07 23:14:28.022: D/gray value is=(958): 0
12-07 23:14:28.022: D/gray value is=(958): 0
12-07 23:14:28.022: D/gray value is=(958): 0
12-07 23:14:28.022: D/gray value is=(958): 0
12-07 23:14:28.022: D/gray value is=(958): 83886079
12-07 23:14:28.022: D/gray value is=(958): 317846001
12-07 23:14:28.022: D/gray value is=(958): 602992880
12-07 23:14:28.022: D/gray value is=(958): 669772779
12-07 23:14:28.022: D/gray value is=(958): 904719596
12-07 23:14:28.022: D/gray value is=(958): 971894253
12-07 23:14:28.022: D/gray value is=(958): 1223552493
12-07 23:14:28.022: D/gray value is=(958): 1223552493
12-07 23:14:28.022: D/gray value is=(958): 1223552493
12-07 23:14:28.022: D/gray value is=(958): 1223552493
12-07 23:14:28.052: D/gray value is=(958): 1223552493
12-07 23:14:28.052: D/gray value is=(958): 1223552493
12-07 23:14:28.052: D/gray value is=(958): 1223552493
12-07 23:14:28.052: D/gray value is=(958): 1223552493
12-07 23:14:28.052: D/gray value is=(958): 1223552493
12-07 23:14:28.052: D/gray value is=(958): 1223552493
12-07 23:14:28.052: D/gray value is=(958): 1223552493
12-07 23:14:28.052: D/gray value is=(958): 1223552493
12-07 23:14:28.052: D/gray value is=(958): 1223552493
12-07 23:14:28.052: D/gray value is=(958): 1223552493
请帮忙。我正在使用它将我的视图转换为位图,然后将位图转换为灰度。
编辑
//Converting to grayscale
int width, height;
height = mybitmap.getHeight();
width = mybitmap.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(mybitmap, 0, 0, paint);
//Done bmpgrayscale is the grayscale image formed
Toast toast23=Toast.makeText(getApplicationContext(),"Grayscale made", Toast.LENGTH_LONG);
toast23.show();
for (int i=0;i<210;i++){
for (int j=0;j<170;j++){
int a=bmpGrayscale.getPixel(i, j);
ByteBuffer b = ByteBuffer.allocate(4);
//b.order(ByteOrder.BIG_ENDIAN); // optional, the initial order of a byte buffer is always BIG_ENDIAN.
b.putInt(a );
byte[] result = b.array();
Log.d("gray value is=", String.valueOf((int)result[0]));
}
}