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

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

问题描述

我要打印一个位图到移动蓝牙打印机(SPP BIXOLON-R200) - 该SDK不提供DIR​​EKT方法来打印内存中的图像。因此,我认为有关转换位图是这样的:

 位图位= Bitmap.createBitmap(宽度,高度,Bitmap.Config.ARGB_8888);

要单色位图。我使用的是画布,效果很好对上面给出的位图绘制黑色文本。然而,当我转换上面位图的ByteArray,打印机似乎是无法处理这些字节。我怀疑我需要每个象素一位数组(一个像素是白任= 1或黑色= 0)。

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

  bitmap.getPixels(像素偏移,步幅,X,Y,宽,高)

获取像素。我想,我不得不使用它,如下所示:

  INT宽度= bitmap.getWidth();
INT高度= bitmap.getHeight();INT [] =像素新INT [宽*高]。
bitmap.getPixels(像素,0,宽度,0,0,宽度,高度);

不过 - 我不知道几件事情:


  • 在的getPixels - ?是否有意义简单地传递宽度为跨越的说法

  • 我想我必须评估每一个像素的颜色信息,要么将其切换为黑色或白色(我会写一个新的目标字节数组此值,我将最终传递到打印机)<? / LI>
  • 如何最佳评估,以决定它应该是黑色或白色的每个像素的颜色信息? (渲染位图是黑色的疼痛在白色背景)

请问这种做法意义可言?是否有更简单的方法?这是不够的,只是让黑位和放大器;白,主要问题是降低每个像素的颜色信息转换成一个比特

更新

正如鲁本建议我将位图转换到一个单色位图。然后我会遍历每个像素:

  INT宽度= bitmap.getWidth();
    INT高度= bitmap.getHeight();    INT [] =像素新INT [宽*高]。
    bitmap.getPixels(像素,0,宽度,0,0,宽度,高度);    //迭代高度
    对于(INT Y = 0; Y&LT;高度; Y ++){
        诠释偏移量= Y *高度;
        //迭代宽度
        为(中间体X = 0; X&下;宽度; X ++){
            INT像素= bitmap.getPixel(X,Y);
        }
    }

现在鲁本建议读取每个32位像素的最低字节 - 这将涉及到我关于如何评价像素颜色的问题。我在这方面的最后一个问题:我简单地做这个拿到最低字节:

  //使用从bitmap.getPixel像素(X,Y)
INT lowestByte =像素和放大器; 0xFF的;


解决方案

您可以将图像转换为单色32bpp的使用嘉洛斯。

 位图bmpMonochrome = Bitmap.createBitmap(宽度,高度,Bitmap.Config.ARGB_8888);
帆布帆布=新的Canvas(bmpMonochrome);
嘉洛斯MA =新嘉洛斯();
ma.setSaturation(0);
涂料粉刷=新的油漆();
paint.setColorFilter(新ColorMatrixColorFilter(MA));
canvas.drawBitmap(bmpSrc,0,0,油漆);

这简化了color->单色转换。现在,你可以做的getPixels()和读取每个32位像素的最低字节。如果是&LT; 128是0,否则这是一个1

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

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:

  • In getPixels - does it make sense to simply pass the width as the "Stride" argument?
  • I guess I'd have to evaluate the color information of each pixel and either switch it to black or white (And I'd write this value in a new target byte array which I would ultimately pass to the printer)?
  • How to best evaluate each pixel color information in order to decide that it should be black or white? (The rendered Bitmap is black pain on a white background)

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.

UPDATE

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

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;

解决方案

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

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.

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

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