如何将普通位图转换为单色位图安卓



嗨,我想在Android中将普通位图转换为单色位图,该怎么做。我试图在网上查找,但只能找到

Bitmap bmpMonochrome = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpMonochrome);
ColorMatrix ma = new ColorMatrix();
ma.setSaturation(0);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(ma));
canvas.drawBitmap(bmpSrc, 0, 0, paint);

我想传递一个位图,作为回报,下注单色位图的 Byte 数组。

public static byte[] toBytes(Bitmap bitmap) {
}

请提供任何指针或链接

R

您可以使用 ColorMatrix calss 来实现此目的:

ImageView imgview = (ImageView)findViewById(R.id.imageView_grayscale);
imgview.setImageBitmap(bitmap);
// Apply grayscale filter
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imgview.setColorFilter(filter);

ClorMatrix方法的另一个例子在本要点中介绍。

根据您的代码片段:

public static byte[] toMonochromeByteArray(Bitmap bitmap) {
final Bitmap result = Bitmap.createBitmap(
bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
final Canvas canvas = new Canvas(result);
final ColorMatrix saturation = new ColorMatrix();
saturation.setSaturation(0f);
final Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(saturation));
canvas.drawBitmap(bitmap, 0, 0, paint);
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
final Bitmap.CompressFormat compressFormat =
bitmap.hasAlpha() ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG;
result.compress(compressFormat, 100, stream);
result.recycle();
return stream.toByteArray();
}

相关内容

  • 没有找到相关文章

最新更新