java - 将位图转换为 1 位位图

标签 java android bitmap bit

我想在打印机上打印俄文文本,但不支持,所以我决定打印带文本的图像。以下是我如何使用文本创建图像:

 public Bitmap textAsBitmap(String text, float textSize, int textColor) {
        Paint paint = new Paint();
        paint.setTextSize(textSize);
        paint.setColor(textColor);
        paint.setAntiAlias(true);
        paint.setTypeface(Typeface.SANS_SERIF);
        paint.setTextAlign(Paint.Align.LEFT);
        //int width = (int) (paint.measureText(text) + 0.5f); // round
        int width = 200; // round
        float baseline = (int) (-paint.ascent() + 0.5f); // ascent() is negative
        //int height = (int) (baseline + paint.descent() + 0.5f);
        int height=60;
        Log.e("height",height+"");
        Log.e("width",width+"");
        Bitmap image = Bitmap.createBitmap(width, 2*height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(image);
        canvas.drawColor(Color.WHITE);
        canvas.drawText(text, 0, baseline, paint);
        baseline+=20;
        canvas.drawText(text, 0,baseline , paint);

        return image;
    }

它工作正常。但问题是这台中文打印机只打印 1 位位图。所以我需要一种方法将此图像转换为 1 位位图。 我试过 this解决方案 但我得到了: enter image description here

但是这张图不好(

找到解决方案 我自己回答,见下文

最佳答案

伙计们,这是一个奇迹,我成功了。装箱后 bitma[ 我将其转换为 32bpp 然后根据最后一位将每个像素设置为黑色或白色

Paint paint = new Paint();
    paint.setTextSize(textSize);
    paint.setColor(textColor);
    paint.setTextAlign(Paint.Align.LEFT);
    paint.setTypeface(Typeface.MONOSPACE);
    int width = (int) (paint.measureText(text) + 0.5f); // round
    float baseline = (int) (-paint.ascent() + 0.5f); // ascent() is negative
    int height = (int) (baseline + paint.descent() + 0.5f);
    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    canvas.drawColor(Color.WHITE);
    canvas.drawText(text, 0, baseline, paint);




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


    int width2 = bmpMonochrome.getWidth();
    int height2 = bmpMonochrome.getHeight();

    int[] pixels = new int[width2 * height2];
    bmpMonochrome.getPixels(pixels, 0, width2, 0, 0, width2, height2);

    // Iterate over height
    for (int y = 0; y < height2; y++) {
        int offset = y * height2;
        // Iterate over width
        for (int x = 0; x < width2; x++) {
            int pixel = bmpMonochrome.getPixel(x, y);
            int lowestBit = pixel & 0xff;
            if(lowestBit<128)
                bmpMonochrome.setPixel(x,y,Color.BLACK);
            else
                bmpMonochrome.setPixel(x,y,Color.WHITE);
        }
    }

感谢Android: Converting a Bitmap to a Monochrome Bitmap (1 Bit per Pixel)

关于java - 将位图转换为 1 位位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29078142/

相关文章:

android - 在appium中查找 ListView 中的所有元素

java - Impala单个插入语句创建多个文件

java - 使用 jsoup 替换 HTML 标签

java - 在 JCA 中为 TLS 定义密码套件

java - 坏或没有互联网连接检查器

android - 谷歌云消息 (GCM) 问题

macos - NSImage 和 PDFImageRep 缓存仍然仅以一种分辨率绘制

c - 使用 Snow Leopard 访问原始图像数据

c# - 在 Unity 中读取二维码

java - OpenSessionInViewInterceptor/Filter 来处理多个 SessionFactory