Как конвертировать обычное растровое изображение в монохромный растровый Android - PullRequest
0 голосов
/ 24 сентября 2019

Привет. Я хочу преобразовать обычное растровое изображение в монохромное растровое изображение в 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);

Я хочу передать растровое изображение, а взамен сделать ставку на массив байтов монохромного растрового изображения.

  public static byte[] toBytes(Bitmap bitmap) {

  }

Любые указатели или ссылки, пожалуйста

R

Ответы [ 2 ]

1 голос
/ 24 сентября 2019

На основе вашего кода:

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();
}
1 голос
/ 24 сентября 2019

Вы можете использовать калибровку ColorMatrix для достижения этой цели:

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 рассматривается в этой сущности .

...