以下代码可以生成六行两列的图片,代码改成了可以接受参数可循环的。

package com.xgt.util;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream; public class ImgUtil {
private static final Logger logger = LoggerFactory.getLogger(ImgUtil.class);
public static Integer width = 500; public static Integer height = 500; public static void main(String[] args) {
try {
ImgUtil test = new ImgUtil();
// test.ImageTset();
} catch (Exception e) {
System.out.print(e);
}
} /**
* 注释勿删,测试有用
* 生成图片,合成图片
* @param fileDirectory
* @param realnames
* @throws Exception
*/
public static void ImageTset(String fileDirectory,String[] realnames) throws Exception { /*File _file1 = new File("D:\\QRCodePng\\奥特曼.png");
File _file2 = FontImageUtil.createJpgByFontAndAlign("奥特曼", "center", 32, 150, 50, Color.white, Color.black,
new Font(null, Font.BOLD, 32), "D:\\QRCodePng\\奥特曼.jpg");
File _file3 = new File("D:\\QRCodePng\\谷阿莫.png");
File _file4 = FontImageUtil.createJpgByFontAndAlign("谷阿莫", "center", 32, 150, 50, Color.white, Color.black,
new Font(null, Font.BOLD, 32), "D:\\QRCodePng\\谷阿莫.jpg");
File _file5 = new File("D:\\QRCodePng\\怪兽.png");
File _file6 = FontImageUtil.createJpgByFontAndAlign("怪兽", "center", 32, 150, 50, Color.white, Color.black,
new Font(null, Font.BOLD, 32), "D:\\QRCodePng\\怪兽.jpg");
File _file7 = new File("D:\\QRCodePng\\蓝胖.png");
File _file8 = FontImageUtil.createJpgByFontAndAlign("蓝胖", "center", 32, 150, 50, Color.white, Color.black,
new Font(null, Font.BOLD, 32), "D:\\QRCodePng\\蓝胖.jpg");
File _file9 = new File("D:\\QRCodePng\\淑女.png");
File _file10 = FontImageUtil.createJpgByFontAndAlign("淑女", "center", 32, 150, 50, Color.white, Color.black,
new Font(null, Font.BOLD, 32), "D:\\QRCodePng\\淑女.jpg");
File _file11 = new File("D:\\QRCodePng\\小草.png");
File _file12 = FontImageUtil.createJpgByFontAndAlign("小草", "center", 32, 150, 50, Color.white, Color.black,
new Font(null, Font.BOLD, 32), "D:\\QRCodePng\\小草.jpg");
Image src1 = javax.imageio.ImageIO.read(_file1);
Image src2 = javax.imageio.ImageIO.read(_file2);
Image src3 = javax.imageio.ImageIO.read(_file3);
Image src4 = javax.imageio.ImageIO.read(_file4);
Image src5 = javax.imageio.ImageIO.read(_file5);
Image src6 = javax.imageio.ImageIO.read(_file6);
Image src7 = javax.imageio.ImageIO.read(_file7);
Image src8 = javax.imageio.ImageIO.read(_file8);
Image src9 = javax.imageio.ImageIO.read(_file9);
Image src10 = javax.imageio.ImageIO.read(_file10);
Image src11 = javax.imageio.ImageIO.read(_file11);
Image src12 = javax.imageio.ImageIO.read(_file12);
//获取图片的宽度
int width = src1.getWidth(null);
//获取图片的高度
int height = src1.getHeight(null);*/ //构造一个类型为预定义图像类型之一的 BufferedImage。 宽度为第一只的宽度,高度为各个图片高度之和
BufferedImage tag = new BufferedImage(width + width, 4*height, BufferedImage.TYPE_INT_RGB);
//创建输出流
FileOutputStream out = new FileOutputStream("D:\\QRCodePng\\合成图.png");
//绘制合成图像
Graphics g = tag.createGraphics(); /**
* 此方法最多六行两列,6张二维码图和对应的6张文字图
* 如需扩展,要重新调整尺寸
* x=0,2,4,6,8...
* x对4取余为0存在线性关系,y=x/3
* x对4取余不为0存在线性关系,y=(x-2)/3
* x=1,3,5,7,9...
* x对4取余为1存在线性关系,y=(4x+2)/3
* x对4取余不为1存在线性关系,y=x/3
*/
for(int i=0;i<realnames.length*2;i++){
if(i%2==0) {
File _file = new File(fileDirectory,realnames[i/2]+".png");
Image src = javax.imageio.ImageIO.read(_file);
if(i%4==0){
g.drawImage(src, 0, i*height/3, width, height, null);
}else {
g.drawImage(src, width, (i-2)*height/3, width, height, null);
}
}else{
File _file = FontImageUtil.createJpgByFontAndAlign(realnames[(i-1)/2], "center", 32, 150, 50, Color.white, Color.black,
new Font(null, Font.BOLD, 32), fileDirectory+"\\"+realnames[(i-1)/2]+".jpg");
Image src = javax.imageio.ImageIO.read(_file);
if(i%4==1){
g.drawImage(src, 0, (i+2)*height/3, width, height/3, null);
}else {
g.drawImage(src, width, i*height/3, width, height/3, null);
}
}
} /*g.drawImage(src1, 0, 0, width, height, null);
g.drawImage(src2, 0, height, width, height/3, null);
g.drawImage(src3, width, 0, width , height, null);
g.drawImage(src4, width, height, width, height/3, null);
g.drawImage(src5, 0, 4*height/3, width, height, null);
g.drawImage(src6, 0, 7*height/3, width, height/3, null);
g.drawImage(src7, width, 4*height/3, width, height, null);
g.drawImage(src8, width, 7*height/3, width , height/3, null);
g.drawImage(src9, 0, 8*height/3, width, height, null);
g.drawImage(src10, 0, 11*height/3, width, height/3, null);
g.drawImage(src11, width, 8*height/3, width, height, null);
g.drawImage(src12, width, 11*height/3, width, height/3, null);*/
// 释放此图形的上下文以及它使用的所有系统资源。
g.dispose();
//将绘制的图像生成至输出流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
//关闭输出流
out.close();
logger.info("合成图出来了");
} }

