package com.barcode.test;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
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.qrcode.decoder.ErrorCorrectionLevel;
public class BarcodeUtils {
private static Font font = new Font("微软雅黑", Font.PLAIN, 12);
/**
* 生成二维码
*
* @param contents
* @param width
* @param height
* @param imgPath
*/
public static void encodeQRCode(String contents, int width, int height, String imgPath) {
Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 指定编码格式
hints.put(EncodeHintType.CHARACTER_SET, "GBK");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(imgPath));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解析二维码
*
* @param imgPath
* @return
*/
public static String decodeQRCode(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));
Map<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "GBK");
result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 生成条形码
*
* @param contents
* @param width
* @param height
* @param imgPath
*/
// int width = 105, height = 50; 长度很容易报错:NotFoundException
public static void encodeBarCode(String contents, int width, int height, String imgPath) {
try {
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.AZTEC_LAYERS, contents);
hints.put(EncodeHintType.MARGIN, 4);
// hints.put(EncodeHintType.AZTEC_LAYERS, 100);
// hints.put(EncodeHintType.DATA_MATRIX_SHAPE, 100);
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.CODE_128, width, height,
hints );
FileOutputStream fileOutputStream = new FileOutputStream(imgPath);
MatrixToImageWriter.writeToStream(bitMatrix, "png", fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解析条形码
*
* @param imgPath
* @return
*/
public static String decodeBarCode(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));
/*
* Map<DecodeHintType, Object> hints = new Hashtable<>();
* hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
* hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); result = new
* MultiFormatReader().decode(bitmap, hints);
*/
result = new MultiFormatReader().decode(bitmap, null);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param srcImgPath 源图片路径
* @param tarImgPath 保存的图片路径
* @param waterMarkContent 水印内容
* @param markContentColor 水印颜色
* @param font 水印字体
*/
public static void addWaterMark(String srcImgPath, String tarImgPath, String waterMarkContent,Color markContentColor) {
try {
// 读取原图片信息
File srcImgFile = new File(srcImgPath);//得到文件
Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
int srcImgHeight = srcImg.getHeight(null);//获取图片的高
// 加水印
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight+20, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
g.setBackground(Color.WHITE);//设置背景色
g.clearRect(0, 0, srcImgWidth, srcImgHeight+20);
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight,Color.red, null);
g.setColor(markContentColor); //根据图片的背景设置水印颜色
g.setFont(font); //设置字体
//设置水印的坐标
int x = (srcImgWidth - getWatermarkLength(waterMarkContent, g)) / 2;
int y = srcImgHeight+15;
g.drawString(waterMarkContent, x, y); //画出水印
g.dispose();
// 输出图片
FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
ImageIO.write(bufImg, "png", outImgStream);
System.out.println("添加水印完成");
outImgStream.flush();
outImgStream.close();
} catch (Exception e) {
// TODO: handle exception
}
}
public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
}
public static void main(String[] args) {
BarcodeUtils handler = new BarcodeUtils();
//条形码生成
/* String imgPath2 = "D:/barcode/2.png";
String imgPath3 = "D:/barcode/22.png";
String contents = "3121 3215468 7687";
int width = 105, height = 50;
handler.encodeBarCode(contents, width, height, imgPath2);
addWaterMark(imgPath2, imgPath3, contents, Color.red);*/
//条形码解析
String imgPath = "D:/barcode/out.jpg";
String barcode = handler.decodeBarCode(imgPath);
System.out.println(barcode);
}
}
评论3
最新资源