package com.qrcode.utils;
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.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class Pic {
private Font font = new Font("宋体", Font.BOLD, 40);// 添加字体的属性设置
private Graphics2D g = null;
private int fontsize = 0;
private int x = 0;
private int y = 0;
/**
* 导入本地图片到缓冲区
*/
public BufferedImage loadImageLocal(String imgName) {
Image img = Toolkit.getDefaultToolkit().getImage(imgName);
return toBufferedImage(img);
}
/**
* 导入网络图片到缓冲区
*/
public BufferedImage loadImageUrl(String imgName) {
try {
URL url = new URL(imgName);
return ImageIO.read(url);
} catch (IOException e) {
System.out.println(e.getMessage());
}
return null;
}
/**
* 生成新图片到本地
*/
public void writeImageLocal(String newImage, BufferedImage img) {
if (newImage != null && img != null) {
try {
File outputfile = new File(newImage);
ImageIO.write(img, "jpg", outputfile);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
/**
* 设定文字的字体等
*/
// public void setFont(String fontStyle, int fontSize) {
// this.fontsize = fontSize;
// this.font = new Font(fontStyle, Font.PLAIN, fontSize);
// }
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();
// Determine if the image has transparent pixels; for this method's
// implementation, see e661 Determining If an Image Has Transparent
// Pixels
// boolean hasAlpha = hasAlpha(image);
// Create a buffered image with a format that's compatible with the
// screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
/*
* if (hasAlpha) { transparency = Transparency.BITMASK; }
*/
// Create the buffered image
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;
// int type = BufferedImage.TYPE_3BYTE_BGR;//by wang
/*
* if (hasAlpha) { type = BufferedImage.TYPE_INT_ARGB; }
*/
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 BufferedImage modifyImage(BufferedImage img, Object content1,
int x1, int y1, Object content2, int x2, int y2) {
try {
int w = img.getWidth();
int h = img.getHeight();
// ###初始化
Graphics2D g2 = img.createGraphics();// 小的
g2.setColor(Color.BLACK);
// 普通部分字体
Font font2 = new Font("黑体", Font.PLAIN, 20);// 添加字体的属性设置
g2.setFont(font2);
// ###初始化结束
g = img.createGraphics();// 大的
// g.setBackground(Color.BLUE);
g.setColor(Color.black);// 设置字体颜色
if (this.font != null)
g.setFont(this.font);
// 验证输出位置的纵坐标和横坐标
if (x >= h || y >= w) {
x = h - this.fontsize + 2;
y = w;
}
if (content1 != null) {
g.drawString(content1.toString(), x1, y1);
}
if (content2 != null)
g2.drawString(content2.toString(), x2, y2);
// 底部填写二维码到期时间 和提示
g2.drawString("长按此图识别图中二维码!", 132, 673);
g2.drawString("此二维码有效推广期截至:2016-01-20", 129, 714);
g.dispose();
g2.dispose();
} catch (Exception e) {
System.out.println(e.getMessage());
}
int rgm = img.getRGB(11, 11);
return img;
}
/**
* 修改图片,返回修改后的图片缓冲区(输出多个文本段) xory:true表示将内容在一行中输出;false表示将内容多行输出
*/
public BufferedImage modifyImage(BufferedImage img, Object[] contentArr,
int x, int y, boolean xory) {
try {
int w = img.getWidth();
int h = img.getHeight();
g = img.createGraphics();
g.setBackground(Color.WHITE);
g.setColor(Color.WHITE);
if (this.font != null)
g.setFont(this.font);
// 验证输出位置的纵坐标和横坐标
if (x >= h || y >= w) {
this.x = h - this.fontsize + 2;
this.y = w;
} else {
this.x = x;
this.y = y;
}
if (contentArr != null) {
int arrlen = contentArr.length;
if (xory) {
for (int i = 0; i < arrlen; i++) {
g.drawString(contentArr[i].toString(), this.x, this.y);
this.x += contentArr[i].toString().length()
* this.fontsize / 2 + 5;// 重新计算文本输出位置
}
} else {
for (int i = 0; i < arrlen; i++) {
g.drawString(contentArr[i].toString(), this.x, this.y);
this.y += this.fontsize + 2;// 重新计算文本输出位置
}
}
}
g.dispose();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return img;
}
/**
* 修改图片,返回修改后的图片缓冲区(只输出一行文本)
*
* 时间:2007-10-8
*
* @param img
* @return
*/
public BufferedImage modifyImageYe(BufferedImage img) {
try {
int w = img.getWidth();
int h = img.getHeight();
g = img.createGraphics();
g.setBackground(Color.WHITE);
g.setColor(Color.blue);// 设置字体颜色
if (this.font != null)
g.setFont(this.font);
g.drawString("reyo.cn", w - 85, h - 5);
g.dispose();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return img;
}
public BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d,
int x, int y, int width, int height) {
try {
int w = b.getWidth();
int h = b.getHeight();
if (width != 0)
w = width;
if (height != 0)
h = height;
g = d.createGraphics();
g.drawImage(b, x, y, w, h, null);
g.dispose();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return d;
}
public static void main(String[] args) {
System.out.println(System.currentTimeMillis());
Pic tt = new Pic();
BufferedImage d = tt.loadImageLocal("D:\\bai.png");
BufferedImage b = tt.loadImageLocal("D:\\erweima.jpg");// 二维码
BufferedImage b1 = tt.loadImageLocal("D:\\touxiang.jpg");// 头像
// 往图片上写文件
tt.modifyImage(d, "我是:追寻美", 200, 217, "邀请您来富利网理财分享还能获得奖金,零元创业就这么简单",
50, 295);
// 将多张图片合在一起
tt.modifyImagetogeter(b, d, 100, 300, 430, 358);
// 将多张图片合在一起
tt.writeImageLocal("D:\\ok.jpg",
tt.modifyImagetogeter(b1, d, 230, 53, 0, 130));
System.out.println(System.currentTimeMillis());
System.out.