工具类FontImageUtil.java

package com.xgt.util;
import com.google.zxing.EncodeHintType;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.xgt.dto.ImgQrcodeDto;
import com.xgt.entity.BuildLabourer;
import com.xgt.service.BuildLabourerService;
import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.javase.QRCode;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.imageio.ImageIO; public class FontImageUtil { private static final Logger logger = LoggerFactory.getLogger(FontImageUtil.class);
public static void main(String[] args) throws Exception {
//createImage("谷阿莫", new Font("微软雅黑", Font.PLAIN, 32), new File("d:/a.png"), 150, 50); createJpgByFontAndAlign("生成图片", "right", , , , Color.white, Color.black,
new Font(null, Font.BOLD, ), "D:/right.jpg");
createJpgByFontAndAlign("生成图片", "center", , , , Color.white, Color.black,
new Font(null, Font.BOLD, ), "D:/center.jpg");
createJpgByFontAndAlign("生成图片", "left", , , , Color.white, Color.black,
new Font(null, Font.BOLD, ), "D:/left.jpg");
}/**
* 根据提供的文字生成jpg图片
*
* @param s
* String 文字
* @param align
* 文字位置(left,right,center)
* @param y
* y坐标
* @param width
* 图片宽度
* @param height
* 图片高度
* @param bgcolor
* Color 背景色
* @param fontcolor
* Color 字色
* @param font
* Font 字体字形字号
* @param jpgname
* String jpg图片名
* @return
*/
public static File createJpgByFontAndAlign(String s, String align, int y, int width, int height,
Color bgcolor, Color fontcolor, Font font, String jpgname) {
try { // 宽度 高度
BufferedImage bimage = new BufferedImage(width,
height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.setColor(bgcolor); // 背景色
g.fillRect(, , width, height); // 画一个矩形
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); // 去除锯齿(当设置的字体过大的时候,会出现锯齿)
g.setColor(fontcolor); // 字的颜色
g.setFont(font); // 字体字形字号 int size = font.getSize(); //文字大小
int x = ;
if(align.equals("left")){
x = ;
} else if(align.equals("right")){
x = width - size * s.length() - ;
} else if(align.equals("center")){
x = (width - size * s.length())/;
}
g.drawString(s, x, y); // 在指定坐标除添加文字
g.dispose();
FileOutputStream out = new FileOutputStream(jpgname); // 指定输出文件
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(50f, true);
encoder.encode(bimage, param); // 存盘
out.flush();
out.close();
} catch (Exception e) {
System.out.println("createJpgByFont Failed!");
e.printStackTrace();
}
return new File(jpgname);
} }

