package cn.qrcode;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.decoder.Mode;
import com.google.zxing.qrcode.decoder.Version;
import com.google.zxing.qrcode.encoder.QRCode;
public class QRTest2 {
	public static void main(String[] args) {
		try {
			createImage("hello world!", "src/a.png", 200, 200);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * @param contents 字符串内容
	 * @param width 宽度
	 * @param height  高度
	 * @param imgPath 图片输出路径
	 */
	public void encode(String contents, int width, int height, String imgPath) {
		Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
		// 指定纠错等级
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		// 指定编码格式
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		try {
			BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
			MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPath));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * @param sms_info 信息内容
	 * @param filePath 输出路径
	 * @param width 宽度
	 * @param height 高度
	 * @throws Exception
	 */
	public static void createImage(String sms_info, String filePath, int width,
			int height) throws Exception {
		try {
			QRCode qrCode = new QRCode();
			qrCode.setECLevel(ErrorCorrectionLevel.M);
			qrCode.setMode(Mode.BYTE);
			qrCode.setVersion(Version.getVersionForNumber(7));
			String testString = sms_info;
			byte[] d = testString.getBytes("UTF-8");
			System.out.println(d.length);
			
			// 限制最大字节数为120
			if (d.length > 0 && d.length < 120) {
				boolean[][] s = new boolean[10][15];
				BufferedImage bi = new BufferedImage(s.length, s[0].length, BufferedImage.TYPE_BYTE_BINARY);
				Graphics2D g = bi.createGraphics();
				g.setBackground(Color.WHITE);
				g.clearRect(0, 0, s.length, s[0].length);
				g.setColor(Color.BLACK);
				int mulriple = 1;
				for (int i = 0; i < s.length; i++) {
					for (int j = 0; j < s.length; j++) {
						if (s[j][i]) {
							g.fillRect(j * mulriple, i * mulriple, mulriple, mulriple);
						}
					}
				}
				g.dispose();
				bi.flush();
				File f = new File(filePath);
				if (!f.exists()) {
					f.createNewFile();
				}
				bi = resize(bi, width, height);
				// 创建图片
				ImageIO.write(bi, "png", f);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 图像缩放
	 * 
	 * @param source
	 * @param targetW
	 * @param targetH
	 * @return
	 */
	private static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
		int type = source.getType();
		BufferedImage target = null;
		double sx = (double) targetW / source.getWidth();
		double sy = (double) targetH / source.getHeight();
		target = new BufferedImage(targetW, targetH, type);
		Graphics2D g = target.createGraphics();
		g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
		g.dispose();
		return target;
	}
}
 最近下载更多
最近下载更多
                
                zdmabc     LV3
                2024年6月27日
            
            
        
                xuexizhuanyong23     LV16
                2022年3月23日
            
            
        
                yangctz     LV25
                2021年9月29日
            
            
        
                yiangeee     LV9
                2021年1月14日
            
            
        
                abcd11112     LV6
                2020年8月12日
            
            
        
                hanweinan6     LV13
                2020年6月29日
            
            
        
                huasir2018     LV14
                2020年5月9日
            
            
        
                霸道小盆友     LV3
                2020年4月30日
            
            
        
                sonofy123     LV9
                2020年2月14日
            
            
        
                kong.yee     LV40
                2020年2月1日
            
            
        
 
                 
     
                 最近浏览
最近浏览