二维码


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;
// }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值