Java绘制图片并进行合成的更多相关文章

  1. Java中图片压缩处理

    原文http://cuisuqiang.iteye.com/blog/2045855 整理文档,搜刮出一个Java做图片压缩的代码,稍微整理精简一下做下分享. 首先,要压缩的图片格式不能说动态图片,你 ...

  2. java Swing 图片缓冲机制

    java Swing 图片缓冲机制: 参考:http://jorneyr.iteye.com/blog/868858#comments package util; import java.awt.ge ...

  3. java实现图片压缩

    java实现图片压缩 package Test; import java.awt.Image; import java.awt.image.BufferedImage; import java.io. ...

  4. UWP&WP8.1 重新绘制图片 WriteableBitmap用法 图片转byte[]数组,byte[]数组转图片

    ---恢复内容开始--- WriteableBitmap 是UWP和WP8.1绘制图片的,重组图片的最重要方法.方法较为简单,方法多样性. 通过查看文档,WriteableBitmap的继承性是    ...

  5. java获取图片原始尺寸

    java获取图片原始尺寸 URL url = null; InputStream is = null; BufferedImage img = null; try { url = new URL(pi ...

  6. html5 绘制图片 drawImage

    要在绘图上下文中绘制图片,可以使用 drawImage 方法.该方法有三种不同的参数: drawImage(image,dx,dy) drawImage(image,dx,dy,dw,dh) d ...

  7. 使用html5 canvas绘制图片

    注意:本文属于<html5 Canvas绘制图形入门详解>系列文章中的一部分.如果你是html5初学者,仅仅阅读本文,可能无法较深入的理解canvas,甚至无法顺畅地通读本文.请点击上述链 ...

  8. Android Canvas使用drawBitmap绘制图片

    1.基本的绘制图片方法 //Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置 drawBitmap(Bitmap bitmap, float left, float top, ...

  9. android中Canvas使用drawBitmap绘制图片

    1.主要的绘制图片方法 //Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置     drawBitmap(Bitmap bitmap, float left, float ...

随机推荐

  1. [LeetCode 题解]:Convert Sorted List to Binary Search Tree

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  2. [Erlang16]为什么要用MFA代替fun()–>end?

    MFA:Module Function Arguments. 首先你要知道Module:Func(Args)和Func(Args)的区别在哪里? 如果对细节感兴趣,可以通过这里了解:http://ww ...

  3. 初探UE4中的Profiling【转】

    http://blog.ch-wind.com/ue4-profiling-preview/ Profililng是成品制作过程中非常重要的一个步骤,通过Profiling才能提高运行效率使得作品达到 ...

  4. 1.css介绍

    CSS介绍 我们为什么需要CSS? 使用css的目的就是让网页具有美观一致的页面,另外一个最重要的原因是内容与格式分离 在没有CSS之前,我们想要修改HTML元素的样式需要为每个HTML元素单独定义样 ...

  5. BZOJ3531-[Sdoi2014]旅行(树剖+线段树动态开点)

    传送门 完了今天才知道原来线段树的动态开点和主席树是不一样的啊 我们先考虑没有宗教信仰的限制,那么就是一个很明显的树剖+线段树,路径查询最大值以及路径和 然后有了宗教信仰的限制该怎么做呢? 先考虑暴力 ...

  6. centos7 docker 安装 zookeeper 3.4.13 单机standalone

    假设一台单机主机的ip分别为: 单机主机:192.168.0.200 1. 查找zookeeper镜像: docker search zookeeper 2. 下载官方zookeeper镜像: doc ...

  7. CentOS加入Windows域

    CentOS加入Windows域,为减少操作已经提前关掉了selinux,防火墙.并且更改了主机名 yum install  nss-pam-ldapd -y 第一步:更改主机名为linux.itxd ...

  8. P4168 [Violet]蒲公英 区间众数

    $ \color{#0066ff}{ 题目描述 }$ 在乡下的小路旁种着许多蒲公英,而我们的问题正是与这些蒲公英有关. 为了简化起见,我们把所有的蒲公英看成一个长度为n的序列 \((a_1,a_2.. ...

  9. 基于windows fiber的协程(coroutine)实现

    一个非常简单,但是实用的协程实现,使用Windows的*Fiber函数族(linux可以稍微改一下用*context函数族). fco.h #ifndef _MSC_VER #error " ...

  10. 大数据-hive安装

    1.下载Hive需要的版本 我们选用的是hive-3.1.0 将下载下来的hive压缩文件放到/opt/workspace/下 2.解压hive-3.1.0.tar.gz文件 [root@master ...