java实现图片合成,并添加文字
java实现图⽚合成,并添加⽂字
最近公司⼀个需要,需要把商品的优惠卷分享链接,⽣成⼀个⼆维码然后和商品主图合成⼀张,并且在新合成的主图增加商品信息的描述,好了直接看合成后图⽚的样式
下⾯我就直接贴代码,⾸先是Contorller层
/**
* 淘宝⼆维码,商品主图,合成⼀张图
*
* @param pictUrl
* @param request
* @param response
* @throws IOException
*/
@RequestMapping("/getTaoBaoqQRCode")
public void getTaoBaoqQRCode(TaoBaoQRCode taoBaoQRCode, HttpServletRequest request,
HttpServletResponse response) throws IOException {
ServletOutputStream os = null;
InputStream buffin = null;
try {
// ⼆维码
String couponUlr = "https:" + CouponShareUrl();// ⾼额卷分享链接
byte[] imgByte = ateQrCode2Bytes(250, 250, couponUlr);
buffin = new ByteArrayInputStream(imgByte);
BufferedImage couponImage = ad(buffin);
/
/ 商品主图
String imageUrl = "https:" + PictUrl();
URL url = new URL(imageUrl);
BufferedImage picImage = ad(url);
BufferedImage modifyImage =
response.setContentType("image/jpg");
os = OutputStream();
ImageIO.write(modifyImage, "jpg", os);
os.flush();
} catch (Exception e) {
山东省泰安市邮编
<("getTaoBaoqQRCode error");
e.printStackTrace();
} finally {
buffin.close();
os.close();
}
}
⼆维码QrCodeUtil ⽣成帮助类
public class QrCodeUtil {
private static final int DAFAULT_WIDTH = 360;
private static final int DAFAULT_HEIGHT = 360;
private static final Logger LOGGER = Logger(QrCodeUtil.class);
public static String createQrCode(String text) {
return createQrCode(DAFAULT_WIDTH, DAFAULT_HEIGHT, text);
}
public static String createQrCode(int widht, int height, String text) {
HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
try {
byte[] bytes = createQrCode2Bytes(widht, height, text);
String fileName = UUID.randomUUID().toString().replaceAll("-", "") + ".png";
return UpYunClient.upload(fileName, bytes);
} catch (Exception e) {
<("create qrcode error", e);
}
return null;
}
public static byte[] createQrCode2Bytes(String text) {
return createQrCode2Bytes(DAFAULT_WIDTH, DAFAULT_HEIGHT, text);
}
public static byte[] createQrCode2Bytes(int widht, int height, String text) {
HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
try {
BitMatrix bitMatrix =
new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, widht, height,
hints);
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedImage image = BufferedImage(bitMatrix);
ImageIO.write(image, "png", out);
ByteArray();
} catch (Exception e) {
<("create qrcode error", e);
}
考取健身教练
return null;
}
/**
* ⽣成条形码并已字节码形式返回,⽣成的图⽚格式为png
*
* @param contents
* @param width
* @param height
* @return
*/
public static byte[] createBarcode2Byte(String contents, int width, int height) {
int codeWidth = 3 + // start guard
(7 * 6) + // left bars
5 + // middle guard
(7 * 6) + // right bars
3; // end guard
codeWidth = Math.max(codeWidth, width);
try {
BitMatrix bitMatrix =
new MultiFormatWriter().encode(contents, BarcodeFormat.CODE_128, codeWidth,              height, null);
BufferedImage image = BufferedImage(bitMatrix);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "png", out);
ByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
⼆维码⽣成我这⾥⽤的是⾕歌的看下⾯l ⽂件
<!-- 条形码、⼆维码⽣成 -->
<dependency>
<groupId&le.zxing</groupId>
<artifactId>core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId&le.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.2</version>
</dependency>
合成图⽚⽅法如何
package com.qft.campuscirclemon.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.URL;
import javax.imageio.ImageIO;
import org.springframework.stereotype.Component;
@Component
public class ImageHandleUtil {
private Font font = null;
private Graphics2D g = null;
/**
* 导⼊本地图⽚到缓冲区
*
* @param imgName
* @return
*/
public BufferedImage loadImageLocal(String imgName) {
try {
ad(new File(imgName));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 导⼊⽹络图⽚到缓冲区
*
* @param imgName
* @return
*/
public BufferedImage loadImageUrl(String imgName) {
try {
URL url = new URL(imgName);
ad(url);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* ⽣成新图⽚到本地
*
* @param newImage
* @param img
*/
public void writeImageLocal(String newImage, BufferedImage img) {
if (newImage != null && img != null) {
try {
// ⽬录不存在则创建
String dirUrl = newImage.substring(0, newImage.lastIndexOf(File.separator));        File dir = new File(dirUrl);
if (!ists()) {
dir.mkdirs();
舒淇白举纲
}
File outputfile = new File(newImage);
ImageIO.write(img, "png", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
关于考试的作文
* 设定⽂字的字体等
*
* @param fontStyle
* @param fontSize
*/
public void setFont(String name, int style, int fontSize) {
this.font = new Font(name, style, fontSize);
}
/**
* 修改图⽚,返回修改后的图⽚缓冲区(只输出⼀⾏⽂本),图⽚居中显⽰
*
* @param img
* @param content
* @param y
* @param color
* @return
*/
public BufferedImage modifyImage(BufferedImage img, Object content, int y, Color color) {
try {
g = ateGraphics();
g.setBackground(Color.WHITE);
g.setColor(color);// 设置字体颜⾊
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// 抗锯齿      if (this.font != null)
g.setFont(this.font);
int width = Width();// 图⽚宽度
if (content != null) {
String str = String();
int strWidth = g.getFontMetrics().stringWidth(str);// 字体宽度
g.drawString(str, (width - strWidth) / 2, y);
激励孩子努力上进的话}
g.dispose();
} catch (Exception e) {
e.printStackTrace();
}
return img;
}
public BufferedImage modifyImage(BufferedImage img, Object content, int x, int y, Color color) {
try {
g = ateGraphics();
g.setBackground(Color.WHITE);
g.setColor(color);// 设置字体颜⾊
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// 抗锯齿      if (this.font != null)
g.setFont(this.font);
if (content != null) {
String str = String();
g.drawString(str, x, y);
}
g.dispose();
} catch (Exception e) {
e.printStackTrace();
}
return img;
}
/**
* 将⼀张图⽚画在另⼀张图⽚上
*
* @param addImage 被添加的图⽚
* @param sourceImg 源图
安徽疫情啥时候能结束* @param x
* @param y
* @param width
* @param height
* @return
*/
public BufferedImage modifyImagetogeter(BufferedImage addImage, BufferedImage sourceImg, int x,
int y) {
int width = Width();
int height = Height();
try {
g = ateGraphics();
g.drawImage(addImage, x, y, width, height, null);
g.dispose();
} catch (Exception e) {
e.printStackTrace();
}
return sourceImg;
}
/**
*
* @param img1
* @param img2
* @param title 标题
* @param reservePrice 现价
* @param zkFinalPrice 折扣价
* @return BufferedImage
* @throws IOException
*/
public BufferedImage mergeImage(BufferedImage img1, BufferedImage img2,String title,String reservePrice,String zkFinalPrice)
throws IOException {
Font font = new Font("微软雅⿊", Font.BOLD, 20);
int w1 = Width();
int h1 = Height();
int w2 = Width();
int h2 = Height();
BufferedImage newImage = new BufferedImage(w1, h2 + h1 + h2/2, BufferedImage.TYPE_INT_RGB);// 新的图
Graphics2D graphics = (Graphics2D) Graphics();
graphics.setBackground(Color.WHITE);
graphics.fillRect(0, 0, Width(), Height());
graphics.drawImage(img1, 0, 0, null);
graphics.drawImage(img2, (Width()) / 2 - (w2 / 2), Height() - h2,
null);
graphics.setFont(font);
graphics.setColor(Color.BLACK);
int width = FontMetrics(font).stringWidth(title);
int startY = h1 + 30;
if (width > Width()) {
char[] array = CharArray();
StringBuilder sb = new StringBuilder(array[0]);
for (char c : array) {
sb.append(c);
int newWidth = FontMetrics(font).String());
if ((newWidth + 19) >= Width()) {// 准备换⾏
graphics.String(), 0, startY);
startY += 30;
sb.delete(0, sb.length());
}
}
graphics.String(), 0, startY);
} else {
graphics.drawString(title, 0, startY);
}
graphics.drawString("现价¥"+reservePrice, 0, startY + 30);
startY += 30;
graphics.drawString("卷后价¥"+zkFinalPrice, 0, startY + 30);
return newImage;
}
}
两个帮助类⾥⾯有很多⽅法没⽤到,⼤家只要看⼏个关键的⽅法就可以了,TaoBaoQRCode 对象⾥⾯的属性我就没列出来了,⼤家⾃⼰根据⾃⼰的需求⽽定
以上就是java 实现图⽚合成,并添加⽂字的详细内容,更多关于Java 图⽚合成的资料请关注其它相关⽂章!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。