Jenya  Kirmiza
Jenya Kirmiza

Reputation: 531

convert bitmap to 1bit bitmap

I'm trying to print russian text on printer, but it's not supported, so i decided to print image with text. Here's how i create image with text:

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

It works fine. But the problem is this chinese printer is printing only 1bit bitmap.So i need a method to convert this image to 1bit bitmap. I tried this solution but i got: enter image description here

but this image is bad(

SOLUTION FOUND I answered myself, see below

Upvotes: 1

Views: 3680

Answers (2)

Jenya  Kirmiza
Jenya Kirmiza

Reputation: 531

It's a miracle guys, i got it working. After crating bitma[ i converted it to 32bpp then set each to pixel to Black or White depending on the last bit

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

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

Upvotes: 4

DJGummikuh
DJGummikuh

Reputation: 108

I'm pretty certain that your issue is only the font you are using. Since modern fonts gain a lot of their readability by using antialiasing, it is quite possible that your font is exactly looking like that when all those grey colors (which are used for antialiasing) are stripped off.

I believe the only chance you have is altering the font to one where that issue isn't that big. Try typeface MONOSPACE for instance.

You might also think about using a complete Bitmapfont, i.e. a font where you manually create a typeset where each letter has a representing bitmap that you use to represent that letter on your bitmap that you send to the printer.

Last but not least you could try to simply print larger, therefore allocating more pixels to each letter and reducing the impact of single-pixel black/white decisions.

Upvotes: 1

Related Questions