java zxing生成一维码_java Struts2 Zxing生成一维码

pom 文件如果不是使maven的可以自己下载对应版本的jar

com.google.zxing

core

3.0.0

com.google.zxing

javase

3.0.0

第二部 编写工具类

package com.dianping.emidas.activity.biz.utils;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.google.zxing.BarcodeFormat;

import com.google.zxing.BinaryBitmap;

import com.google.zxing.LuminanceSource;

import com.google.zxing.MultiFormatReader;

import com.google.zxing.MultiFormatWriter;

import com.google.zxing.Result;

import com.google.zxing.WriterException;

import com.google.zxing.client.j2se.BufferedImageLuminanceSource;

import com.google.zxing.client.j2se.MatrixToImageWriter;

import com.google.zxing.common.BitMatrix;

import com.google.zxing.common.HybridBinarizer;

import com.google.zxing.oned.Code128Writer;

public class ZxingUtil{

/**

* 条形码编码

*

* @param contents

* @param width

* @param height

* @param imgPath

*/

public static void encode(String contents, int width, int height, String imgPath) {

int codeWidth = 3 + // start guard

(7 * 6) + // left bars

5 + // middle guard

(7 * 6) + // right bars

3; // end guard

codeWidth = Math.max(codeWidth, width);

try {

BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,

BarcodeFormat.EAN_13, codeWidth, height, null);

MatrixToImageWriter.writeToStream(bitMatrix, "png",

new FileOutputStream(imgPath));

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 解析条形码

*

* @param imgPath

* @return

*/

public static String decode(String imgPath) {

BufferedImage image = null;

Result result = null;

try {

image = ImageIO.read(new File(imgPath));

if (image == null) {

System.out.println("the decode image may be not exit.");

}

LuminanceSource source = new BufferedImageLuminanceSource(image);

BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

result = new MultiFormatReader().decode(bitmap, null);

return result.getText();

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

public static void main(String[] args) {

String imgPath = "c://zxing_EAN13.png";

// 益达无糖口香糖的条形码

// String contents = "6923450657713";

String contents="6901236341292";

int width = 105, height = 50;

ZxingUtil handler = new ZxingUtil();

handler.encode(contents, width, height, imgPath);

System.out.println("Michael ,you have finished zxing EAN13 encode.");

}

private static final String KEY = "keycode";

private static final String WIDTH = "mwidth";

private static final String HEIGHT = "mheight";

private static final String IMAGETYPE = "png";

public static void BarCode(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

String keycode = "6923450657713";

if (keycode != null && !"".equals(keycode)) {

ServletOutputStream stream = null;

try {

Code128Writer writer = new Code128Writer();

int width=180;

int height=60;

String mwidth = req.getParameter(WIDTH);

if (mwidth != null && !"".equals(mwidth.trim())) {

try{

width=Integer.valueOf(mwidth);

} catch (NumberFormatException e) {

//TODO output to log

}

}

String mheight = req.getParameter(HEIGHT);

if (mheight != null && !"".equals(mheight.trim())) {

try{

height = Integer.valueOf(mheight);

} catch (NumberFormatException e) {

//TODO output to log

}

}

int codeWidth = 3 + // start guard

(7 * 6) + // left bars

5 + // middle guard

(7 * 6) + // right bars

3; // end guard

codeWidth = Math.max(codeWidth, width);

stream = resp.getOutputStream();

BitMatrix bitMatrix = new MultiFormatWriter().encode(keycode,

BarcodeFormat.EAN_13, codeWidth, height, null);

MatrixToImageWriter.writeToStream(bitMatrix, IMAGETYPE,

stream);

} catch (WriterException e) {

e.printStackTrace();

} finally {

if (stream != null) {

stream.flush();

stream.close();

}

}

}

}

}

编写Action
  public void barcode(){

try {

ZxingUtil.BarCode(request, response);

} catch (ServletException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

struts2 文件
 

jsp 文件

pageEncoding="UTF-8"%>

Insert title here

111

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java生成一维并在下方显示一维内容的示例代: ```java import java.awt.EventQueue; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import org.apache.commons.lang3.StringUtils; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.oned.Code128Writer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class OneDimBarcodeGenerator { public static void main(String[] args) { String text = "One-dimensional barcode content"; try { BufferedImage barcodeImage = generateOneDimBarcode(text); // Display the barcode image and text in a JFrame EventQueue.invokeLater(() -> { JFrame frame = new JFrame("One-dimensional Barcode"); frame.setSize(400, 200); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(barcodeImage, 0, 0, null); g.drawString(text, 50, 150); } }; frame.setContentPane(panel); frame.setVisible(true); }); } catch (WriterException | IOException e) { e.printStackTrace(); } } public static BufferedImage generateOneDimBarcode(String text) throws WriterException, IOException { // Set the barcode format and encoding hint Code128Writer writer = new Code128Writer(); HashMap<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // Generate the bit matrix and write it to an image BitMatrix bitMatrix = writer.encode(text, BarcodeFormat.CODE_128, 400, 200, hints); BufferedImage barcodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix); return barcodeImage; } } ``` 该示例使用GoogleZXing生成一维,并在JFrame中显示一维图像和下方的一维内容。如果您想在控制台中显示一维,可以简单地使用`System.out.println(text);`来打印一维内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值