积跬步,聚小流------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服务器 目标:免费拥有自己的网站及域名 ...
随机推荐
- Nginx实现负载均衡 + Keepalived实现Nginx的高可用
前言 使用集群是大中型网站解决高并发.海量数据问题的常用手段.当一台服务器的处理能力.存储空间不足时,不要企图去换更强大的服务器,对大型网站而言,不管多么强大的服务器,都满足不了网站持续增长的业务需求 ...
- PyQt5.9 Html与本地代码交互实例
在PyQt5.9中, 应用QWebEngineView和QWebChannel技术, 可以进行HTML与本地代码进行交互. 要点: 创建交互对象, 基于QObject, 定义信息槽 创建QWebCha ...
- B - Spyke Talks
Problem description Polycarpus is the director of a large corporation. There are n secretaries worki ...
- listview 控件
private void Form1_Load(object sender, EventArgs e) { //设置该listview关联的imagelist listView1.LargeImage ...
- Sybase 动态改变存储过程里查询的数据库
declare @sql varchar(500) select @sql='select * from '+@dbName+'..tableName' --此句用于执行拼接好的SQL语句 exec( ...
- [Offer收割]编程练习赛40
不到一个小时AK,虽然是VP的,舒服,第一次.都简单的一比,没什么可说的. 查找三阶幻方 #pragma comment(linker, "/STACK:102400000,10240000 ...
- html让图片居中显示
<div align=center><img............></div>
- javaweb 之 代理模式
一.动态代理 1.1.代理模式 什么是代理模式及其作用 Proxy Pattern(即:代理模式),23种常用的面向对象软件的设计模式之一 代理模式的定义:为其他对象提供一种代理以控制对这个对象的访问 ...
- JDK1.7源码阅读tools包之------ArrayList,LinkedList,HashMap,TreeMap
1.HashMap 特点:基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Has ...
- pwiz, a model generator
文档链接 pwiz is a little script that ships with peewee and is capable of introspecting an existing data ...