package com.sunyard.mall.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class CodeImage {
private BufferedImage image;
private int imageWidth = 300; //圖片的寬度
private int imageHeight = 500; //圖片的高度
//生成圖片文件
@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();
}
}
}
}
}
//imgurl插入廣告圖片路徑,imgPath最終圖片保存路徑
public void graphicsGeneration(String name, String id, String classname, String imgurl,String imgPath) {
int H_title = 30; //頭部高度
int H_mainPic = 200; //輪播廣告高度
int H_tip = 60; //上網提示框高度
int H_btn = 25; //按鈕欄的高度
int tip_2_top = (H_title+H_mainPic+H_title);
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 {
bimg = javax.imageio.ImageIO.read(new java.io.File(imgurl));
} catch (Exception e) {}
if(bimg!=null){
mainPic.drawImage(bimg, 0, H_title, imageWidth, H_mainPic, null);
mainPic.dispose();
}
//***********************設置下面的提示框
Graphics2D tip = image.createGraphics();
//設置區域顏色
tip.setColor(new Color(255, 120, 89));
//填充區域並確定區域大小位置
tip.fillRect(0, tip_2_top, imageWidth, H_tip);
//設置字體顏色,先設置顏色,再填充內容
tip.setColor(Color.white);
//設置字體
Font tipFont = new Font("宋體", Font.PLAIN, 14);
tip.setFont(tipFont);
tip.drawString("登錄成功,本次認證時間1小時", 60, tip_2_top+(H_tip)/2-10);
tip.drawString("正在返回商家主頁", 100, tip_2_top+(H_tip)/2+10);
//***********************設置下面的按鈕塊
//設置字體顏色,先設置顏色,再填充內容
tip.setColor(Color.black);
tip.drawString("您可以選擇的操作:", 20, btns_2_top);
tip.drawString("下面的小圖標:", 20, shops_2_top);
//***********************按鈕
Font btnFont = new Font("宋體", Font.BOLD, 14);
Graphics2D btn1 = image.createGraphics();
btn1.setColor(new Color(41,192 , 50));//#29C65A
btn1.fillRect(10, btn1_2_top, W_btn, H_btn);
btn1.setColor(Color.BLACK);
btn1.drawRect(10, btn1_2_top, W_btn, H_btn);
//btn1 文本
btn1.setColor(Color.white);
btn1.setFont(btnFont);
btn1.drawString("單擊我啊", 120, btn1_2_top+(H_btn/2)+5);
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(imgPath);
}
public static void main(String[] args) {
CodeImage cg = new CodeImage();
try {
cg.graphicsGeneration("ewew", "2", "12", "C:\\qRcode.jpg","C:\\share.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
}
輸出圖片到頁面
@RequestMapping(value = "/IoReadImage", method = RequestMethod.GET)
public String IoReadImage(HttpServletRequest request,HttpServletResponse response) throws IOException, WriterException {
String qRcodePath="C:\\qRcode.jpg";// 二維碼圖片路徑
String imgPath = "C:\\share.jpg";// 分享圖路徑
//生成海報圖
CodeImage cg = new CodeImage();
try {
cg.graphicsGeneration("ewew", "2", "12", qRcodePath,imgPath);
} catch (Exception e) {
e.printStackTrace();
}
ServletOutputStream out = null;
FileInputStream ips = null;
try {
//獲取圖片存放路徑
ips = new FileInputStream(new File(imgPath));
response.setContentType("multipart/form-data");
out = response.getOutputStream();
//讀取文件流
int len = 0;
byte[] buffer = new byte[1024 * 10];
while ((len = ips.read(buffer)) != -1){
out.write(buffer,0,len);
}
out.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
out.close();
ips.close();
}
File file1 = new File(imgPath);
file1.delete();
File file2 = new File(qRcodePath);
file2.delete();
return null;
}