验证中...
开源中国 2018 年度最后一场技术盛会邀你来约~错过就要等明年啦!点此立即预约
语言: Java
分类: Web开发技术
最后更新于 2018-12-07 13:56
imgUtils.java
原始数据 复制代码
public final class imgUtils {
public imgUtils(){
}
/** *//**
* 把图片印刷到图片上
*
* @param pressImg1 pressImg2 --
* 水印文件
* @param targetImg --
* 目标文件58
* @param x
* --x坐标
* @param y
* --y坐标
*/
public final static void pressImage(String pressImg1,String pressImg2, String targetImg,String outUrl,
int x, int y) {
try {
//目标文件(海报模版)
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
//水印文件1(二维码)
File _filebiao = new File(pressImg1);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.drawImage(src_biao, (wideth - wideth_biao) / 1,
(height - height_biao+250) / 2, wideth_biao, height_biao, null);
//水印文件2(头像)
File _filebiao2 = new File(pressImg2);
Image src_biao2 = ImageIO.read(_filebiao2);
int wideth_biao2 = src_biao2.getWidth(null);
int height_biao2 = src_biao2.getHeight(null);
g.drawImage(src_biao2, (wideth - wideth_biao2+230) / 2,
(height - height_biao2-470) / 5, wideth_biao2, height_biao2, null);
//水印文件结束
g.dispose();
FileOutputStream out = new FileOutputStream(outUrl);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/** *//**
* 打印文字水印图片
*
* @param pressText
* --文字
* @param targetImg --
* 目标图片
* @param fontName --
* 字体名
* @param fontStyle --
* 字体样式
* @param color --
* 字体颜色
* @param fontSize --
* 字体大小
* @param x --
* 偏移量
* @param y
*/
public static void pressText(String pressText, String targetImg,
String fontName, int fontStyle, int color, int fontSize, int x,
int y) {
try {
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
g.setColor(Color.white);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.drawString(pressText, wideth - fontSize - x, height - fontSize/ 2 - y);
// g.drawString(iterator, x, y);
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
/**
* 给图片添加水印
* @param filePath 需要添加水印的图片的路径
* @param markContent 水印的文字
* @param markContentColor 水印文字的颜色
* @param qualNum 图片质量
* @return
*/
public static void mark(String srcImgPath, String outImgPath, String watermarkStr) {
try {
String str = java.net.URLDecoder.decode(watermarkStr , "UTF-8");
// 读取原图片信息
File srcImgFile = new File(srcImgPath);
Image srcImg = ImageIO.read(srcImgFile);
int srcImgWidth = srcImg.getWidth(null);
int srcImgHeight = srcImg.getHeight(null);
// 加水印
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
Font font = new Font("Courier New", Font.PLAIN, 50);
g.setColor(Color.RED);
g.setFont(font);
int x = srcImgWidth - getWatermarkLength(str, g) - 300;
int y = srcImgHeight - 600;
g.drawString(str, x, y);
g.dispose();
// 输出图片
FileOutputStream outImgStream = new FileOutputStream(outImgPath);
ImageIO.write(bufImg, "jpg", outImgStream);
outImgStream.flush();
outImgStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//获取水印文字总长度
public static int getWatermarkLength(String str, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charsWidth(str.toCharArray(), 0,
str.length());
}
/**
* 将头像
* @param fileUrl 头像路径
* @param savePath 保存地址
* @return
*/
public static void saveUrlAs(String fileUrl, String savePath) {
try
{
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
DataInputStream in = new DataInputStream(connection.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(savePath));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
out.close();
in.close();
connection.disconnect();
}
catch (Exception e)
{
System.out.println(e + fileUrl + savePath);
}
}
/**
* 对图片进行缩小
* @param originalImage 原始图片
* @param times 缩小倍数
* @return 缩小后的Image
*/
public static BufferedImage zoomOutImage(BufferedImage originalImage, Integer times){
int width = originalImage.getWidth()/times;
int height = originalImage.getHeight()/times;
BufferedImage newImage = new BufferedImage(width,height,originalImage.getType());
Graphics g = newImage.getGraphics();
g.drawImage(originalImage, 0,0,width,height,null);
g.dispose();
return newImage;
}
/**
* 对图片进行缩小
* @param srcPath 源图片路径(绝对路径)
* @param newPath 新图片路径(绝对路径)
* @param times 缩小倍数
* @return 保存是否成功
*/
public static boolean zoomOutImage(String srcPath,String newPath,Integer times){
BufferedImage bufferedImage = null;
try {
File of = new File(srcPath);
if(of.canRead()){
bufferedImage = ImageIO.read(of);
}
} catch (IOException e) {
//TODO: 打印日志
return false;
}
if(bufferedImage != null){
bufferedImage = zoomOutImage(bufferedImage,times);
try {
//TODO: 这个保存路径需要配置下子好一点
ImageIO.write(bufferedImage, "JPG", new File(newPath)); //保存修改后的图像,全部保存为JPG格式
} catch (IOException e) {
// TODO 打印错误信息
return false;
}
}
return true;
}
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助