5,658
社区成员
发帖
与我相关
我的任务
分享 /**
* 将照片logo添加到二维码中间
* @param matrix
* @param format
* @param stream
* @param logoPath
* @throws IOException
*/
public void overlapImage(BitMatrix matrix, String format, OutputStream stream, String logoPath) throws IOException {
BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix, DEFAULT_CONFIG);
BufferedImage logo = ImageIO.read(new File(logoPath));
Graphics2D g = image.createGraphics();
// 考虑到logo照片贴到二维码中,建议大小不要超过二维码的1/5;目前设置的就是最大值,除非不用叠加算法
int width = image.getWidth() / 5;
int height = image.getHeight() / 5;
// logo起始位置,此目的是为logo居中显示
int x = (image.getWidth() - width) / 2;
int y = (image.getHeight() - height) / 2;
// 绘制图
g.drawImage(logo, x, y, width, height, null);
g.dispose();// 清理内存中的图片,返还内存给系统
logo.flush();
image.flush();
// 输出二维码
ImageIO.write(image, format, stream);
}