前段时间被我哥催的,非逼着我给他搞个淘客图片生成机制。网上找的代码利用午休时间改了一下,很多参数我为了图省事,直接用数字代替了。当然可以做的更规范一点,木有那个时间啊!有兴趣自己down下来改吧!
1、解决图片红色蒙层的 Maven依赖包
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
2、代码如下,可以直接用
/**
* @Copyright: Copyright (c) 2010-2100
* @Company:
* @ProjectName: Test
* @Date: 2018年11月22日上午10:22:42
* @Author:
* @Version:
* @Description:
*
*/
package com.test
/**
*@Description:
*@Author: lujq
*@Since:2018年11月22日
*@Version:1.1.0
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class Test {
private BufferedImage image;
private int imageWidth = 500; // 图片的宽度
private int imageHeight = 750; // 图片的高度
// 生成图片文件
@SuppressWarnings("restriction")
public void createImage(String fileLocation) {
BufferedOutputStream bos = null;
if (image != null) {
try {
FileOutputStream fos = new FileOutputStream(fileLocation);
bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
}catch (Exception e) {
e.printStackTrace();
}finally {
if (bos != null) {// 关闭输出流
try {
bos.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public void graphicsGeneration(String name, String id, String classname, String imgurl) {
int H_title = 0; // 头部高度
int H_mainPic = 500; // 轮播广告高度
int H_tip = 20; // 上网提示框高度
int H_btn = 25; // 按钮栏的高度
int tip_2_top = (H_title + H_mainPic);
int btns_2_top = tip_2_top + H_tip + 20;
int btn1_2_top = btns_2_top + 10;
int btn2_2_top = btn1_2_top + H_btn;
int shops_2_top = btn2_2_top + H_btn + 20;
int W_btn = 280; // 按钮栏的宽度
image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
// 设置图片的背景色
Graphics2D main = image.createGraphics();
main.setColor(Color.white);
main.fillRect(0, 0, imageWidth, imageHeight);
// ***********************页面头部
/*
* Graphics title = image.createGraphics(); //设置区域颜色 title.setColor(new
* Color(143, 0, 0)); //填充区域并确定区域大小位置 title.fillRect(0, 0, imageWidth,
* H_title); //设置字体颜色,先设置颜色,再填充内容 title.setColor(Color.white); //设置字体
* Font titleFont = new Font("宋体", Font.BOLD, 14);
* title.setFont(titleFont); title.drawString("my head", 100,
* (H_title)/2+5);
*/
// ***********************插入中间广告图
Graphics mainPic = image.getGraphics();
BufferedImage bimg = null;
try {
Image src=Toolkit.getDefaultToolkit().getImage(imgurl);
//image=toBufferedImage(src);
bimg = toBufferedImage(src);
}catch (Exception e) {
}
if (bimg != null) {
mainPic.drawImage(bimg, 0, H_title, imageWidth, H_mainPic, null);
mainPic.dispose();
}
// ***********************插入二维码
Graphics mainPic2 = image.getGraphics();
BufferedImage bimg2 = null;
try {
bimg2 = javax.imageio.ImageIO.read(new java.io.File("D:\\b.jpeg"));
}catch (Exception e) {
}
if (bimg2 != null) {
mainPic2.drawImage(bimg2, 290, H_title + 510, 200, 200, null);
mainPic2.dispose();
}
// ***********************设置下面的提示框
Graphics2D tip = image.createGraphics();
// 设置区域颜色
tip.setColor(Color.white);
// 填充区域并确定区域大小位置
tip.fillRect(0, tip_2_top, imageWidth, H_tip);
// 设置字体颜色,先设置颜色,再填充内容
tip.setColor(Color.white);
// 设置字体
Font tipFont = new Font("宋体", Font.BOLD, 20);
tip.setFont(tipFont);
// ***********************设置下面的按钮块
// 设置字体颜色,先设置颜色,再填充内容
tip.setColor(Color.black);
String title = "2018红小豆农家自产红豆 小红豆1000g赤小豆五谷杂粮红豆薏米新货";
int length = title.length();
if (length > 12) {
int mod = length % 12;
int n = 0;
if (mod == 0) {
n = length / 12;
}else {
n = length / 12 + 1;
}
for (int i = 0; i < n; i++) {
if (i == n - 1) {
tip.drawString(title.substring(i * 12, length), 20, btns_2_top + (i + 1) * 20);
}else {
tip.drawString(title.substring(i * 12, (i + 1) * 12), 20, btns_2_top + (i + 1) * 20);
}
}
}
Font btnFont0 = new Font("宋体", Font.BOLD, 20);
tip.setColor(new Color(220, 20, 60));
tip.setFont(btnFont0);
tip.drawString("长按识别二维码购买", 290, 730);
// ***********************按钮
Font btnFont = new Font("宋体", Font.BOLD, 14);
Graphics2D btn1 = image.createGraphics();
btn1.setColor(new Color(220, 20, 60));// #29C65A
btn1.fillRect(10, btn1_2_top + 150, 50, H_btn);
btn1.setColor(new Color(220, 20, 60));
btn1.drawRect(10, btn1_2_top + 150, 100, H_btn);
tip.setFont(tipFont);
tip.setColor(Color.black);
tip.drawString("现价:¥ 30.8 ", 16, btn1_2_top + (H_btn / 2) + 5 + 100);
// btn1 文本
btn1.setColor(Color.white);
btn1.setFont(btnFont);
btn1.drawString("券", 20, btn1_2_top + (H_btn / 2) + 5 + 150);
btn1.setColor(new Color(220, 20, 60));
btn1.drawString(" 15 元", 60, btn1_2_top + (H_btn / 2) + 5 + 150);
Font btnFont1 = new Font("宋体", Font.BOLD, 20);
btn1.drawString("券后价:¥ 9.9", 120, btn1_2_top + (H_btn / 2) + 8 + 150);
/*
* Graphics2D btn2 = image.createGraphics(); btn2.setColor(new
* Color(141,120 , 22));//#29C65A btn2.fillRect(10, btn2_2_top, W_btn,
* H_btn); btn2.setColor(Color.BLACK); btn2.drawRect(10, btn2_2_top,
* W_btn, H_btn); //btn2文本 btn2.setColor(Color.white);
* btn2.setFont(btnFont); btn2.drawString("单击我啊", 120,
* btn2_2_top+(H_btn/2)+5);
*/
createImage("c:\\hehe.jpg");
}
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null),
image.getHeight(null), transparency);
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null),
image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
public static void main(String[] args) {
Test cg = new Test();
try {
cg.graphicsGeneration("ewew", "1", "12", "D:\\a.jpeg");
}catch (Exception e) {
e.printStackTrace();
}
}
}