积跬步,聚小流------java信息生成图片
需求:
代码编写:
package com.jzba.utils; import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random; import javax.imageio.ImageIO;
import javax.swing.ImageIcon; import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImgUtils { private static int newWidth=0,newHeight=0,newX=0,newY=0; public static void main(String[] args) throws Exception{--1、本地測试
String over="D:/zz/1/over.png";
Color color = new Color(100,100,100);
ImageUtil iu=new ImageUtil();
Map map=iu.getImageSizeByBufferedImage("D:/zz/ceshi.png");--备注:见代码下方
int width=(Integer)map.get("width");
int height=(Integer)map.get("height");
float bili=width/height;
if (width>height) {
newWidth=660;
newHeight=height*660/width;
newX=600;
newY=1160+330-newHeight/2;
}else{
newHeight=660;
newWidth=width*660/height;
newY=1160;
newX=600+330-newWidth/2;
}
//String temp1=createCard("D:/zz/1/background.png","D:/zz/1/ceshi.png","D:/zz/1/code.bmp","D:/zz/1/temp1.png",newX,newY,600,1210,newWidth,newHeight,250,250);
String temp1=createCard("D:/zz/1/background.png","D:/zz/1/ceshi2.jpg","D:/zz/1/code.bmp","D:/zz/1/temp1.png",100,100,600,1210,newWidth,newHeight,250,250);
ImageIcon imgIcon = new ImageIcon(temp1);
Image theImg = imgIcon.getImage();
int w = theImg.getWidth(null);
int h = theImg.getHeight(null);
BufferedImage bufImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// 获取Graphics2D
Graphics2D g = bufImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// 绘图
bufImg = g.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
g.dispose();
g = bufImg.createGraphics();
g.setStroke(new BasicStroke(1));
g.drawImage(theImg, 0, 0, w, h, null);//背景图起始位置
g.setColor(color);
Font nf = loadFont("D:/zz/simhei.ttf",60);
Font nf2 = loadFont("D:/zz/simhei.ttf",50);
g.setFont(nf);
g.drawString("秘 密",2500,1040);//
g.setFont(nf2);
g.drawString("秘 密",1920,1190);//
g.drawString("秘 密", 1920, 1290);//
g.drawString("秘 密", 1920, 1390);//
g.drawString("秘 密", 1920, 1490);//
g.drawString("秘 密 ", 1920, 1590);//
g.drawString("秘 密", 1920, 1700);//
g.drawString("秘 密", 1920, 1800);//
//释放对象
File tofile=new File(over);
ImageIO.write(bufImg, "png", tofile);
g.dispose();
}
/**
*
* 将所要加入的图片整合在一起进行加入
* @param backsPath 背景图片的路径
* @param titleImgPath 缩略图图片的路径
* ...
*
* @return toPath 合成后图片的路径
*
* */ public static String createCard(String backsPath,String titleImgPath,String codeImgPath,String toPath,int x,int y,int newx,int newy,int widthTitle,int heightTitle,int codeWidth,int codeHeight){
try {
InputStream imageBack=new FileInputStream(backsPath);//背景图
InputStream imageTitle=new FileInputStream(titleImgPath);//缩略图图片
InputStream imageCode=new FileInputStream(codeImgPath);//二维码图片
BufferedImage backImg=ImageIO.read(imageBack);
BufferedImage titleImg=ImageIO.read(imageTitle);
BufferedImage codeImg=ImageIO.read(imageCode);
System.out.println("*****Graphics****"+backsPath);
Graphics g=backImg.getGraphics();
System.out.println("*****Graphics****1");
g.drawImage(titleImg,x,y,widthTitle,heightTitle,null);
System.out.println("*****Graphics****2");
g.drawImage(codeImg,newx,newy,codeWidth,codeHeight,null);
System.out.println("*****Graphics****3");
OutputStream outImage=new FileOutputStream(toPath);
System.out.println("*****Graphics****4");
// JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(outImage);--这里存在问题
// System.out.println("*****Graphics****5");--在window下能够。在linux下无法进行
// enc.encode(backImg);
File tofile=new File(toPath);
ImageIO.write(backImg, "png", tofile);
// System.out.println("*****"+backsPath);
imageBack.close();//关闭输出端口
imageTitle.close();
imageCode.close();
outImage.close();
} catch (Exception e) {
e.printStackTrace();
}
return toPath;
}
/**
* 将所要加入的文本信息整合在一起进行加入
*
*
* **/
public static String addCardWord(String filePath,String toPath,int fontsize, Color markContentColor,String fontPath,String code,String type,String artist,
String artname,String description,String material,String artisttype,String recordescription,String releaseDate,int codex,int codey,int typex,int typey,
int artistx,int artisty,int artnamex,int artnamey,int descriptionx,int descriptiony,int materialx,int materialy,int artisttypex,int artisttypey,
int recordescriptionx,int recordescriptiony)throws Exception{
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
int w = theImg.getWidth(null);
int h = theImg.getHeight(null);
BufferedImage bufImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// 获取Graphics2D
Graphics2D g = bufImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// 绘图
bufImg = g.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
g.dispose();
g = bufImg.createGraphics();
g.setStroke(new BasicStroke(1));
g.drawImage(theImg, 0, 0, w, h, null);//背景图起始位置
g.setColor(markContentColor);
Font nf = loadFont(fontPath,fontsize);
g.setFont(nf);
g.drawString(code, codex, codey);
g.drawString(type, typex, typey);
g.drawString(artist, artistx, artisty);
g.drawString(artname, artnamex, artnamey);
g.drawString(description, descriptionx, descriptiony);
g.drawString(material, materialx, materialy);
g.drawString(artisttype, artisttypex, artisttypey);
g.drawString(recordescription, recordescriptionx, recordescriptiony);
//释放对象
File tofile=new File(toPath);
ImageIO.write(bufImg, "png", tofile);
g.dispose();
return toPath;
} /**
* 向图片里面加入图片
* @param backsPath 背景图片路径
* @param upPath 加入图片路径
* @param toPath 生成图片路径
* @param x 所加入图片在背景图片上的横向位移,以左上角为标准
* @param y 所加入图片在背景图片上的竖向位移,以左上角为标准
* @param width 所加入图片的宽度
* @param height 所加入图片的高度
* @return toPath String 返回生成图片的路径
*
* **/
public static String addImg2Img(String backsPath,String upPath,String toPath,int x,int y,int width,int height){
try {
InputStream imagein=new FileInputStream(backsPath);
InputStream imagein2=new FileInputStream(upPath);
BufferedImage image=ImageIO.read(imagein);//读取背景图片
BufferedImage image2=ImageIO.read(imagein2);//读取要加入的图片
Graphics g=image.getGraphics();//将背景图片作为绘图面板
g.drawImage(image2,x,y,width,height,null);//在背景图片的基础上进行绘图,将待加入图片加入
OutputStream outImage=new FileOutputStream(toPath);//打开导出图片路径
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(outImage);
enc.encode(image);//将生成的图片输出
imagein.close();//关闭打开的导入、导出通道
imagein2.close();
outImage.close();
} catch (Exception e) {
e.printStackTrace();
}
return toPath;
}
/**
* 向图片里面加入文字
* @param filePath 背景图片路径
* @param toPath 生成图片路径
* @param markContent 加入的文本内容
* @param fontsize 加入文本的字体大小
* @param markContentColor 加入文本的颜色
* @param fontPath 加入文本的字体
* @param x 所加入图片在背景图片上的横向位移。以左上角为标准
* @param y 所加入图片在背景图片上的竖向位移,以左上角为标准
* @return toPath String 返回生成图片的路径
*
* **/
public static String addWord2Img(String filePath,String toPath, String markContent,int fontsize, Color markContentColor,String fontPath,int x,int y) throws Exception {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
int w = theImg.getWidth(null);
int h = theImg.getHeight(null);
BufferedImage bufImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// 获取Graphics2D
Graphics2D g = bufImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// 绘图
bufImg = g.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
g.dispose();
g = bufImg.createGraphics();
g.setStroke(new BasicStroke(1));
g.drawImage(theImg, 0, 0, w, h, null);//背景图起始位置
g.setColor(markContentColor);//给加入的文本进行颜色设置
Font nf = loadFont(fontPath,fontsize);
g.setFont(nf);//对加入的文本进行字体和字体大小设置
g.drawString(markContent, x, y);//将文本加入到图片指定的位置
//释放对象
File tofile=new File(toPath);//打开导出通道
ImageIO.write(bufImg, "png", tofile);//进行绘图
g.dispose();
return toPath;
}
/**
* 对加入文本的字体和字体大小进行设置
* @param fontFileName 外部字体名
* @param fontsize 加入文本的字体大小
* @param markContentColor 加入文本的颜色
* @return dynamicFontPt Font 对文本的对应设置
*
* **/
public static Font loadFont(String fontFileName, float fontSize) throws Exception //第一个參数是外部字体名,第二个是字体大小
{
File file = new File(fontFileName);
FileInputStream aixing = new FileInputStream(file);
Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
Font dynamicFontPt = dynamicFont.deriveFont(fontSize);
aixing.close();
return dynamicFontPt;
} }
备注:在代码中调用到了上一篇文章中获取图片尺寸的对应类。用在这里的目的是。针对不同的作品,可能是横向图片。也可能是竖条图片,为了保证图片不会变形失真。则将图片显示在固定的一个区域内,像这里:
积跬步,聚小流------java信息生成图片的更多相关文章
- 积跬步,聚小流------java获取图片的尺寸
在一篇文章中获取到通过例如以下两种方式进行获取: 1.使用ImageReader进行获取: 2.使用BufferedImage进行获取: 而且经过验证ImageReader进行操作的耗时远远低于Buf ...
- 积跬步,聚小流------关于UML类图
UML的存在 类图是使用频率比較高的UML图,它用于描写叙述系统中所含的类以及它们之间的相互关系,帮助人们简化对系统的理解,也是系统分析和设计阶段的重要产物,也是系统编码和測试的重要类型根据. UML ...
- 积跬步,聚小流------Bootstrap学习记录(3)
响应式作为Bootstrap的一大特色.栅格系统可谓是功不可没,既然如此,那我们就来看一下栅格系统是怎样帮助bootstrap实现响应式布局的呢? 1.什么是栅格系统 我们能够从Bootstrap的官 ...
- 积跬步,聚小流------Bootstrap学习记录(2)
现阶段开启每一次新的征程,已然离不开"Hello World"的习惯仪式.这次自然也不例外.先来看下给出的官网给出的演示样例: 1.bootstrap官网提供的html基本模板代码 ...
- 积跬步,聚小流-------js实现placeholder的效果
前几天在"技术问答"上问了问题,然后有回复一句话就给概括了:placeholder的效果,不得不说.了解的多了才干说起来言简意赅,用最简单的语言描写叙述最清晰的表达. 可是plac ...
- 积跬步,聚小流------ps有用小技巧,改变png图标颜色
* 实现效果: 原图: 改动后: * 实现目的: 满足为实现不同界面色彩搭配改动png图标的颜色 * 实现方法: 1.打开Photoshop工具,导入须要进行改动的png图标: 2.对导入的图 ...
- 积跬步,聚小流------界面经常使用的jeecms标签
* JEECMS初印象 第一次接触JEECMS,突然脑海就浮现了一句话"20元建站,立等可取",原来这都是真的... * JEECMS的界面经常使用标签 临时忽略掉环境搭建.栏目配 ...
- 积跬步,聚小流------Bootstrap学习记录(1)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- qingshow “不积跬步无以至千里,不积小流无以成江海”。--荀子《劝学篇》 用tomcat+花生壳搭建自己的web服务器+域名(参考)
链接地址:http://www.blogjava.net/qingshow/archive/2010/01/17/309846.html 用tomcat搭建web服务器 目标:免费拥有自己的网站及域名 ...
随机推荐
- 如何卸载Win10自带Xbox、人脉、天气等应用?
Win10中的Modern应用一般可在右键菜单中进行卸载,不过某些自带的Modern应用,比如Xbox.天气.人脉.照片等应用是无法在右键菜单中进行卸载的.如果你有卸载这些应用的需求,可参考下面的方法 ...
- NOIP2013T1 转圈游戏 快速幂
描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置, --, 依此 ...
- vue-cli3+typescript+router
vue基于类的写法,和基于对象的写法并不一致.使用vue-cli3创建的项目,src目录下的文件结构并没有多大区别,store.router.app.view.components.aeests该有的 ...
- sql多表关联
inner join(等值连接) 只返回两个表中联结字段相等的行 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有 ...
- 杭电1003 Max Sum 【连续子序列求最大和】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目意思: 即给出一串数据,求连续的子序列的最大和 解题思路: 因为我们很容易想到用一个max ...
- Swift - what's the difference between metatype .Type and .self?
Declaration typealias AnyClass = AnyObject.Type .Type The metatype of a class, structure, or enumera ...
- ajax发送请求是图标转圈圈实现
css部分 .load-img{ //控制图标大小width:40px;height:40px;margin:100px;border-radius:50%;-webkit-animation:cir ...
- eclipse中的maven项目部署到tomcat中
http://www.cnblogs.com/guodefu909/p/4874549.html
- node——通过express模拟Apache实现静态资源托管
1.express中处理静态资源的函数 创建一个app.js作为入口文件,创建一个public文件夹作为静态资源文件夹 var app=express();var fn=express.static( ...
- 解决python执行cmd命令出现"XXX不是内部命令"的问题
由于最近呢,比较得空.无意中浏览到新闻XXX事件(你懂的).后面直接去百度云搜索下载,果然有资源. 怀着迫不及待的心情去打开,解压.我艹,竟然有解压密码.万恶的资本主义啊.作为程序员的我,怎么可能为这 ...