结果在应用的过程中遇到了问题,有的单位使用的是针式打印机,一式多份的,打印出来的一维条码识别率很低。这是个致命的问题,必须要解决。仔细阅读了一维条码的原理、码制,发现一个说法,条码的宽单元与窄单元的比例(W/N Ratio)可以为2至3。看一下打印结果,ZXing打印出来的好像是2,如果调整到3,是不是能提高识别率呢?
可是,把ZXing的接口反复看了几遍,没有找到能改的地方,觉得这是一个比较重要的参数,ZXing不提供有点不厚道呀!
只好改源码了,阅读了一下源码,发现ZXing确实不支持调整宽窄单元比,因为2是硬编码在代码里的,为了不影响原有的代码,我就自已加一种码制吧,起个名叫Code393,表示宽窄单元比为3的Code39码。把我的代码完整贴一下,改动并不多,加了一点儿注释。还要修改BarcodeFormat、MultiFormatWriter,很简单,这里不做介绍了。
/* * Copyright 2010 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.zxing.oned; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import java.util.Map; /** * This object renders a CODE39 code as a {@link BitMatrix}. * 宽单元这宽度为窄单元的2到3倍,这是我自定义的3倍的单元比 * * @author [email protected] (Erik Barbara), Modified by [email protected] */ public final class Code393Writer extends OneDimensionalCodeWriter { @Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Maphints) throws WriterException { if (format != BarcodeFormat.CODE_39_3) { throw new IllegalArgumentException("Can only encode CODE_39, but got " + format); } return super.encode(contents, format, width, height, hints); } @Override public boolean[] encode(String contents) { int length = contents.length(); if (length > 80) { throw new IllegalArgumentException( "Requested contents should be less than 80 digits long, but got " + length); } int[] widths = new int[9]; //int codeWidth = 24 + 1 + length; int codeWidth = 1 + length;//每个字符间的空 for (int i = 0; i < length; i++) { int indexInString = Code39Reader.ALPHABET_STRING.indexOf(contents.charAt(i)); if (indexInString < 0) { throw new IllegalArgumentException("Bad contents: " + contents); } toIntArray(Code39Reader.CHARACTER_ENCODINGS[indexInString], widths); for (int width : widths) { codeWidth += width; } } toIntArray(Code39Reader.CHARACTER_ENCODINGS[39], widths); //起始码、结束码占的宽度 for (int width : widths) { codeWidth += width; } for (int width : widths) { codeWidth += width; } boolean[] result = new boolean[codeWidth]; int pos = appendPattern(result, 0, widths, true); int[] narrowWhite = {1}; pos += appendPattern(result, pos, narrowWhite, false); //append next character to bytematrix for(int i = length-1; i >= 0; i--) { int indexInString = Code39Reader.ALPHABET_STRING.indexOf(contents.charAt(i)); toIntArray(Code39Reader.CHARACTER_ENCODINGS[indexInString], widths); pos += appendPattern(result, pos, widths, true); pos += appendPattern(result, pos, narrowWhite, false); } toIntArray(Code39Reader.CHARACTER_ENCODINGS[39], widths); pos += appendPattern(result, pos, widths, true); return result; } private static void toIntArray(int a, int[] toReturn) { for (int i = 0; i < 9; i++) { int temp = a & (1 << i); toReturn[i] = temp == 0 ? 1 : 3;//原来此处为3 } } }
调整好了,打印一试,识别率比较令人满意。
另,此项修改,不知对条码识别有没有影响,本人没有检查识读部分的代码。
本人原创,转载请保留原文地址:http://wallimn.iteye.com/blog/2111896