Wednesday, 15 August 2012

java - convert bitmap to 1bit bitmap -


I am trying to print the Russian text on the printer, but it is not supported, so I replaced the image with the text Decided to print. Here's how to create an image with text:

  public bitmap textAsBitmap {paint color = new paint () (string text, textSize, integer textcolor floats); Paint.setTextSize (textSize); Paint.setColor (textcolor); Paint.setAntiAlias ​​(right); Paint.setTypeface (Typeface.SANS_SERIF); Paint.setTextAlign (Paint.Align.LEFT); // Intensity width = (int) (paint. Major text (text) + 0.5 F); // round full width = 200; // round float baseline = (int) (-paint.cent (+ 0.5F); // acent () is negative / intensity = (int) (baseline + paint. Descent (+ 0.5f); intensity height = 60; Log.e ("height", height + ""); Log.e ("width", width + ""); bitmap image = bitmap.centbitmap (width, 2 * height, bitmap config.argb_8888); Canvas canvas = new canvas (canvas.drawColor (Color.WHITE); canvas left text (text, 0, baseline, paint); base + = 20; canvas left text (text, 0, baseline, paint); returnV;}  

This works fine but the problem is that Chinese printers are printing only 1 bit bitmap. That's why I have a method of converting this image to 1 bit bitmap I tried the solution but I found: Enter image details here

But this image is bad (

No solution found I answered myself, see below

It is a miracle people, I got it working. After bitma crating [I changed it to 32bpp so the last bit

  Paint color = each set to pixel or white (depending on the new paint); Paint.setTextSize (textSize); Paint.setColor (textcolor); Paint.setTextAlign (Paint.Align.LEFT); Paint.setTypeface (Typeface.MONOSPACE); Integer width = (int) (paint.measureText (text) + 0.5f); // round name baseline = (int) (-paint.ascent () + 0.5f); // climb + 0.5f) () negative integer height = (int) (baseline + paint.descent); 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.creditbetaMap (width, height, bitmap.sf. ARGB_8888); canvas canvas1 = new canvas ( BMPMonocrome); color matrix = new color matrix (); ma.setSaturation (0); paint paint 1 = new paint (); Paint.setColorFilter (new ColorMatrixColorFilter (ma); Canvas1.drawBitmap (Image, 0, 0, Paint 1); Int width2 = bmpMonochrome.getWidth (); Int height2 = bmpMonochrome.getHeight (); Int [] Pixel = New int [width 2 * height 2]; BmpMonochrome.getPixels (pixels, 0, width 2, 0, 0, width 2, height 2); // for altitude (intestine y = 0; y and lt; height2; y ++) on intrate {int Offset = y * height2; // Width (int x = 0; x & lt; width 2; x ++) for greater than intensity {int pixel = bmpMonochrome.getPixel (x, y); intcombied = pixels & amp; ; 0xff; If (lowest bit & lt; 128) bmpMonochrome.setPixel (X, Y, Color. Black); And BMPMonochrome.SetPixel (X, Y, Color. WHITE); }}  

thanks


No comments:

Post a Comment