javaweb条形码产生、打印、扫描

产生条形码的插件到是不少,可是能用针式打印机打印在合适表单上,并且能用经常使用的激光扫描器扫描出来的demo到不是非常多。

本文,所牵扯的代码与工具均亲測可用。

使用工具:

epson  LQ-630K针式打印机

honeywell ms9540 激光条码扫描枪

激光打印机


难度:

激光打印机,无论用什么编码格式。用下文的代码输出图片,扫描枪均能够扫描出来;

针式打印机打印效果没有那么好。打印出来的不能扫描出来,刚開始不知道什么问题。换编码方式。换样式表示方式等。调针式打印机配置等,各种组合測试。


解决方式:

採用下文代码、适当调宽条形码宽度就可以。

也不用添加太大(太大显得不协调),15位(5位字母后面都是数字)一般正常输出的话宽度是51mm左右吧,调到56就能够识别了,当然假设不理想,能够再调下。


demo:

另外项目中要增加jbarcode-0.2.8.jar包,点此下载

项目使用struts框架,struts中例如以下配置:

<action name="barcode" class="XX.YY.ZZAction"> 
<span style="white-space:pre">	</span><result type="stream"> 
<span style="white-space:pre">		</span><param name="contentType">image/jpeg</param> 
<span style="white-space:pre">		</span><param name="inputName">inputStream</param> 
<span style="white-space:pre">	</span></result> 
</action>
前台例如以下调用就可以

<img style="width:56mm" src="barcode.action?

barcode=ABCDE0123456789">

aciton类

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code93Encoder;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.WidthCodedPainter;

public class BarcodeAction {
private ByteArrayInputStream inputStream;
private String barcode;

public String execute() throws Exception {
JBarcode jBarcode = new JBarcode(Code93Encoder.getInstance(), WidthCodedPainter.getInstance(), BaseLineTextPainter.getInstance());
jBarcode .setShowCheckDigit(false);
jBarcode .setCheckDigit(true);
jBarcode .setShowText(false);
jBarcode .setBarHeight(10);
BufferedImage bufferedImage = jBarcode .createBarcode(barcode);
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageOutputStream imageOut = ImageIO.createImageOutputStream(output); 
ImageIO.write(bufferedImage , "JPEG", imageOut);
imageOut.close();
inputStream = new ByteArrayInputStream(output.toByteArray()); 
return "success";
}

public void setInputStream(ByteArrayInputStream inputStream) {
this.inputStream = inputStream;
}

public ByteArrayInputStream getInputStream() {
return inputStream;
}

public String getBarcode() {
return barcode;
}

public void setBarcode(String barcode) {
this.barcode = barcode;
}
}




posted @ 2017-05-25 14:58  jzdwajue  阅读(551)  评论(0编辑  收藏  举报