二维码融合模板
二维码融合图片
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.skjd.service.system.driver.DriverService; public class ImageAndQRcode { @Autowired
DriverService driverService; private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; ImageAndQRcode() { } private static BufferedImage toBufferedImage(BitMatrix matrix) { 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);
}
}
return image;
} private static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
} /** * @Title: 构造图片 * @Description: 生成水印并返回java.awt.image.BufferedImage * @param file源文件(图片) * @param waterFile水印文件(图片) * @param x距离右下角的X偏移量 * @param y 距离右下角的Y偏移量 * @param alpha 透明度, 选择值从0.0~1.0: 完全透明~完全不透明 * @return BufferedImage * @throws IOException */
private static BufferedImage watermark(File file, File waterFile, int x, int y, float alpha) throws IOException { Image src=Toolkit.getDefaultToolkit().getImage(file.getPath());
BufferedImage image=BufferedImageBuilder.toBufferedImage(src); Image src1=Toolkit.getDefaultToolkit().getImage(waterFile.getPath());
BufferedImage image1=BufferedImageBuilder.toBufferedImage(src1); Graphics2D g2d = image.createGraphics(); int waterImgWidth = image1.getWidth();// 获取层图的宽度 int waterImgHeight = image1.getHeight();// 获取层图的高度 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); g2d.drawImage(image1, x, y, waterImgWidth, waterImgHeight, null); g2d.dispose();// 释放图形上下文使用的系统资源 return image;
} /**
* 输出水印图片
* @param buffImg 图像加水印之后的BufferedImage对象
* @param savePath 图像加水印之后的保存路径
*/
private void generateWaterFile(BufferedImage buffImg, String savePath) { int temp = savePath.lastIndexOf(".") + 1; try { ImageIO.write(buffImg, savePath.substring(temp), new File(savePath)); } catch (IOException e1) { e1.printStackTrace();
} } /**
*
* @param text 二维码内容
* @param width 二维码图片宽度
* @param height 二维码图片高度
* @param format 二维码的图片格式
* @param sourceFilePath 底层图片路径
* @param waterFilePath 二维码路径
* @param saveFilePath 合成图片路径
* @param maginx 二维码距离底图x轴距离
* @param maginy 二维码距离底图y轴距离
* @throws Exception
* @return */
public static byte[] addImageQRcode(String text, int width, int height, String format, String sourceFilePath,
String waterFilePath, String saveFilePath, int maginx, int maginy) throws Exception { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码 BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); File outputFile = new File(waterFilePath); ImageAndQRcode.writeToFile(bitMatrix, format, outputFile); ImageAndQRcode newImageUtils = new ImageAndQRcode(); BufferedImage buffImg = ImageAndQRcode.watermark(new File(sourceFilePath), new File(waterFilePath), maginx,
maginy, 1.0f); newImageUtils.generateWaterFile(buffImg, saveFilePath); ByteArrayOutputStream os = new ByteArrayOutputStream();
boolean flag = ImageIO.write(buffImg, "jpg", os);
byte[] byteArray = os.toByteArray();
return byteArray; } public static void main(String[] args) throws IOException { ImageAndQRcode add = new ImageAndQRcode(); /**
*
* @param text 二维码内容
* @param width 二维码图片宽度
* @param height 二维码图片高度
* @param format 二维码的图片格式
* @param sourceFilePath 底层图片路径
* @param waterFilePath 二维码路径
* @param saveFilePath 合成图片路径
* @param maginx 二维码距离底图x轴距离
* @param maginy 二维码距离底图y轴距离
* @throws Exception
*/
try { add.addImageQRcode("http://www.baidu.com", 300, 300, "jpg",
"C:/Users/admin/Desktop/1.png",
"C:/Users/admin/Desktop/new.jpg",
"C:/Users/admin/Desktop/3.png",
250, 250); } catch (Exception e) { e.printStackTrace(); }
}
}
调用案例
public static byte[] t4(HttpServletRequest request,String t,String id){
String realPath = "";
if("1".equals(t)){
realPath = request.getSession().getServletContext().getRealPath("/static/images/1.png").replaceAll("\\\\", "/");
}else{
realPath= request.getSession().getServletContext().getRealPath("/static/images/2.png").replaceAll("\\\\", "/");
}
String uuid1 = UuidUtils.getUUID();
String uuid3 = UuidUtils.getUUID();
String realPath3= request.getSession().getServletContext().getRealPath("/static/images/)"+uuid1+"(.jpg").replaceAll("\\\\", "/");
String realPath4 = request.getSession().getServletContext().getRealPath("/static/images/)"+uuid3+"(.jpg").replaceAll("\\\\", "/");
String contentz = "www.skjiadao.com?id="+id+"&type="+t; byte[] addImageQRcode1;
try {
addImageQRcode1 = ImageAndQRcode.addImageQRcode(contentz, 300, 300, "jpg",
realPath,
realPath4,
realPath3,
250,
250);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new byte[1];
}
return addImageQRcode1; }