问题描述
- android系统pos机采用serialport串口操作打印机,打印出来的二维码不是正方形的
-
Bitmap bitmap = Create2DCode("测试二维码"); int bmWidth = bitmap.getWidth(); int bmHeight = bitmap.getHeight(); byte[] data = new byte[]{0x00}; Color prefix = new Color(); int prefixint; /* * Set line spacing */ mOutputStream_P.write(0x1b); mOutputStream_P.write(0x33); mOutputStream_P.write(8); int[] dots = new int[bmWidth * bmHeight]; // //把位图的数据拷贝到dots数组中,每一个都由一个表示颜色值的int值来表示 bitmap.getPixels(dots, 0, bmWidth, 0, 0, bmWidth, bmHeight); for (int i = 0; i < ((bmHeight+7)/8); i++) { mOutputStream_P.write(0x1b); mOutputStream_P.write(0x2a); mOutputStream_P.write(0); mOutputStream_P.write(bmWidth % 256); mOutputStream_P.write(bmWidth / 256); for (int j = 0; j < bmWidth; j++) { for (int k = 0; k < 8; k++) { if (((i * 8) + k) < bmHeight) // if within the BMP size { prefixint = bitmap.getPixel(j, (i * 8) + k); if (prefix.BLACK == prefixint) { data[0] += (byte)(128 >> k); } } } mOutputStream_P.write(data); data[0] = 0x00; } mOutputStream_P.write(10); }
/**
* 用字符串生成二维码
* @param str
* @author
* @return
* @throws WriterException
*/
public Bitmap Create2DCode(String str) throws WriterException {
Hashtable hint = new Hashtable();
hint.put(EncodeHintType.CHARACTER_SET, "GBK");
//生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, 144, 96,hint);
int width = matrix.getWidth();
int height = matrix.getHeight();
//二维矩阵转为一维像素数组,也就是一直横着排了
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if(matrix.get(x, y)){
pixels[y * width + x] = 0xff000000;//black
}else{
pixels[y * width + x] = 0xffffffff;//white
}} } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); //通过像素数组生成bitmap,具体参考api bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
解决方案
博主,你好,问下你解决这个问题没有??