Android:将位图转换为单色位图(每像素 1 位) [英] Android: Converting a Bitmap to a Monochrome Bitmap (1 Bit per Pixel)

查看:28
本文介绍了Android:将位图转换为单色位图(每像素 1 位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将位图打印到移动蓝牙打印机 (Bixolon SPP-R200) - SDK 不提供直接方法来打印内存中的图像.所以我想像这样转换一个位图:

I want to print a Bitmap to a mobile Bluetooth Printer (Bixolon SPP-R200) - the SDK doesn't offer direkt methods to print an in-memory image. So I thought about converting a Bitmap like this:

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

到单色位图.我正在使用 Canvas 在上面给定的 Bitmap 上绘制黑色文本,效果很好.但是,当我将上述 Bitmap 转换为 ByteArray 时,打印机似乎无法处理这些字节.我怀疑我需要一个每像素一个位的数组(一个像素可以是白色 = 1 或黑色 = 0).

To a Monochrome Bitmap. I am drawing black text on above given Bitmap using a Canvas, which works well. However, when I convert the above Bitmap to a ByteArray, the printer seems to be unable to handle those bytes. I suspect I need an Array with one Bit per Pixel (a Pixel would be either white = 1 or black = 0).

由于似乎没有方便、开箱即用的方法来做到这一点,我的一个想法是使用:

As there seems to be no convenient, out of the box way to do that, one idea I had was to use:

bitmap.getPixels(pixels, offset, stride, x, y, width, height)

获取像素.我想,我必须按如下方式使用它:

to Obtain the pixels. I assume, I'd have to use it as follows:

int width = bitmap.getWidth();
int height = bitmap.getHeight();

int [] pixels = new int [width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

但是 - 我不确定一些事情:

However - I am not sure about a few things:

  • 在 getPixels 中 - 简单地将宽度作为Stride"参数传递是否有意义?
  • 我想我必须评估每个像素的颜色信息并将其切换为黑色或白色(我会将这个值写入一个新的目标字节数组中,最终将其传递给打印机)?
  • 如何最好地评估每个像素的颜色信息以决定它应该是黑色还是白色?(渲染出来的Bitmap是白底黑痛)

这种方法有意义吗?有没有更简单的方法?仅仅使位图变黑是不够的 &白色,主要问题是将每个像素的颜色信息减少到一位.

Does this approach make sense at all? Is there an easier way? It's not enough to just make the bitmap black & white, the main issue is to reduce the color information for each pixel into one bit.

更新

按照 Reuben 的建议,我将首先将位图转换为单色位图.然后我将遍历每个像素:

As suggested by Reuben I'll first convert the Bitmap to a monochrome Bitmap. and then I'll iterate over each pixel:

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    // Iterate over height
    for (int y = 0; y < height; y++) {
        int offset = y * height;
        // Iterate over width
        for (int x = 0; x < width; x++) {
            int pixel = bitmap.getPixel(x, y);
        }
    }

现在 Reuben 建议读取每个 32 位像素的最低字节" - 这与我关于如何评估像素颜色的问题有关.我在这方面的最后一个问题:我是否通过简单地这样做来获得最低字节:

Now Reuben suggested to "read the lowest byte of each 32-bit pixel" - that would relate to my question about how to evaluate the pixel color. My last question in this regard: Do I get the lowest byte by simply doing this:

// Using the pixel from bitmap.getPixel(x,y)
int lowestByte = pixel & 0xff;

推荐答案

您可以使用 ColorMatrix 将图像转换为单色 32bpp.

You can convert the image to monochrome 32bpp using a ColorMatrix.

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);

这简化了颜色->单色转换.现在您可以执行 getPixels() 并读取每个 32 位像素的最低字节.如果它是 <128,则为 0,否则为 1.

That simplifies the color->monochrome conversion. Now you can just do a getPixels() and read the lowest byte of each 32-bit pixel. If it's <128 it's a 0, otherwise it's a 1.

这篇关于Android:将位图转换为单色位图(每像素 1 位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