二维码图片生成_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 二维码图片生成

二维码图片生成

 2018/2/27 18:22:45  chenjiayi302  程序员俱乐部  我要评论(0)
  • 摘要:writeImageLocal(filePath,createImages(content,imgPath,120,true));/***生成新图片*/publicBufferedImagewriteImageLocal(StringfilePath,BufferedImageimg){BufferedImageim=null;if(filePath!=null&&img!=null){try{Fileoutputfile=newFile(filePath);ImageIO
  • 标签:图片 二维码
     
        writeImageLocal(filePath, createImages(content, imgPath, 120, true));
     
        /**
         * 生成新图片
         */ 
        public BufferedImage writeImageLocal(String filePath, BufferedImage img) { 
    BufferedImage im = null;
            if (filePath != null && img != null) { 
                try { 
                    File outputfile = new File(filePath); 
                    ImageIO.write(img, "jpg", outputfile);
                    File image = new File(filePath);
                    im = ImageIO.read(image);
                } catch (Exception e) { 
                    System.out.println(e.getMessage()); 
                }
            } 
            return im;
        } 
       
       
    /**
* 自定义生成二维码尺寸
* @param content
* @param imgPath
* @param needCompress
* @return
* @throws Exception
*/
public static BufferedImage createImages(String content, String imgPath,Integer size,
boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE,size, size, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000
: 0xFFFFFFFF);
}
}
if (imgPath == null || "".equals(imgPath)) {
return image;
}
// 插入图片
MyQRCodeUtil.insertImage(image, imgPath, needCompress);
return image;
}


/**
* 插入LOGO
*
* @param source
*            二维码图片
* @param imgPath
*            LOGO图片地址
* @param needCompress
*            是否压缩
* @throws Exception
*/
private static void insertImage(BufferedImage source, String imgPath,
boolean needCompress) throws Exception {
File file = new File(imgPath);
if (!file.exists()) {
log.error(""+imgPath+"   该文件不存在!");
return;
}
Image src = ImageIO.read(new File(imgPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width > WIDTH) {
width = WIDTH;
}
if (height > HEIGHT) {
height = HEIGHT;
}
Image image = src.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
发表评论
用户名: 匿名