class="java" name="code">import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Hashtable; import javax.imageio.ImageIO; import org.apache.log4j.Logger; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public final class MatrixCodeUtil{ private static Logger logger = Logger.getLogger(MatrixCodeUtil.class); private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; public static BitMatrix createQRCode(String content, int width, int height){ Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); //设置字符编码 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 指定纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); BitMatrix matrix = null; try { matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); } catch (WriterException e) { logger.error(e); e.printStackTrace(); } return matrix; } public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { int width = matrix.getWidth(); int height = matrix.getHeight(); 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, matrix.get(x, y) ? BLACK : WHITE); } } ImageIO.write(image, format, file); } public static void addLogoWriteToFile(BitMatrix matrix, String targetPath, String logoPath, int logoPart){ int width = matrix.getWidth(); int height = matrix.getHeight(); 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, matrix.get(x, y) ? BLACK : WHITE); } } BufferedImage logo = null; try { logo = ImageIO.read(new File(logoPath)); } catch (IOException e) { e.printStackTrace(); } Graphics2D g = image.createGraphics(); //考虑到logo照片贴到二维码中,建议大小不要超过二维码的1/5; int logoWidth = width / logoPart; int logoHeight = height / logoPart; //logo起始位置,此目的是为logo居中显示 int x = (height - logoWidth) / 2; int y = (height - logoHeight) / 2; //绘制图 g.drawImage(logo, x, y, logoWidth, logoHeight, null); g.setStroke(new BasicStroke(2)); g.setColor(Color.GRAY); g.drawRect(x, y, logoWidth, logoHeight); g.dispose(); try { ImageIO.write(image, "jpg", new File(targetPath)); } catch (IOException e) { logger.error(e); e.printStackTrace(); } } public static void main(String [] arr){ try { String content = "['name':'test','company':'11111']"; String targetPath = "e:/Desktop/test.jpg"; String logoPath = "e:/Desktop/logo_comp.jpg"; int width = 400; int height = 400; int logoPart = 6; BitMatrix matrix = MatrixCodeUtil.createQRCode(content, width, height); MatrixCodeUtil.addLogoWriteToFile(matrix, targetPath, logoPath, logoPart); } catch (Exception e) { e.printStackTrace(); } } }
?