package com;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.servlet.ServletContext;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class ZxingEncoderHandler{
// private ServletContext servletContext;
/** 编码
* @param contents
* @param width
* @param height
* @param imgPath
* @throws WriterException
* @throws IOException
*/
public BufferedImage process(String contents, int width, int height) throws WriterException, IOException{
Hashtable<Object, Object> hints = new Hashtable<Object, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 指定纠错等级
hints.put(EncodeHintType.CHARACTER_SET, "GBK"); // 指定编码格式
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.QR_CODE, width, height, hints);
int BLACK = 0xFF000000;
int WHITE = 0xFFFFFFFF;
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y)?BLACK:WHITE);
}
}
return image;
}
public BufferedImage process_new(String contents, int width, int height) throws WriterException, IOException{
Hashtable<Object, Object> hints = new Hashtable<Object, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "GBK");
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.QR_CODE, width, height, hints);
int BLACK = 0xFF000000;
int WHITE = 0xFFFFFFFF;
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y)?BLACK:WHITE);
}
}
int m = 0;
for (int x = 0; x < width; x++) {
if(bitMatrix.get(x, x)){
m = x;
break;
}
}
image=cutWhiteSpace_new(image,"png",m);
return image;
}
public void encode(String contents, int width, int height, String imgPath) {
try{
BufferedImage bi = process(contents,width, height);
bi = cutWhiteSpace(bi,"png");
bi = addLogo(bi);
File file = new File(imgPath);
if (!file.exists()) {
file.mkdirs();
}
ImageIO.write(bi,"png",file);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 生成不带图片的二维码
* @param contents
* @param width
* @param height
* @param imgPath
*/
public void noPictureEncode(String contents, int width, int height, String imgPath) {
Hashtable<Object, Object> hints = new Hashtable<Object, 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);
File file = new File(imgPath);
if (!file.exists()) {
file.mkdirs();
}
MatrixToImageWriter.writeToFile(bitMatrix, "png", file);
} catch (Exception e) {
e.printStackTrace();
}
}
public void encodeM(String contents, int width, int height, String imgPath) {
try{
BufferedImage bi = process_new(contents,width, height);
bi = addLogo(bi);
File file = new File(imgPath);
if (!file.exists()) {
file.mkdirs();
}
ImageIO.write(bi,"png",file);
}catch(Exception e){
e.printStackTrace();
}
}
public BufferedImage cutWhiteSpace(BufferedImage in,String type) throws IOException{
Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(type);
ImageReader reader = it.next();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(in, type, os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
ImageInputStream iis = ImageIO.createImageInputStream(is);
reader.setInput(iis, false);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(20, 20, 110,110);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0,param);
return bi;
}
public BufferedImage cutWhiteSpace_new(BufferedImage in,String type,int height) throws IOException{
Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(type);
ImageReader reader = it.next();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(in, type, os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
ImageInputStream iis = ImageIO.createImageInputStream(is);
reader.setInput(iis, false);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(height-5, height-5, 150-2*height+9,150-2*height+9);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0,param);
return bi;
}
public BufferedImage addLogo(BufferedImage qrcode) throws IOException{
// String path = servletContext.getRealPath("/");
// BufferedImage logo=ImageIO.read(new File(path+"manage/act_add/22.png"));
BufferedImage logo=ImageIO.read(new File("E:\\test\\11.png"));
int width = logo.getWidth();
int height = logo.getHeight();
int x=(qrcode.getWidth()-logo.getWidth())/2;
int y=(qrcode.getHeight()-logo.getHeight())/2;
int[] imageArray = new int[width * height];
imageArray = logo.getRGB(0, 0, width, height, imageArray,0, width);
qrcode.setRGB(x, y, width, height, imageArray, 0, width);
return qrcode;
}
// public void setServletContext(ServletContext arg0) {
// servletContext = arg0;
// }
}
二维码
最新推荐文章于 2023-06-16 17:24:51 发布