zoukankan      html  css  js  c++  java
  • C# GDI+绘制一维条码打印模糊的解决办法

    最近遇到使用zxing生成的一维条码打印出来的条码图形很模糊根本识别不了。其实原因只有一句话: bitmap没有直接使用PrintDocument的Graphics画布进行绘制,而是中间处理了一下外部传过来一个图片,这个图片看起来很成像质量很好,但其实是一个彩色图片,一维条码是由黑白两种颜色组成的,没有灰度是两种纯色。这样打印出来的图片看起来有毛刺,直线不连续了。解决方法是减少中间环节直接把zxing生成的bitmap对象使用"PrintDocument的Graphics画布"绘制。PrintDocument的Graphics和Bitmap的Graphics要是同一个对象。

    记录一下,zxing生成黑白图片的办法:

    Dictionary<EncodeHintType, Object> hintMap = new Dictionary<EncodeHintType, Object>();
                       //设置编码
                       hintMap.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
    
                       // Now with zxing version 3.2.1 you could change border size (white
                       // border size to just 1)
                       //设置间距
                       hintMap.Add(EncodeHintType.MARGIN, 0);
                       //设置纠错级别
                       hintMap.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    
                       QRCodeWriter qrCodeWriter = new QRCodeWriter();
                       //ZXing.MultiFormatWriter qrCodeWriter = new ZXing.MultiFormatWriter();//new QRCodeWriter();
                       //BitMatrix 根据其需要输出的参数,和设置条件等新建BitMatrix对象
                       BitMatrix byteMatrix = qrCodeWriter.encode(value, BarcodeFormat.QR_CODE, width, height, hintMap);
    
                       var imgInfo = new Bitmap(width, height);
    
                       for (int x = 0; x < byteMatrix.Width; x++)
                       {
                           for (int y = 0; y < byteMatrix.Width; y++)
                           {
                               if (byteMatrix[y, x])
                               {
                                   if (graphics != null)
                                       graphics.FillRectangle(Brushes.Black, x + viewX, y + viewY, 1, 1);
                                   imgInfo.SetPixel(x, y, Color.Black);
                               }
                               else
                               {
                                   if (graphics != null)
                                       graphics.FillRectangle(Brushes.White, x + viewX, y + viewY, 1, 1);
                                   imgInfo.SetPixel(x, y, Color.White);
                               }
                           }
                       }

    BitMatrix是zxing将字符Encode成条码图形的一种返回值类型,是条码图形的矩阵表示方式。转换成bitmap时就是需要每个像素重绘一遍。这样生成的图形就是黑白两种颜色的。

  • 相关阅读:
    2.9数据-paddlepaddle数据集wmt16
    2.8数据-paddlepaddle数据集uci_housing
    2.6数据-paddlepaddle数据集movielens
    2.5数据-paddlepaddle数据集imikolov
    2.4数据-paddlepaddle数据集imdb
    2.3数据-paddlepaddle数据集Conll05
    在android程序中怎么执行ifconfig命令来修改android 的ip地址,
    VMware 11安装Mac OS X 10.10 及安装Mac Vmware Tools.
    xcode7 如何真机测试
    海子
  • 原文地址:https://www.cnblogs.com/datacool/p/datacool_2018_zxingbarcode.html
Copyright © 2011-2022 走看看