调整一维条码宽单元窄单元比例(ZXing2.3)

  最近用到一维条码、二维条码,看了几种方案,选了ZXing,因为即支持一维条码、二维条码的生成,也支持条码的识读。
  结果在应用的过程中遇到了问题,有的单位使用的是针式打印机,一式多份的,打印出来的一维条码识别率很低。这是个致命的问题,必须要解决。仔细阅读了一维条码的原理、码制,发现一个说法,条码的宽单元与窄单元的比例(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 erik.barbara@gmail.com (Erik Barbara), Modified by wallimn@sohu.com
*/
public final class Code393Writer extends OneDimensionalCodeWriter {

@Override
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType,?> hints) 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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值