把多张图合并到一张图上,并加上文字说明.

package  cn.net.ssd;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.*;

//import com.citystroll.PersistentLayer.ValueObject.VOSys.*;
import com.sun.image.codec.jpeg.*;

public class createImg {
 private int name_x;// 文字的x轴坐标
 private int name_y;// 文字的y轴坐标

 private int logo_x;// 小图片的x轴坐标
 private int logo_y;// 小图片的x轴坐标

 public createImg() {
 }
 
   /*
  * 根据提供的文字生成jpg图片
     * @param backJpgPath String 背景图片路径
     * @param Logoimage String 小图片路径
     * @param String s 文字信息
     * @param jpgname String 生成的新图片名称
     */

 public void createJpgByFont(String backJpgPath, String Logoimage, String s,
   String jpgname) {
  try { // 宽度 高度
   //String s = "";
   File fileOne = new File(backJpgPath);
   BufferedImage bimage = ImageIO.read(fileOne);
   
   //加字体到图片上
   Graphics2D g = bimage.createGraphics();
   g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
     RenderingHints.VALUE_ANTIALIAS_ON); // 去除锯齿(当设置的字体过大的时候,会出现锯齿)
   Font font = Font.decode("宋体");
   g.setColor(Color.decode("#FF0000")); // 字的颜色
   g.setFont(font.deriveFont(Float.parseFloat("20")));  
   // 对象并应用新设置字体的大小
   g.drawString(s, this.getName_x(), this.getName_y()); // 在指定坐标除添加文字
   g.dispose();// 释放资源
   
            //图片加在背景图上面
   File filelogo = new File(Logoimage);
   BufferedImage bimageLogo = ImageIO.read(filelogo);
   int width = bimageLogo.getWidth();
   int height = bimageLogo.getHeight();   
   // 从图片中读取RGB
   int[] ImageArrayOne = new int[width * height];
   ImageArrayOne = bimageLogo.getRGB(0, 0, width, height,ImageArrayOne, 0, width);
   bimage.setRGB(this.getLogo_x(), this.getLogo_y(), width, height,
     ImageArrayOne, 0, width);
   
   
   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();
  }
 }

 public static void main(String[] args) {
  createImg c = new createImg();
  c.createImgByData();
  //c.createMulToOneImg();
 }
 
  /*
  * 将多张图和说明生产到一张图上
     */
 public  void createImgByData(){
  createImg c = new createImg();
  try {
   //BufferedImage ImageNew = new BufferedImage(width*2,height,BufferedImage.TYPE_INT_RGB);

   String back = "e://test//jxdt-bak.bmp";
   File fileOne = new File(back);
   
   BufferedImage bimage = ImageIO.read(fileOne);
   int width=bimage.getWidth();
   int height=bimage.getHeight();
   BufferedImage ImageNew = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
   int[] ImageArrayOne = new int[width*height];
      ImageArrayOne = bimage.getRGB(0,0,width,height,ImageArrayOne,0,width);
      ImageNew.setRGB(0,0,width,height,ImageArrayOne,0,width);//设置左半部分的RGB
     
   c.setName_x(330);
   c.setName_y(170);
   c.setLogo_x(330);
   c.setLogo_y(65);
   bimage=c.createBufferedImage(ImageNew, "e://test//a.png", "嘉兴Test");
   
   c.setName_x(200);
   c.setName_y(170);
   c.setLogo_x(200);
   c.setLogo_y(65);
   bimage=c.createBufferedImage(ImageNew, "e://test//c.bmp", "AAAAAAA");   
   
   File outFile = new File("e://test//d.png");
      ImageIO.write(ImageNew, "png", outFile);//写图片
  }catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 private BufferedImage createBufferedImage(BufferedImage bimage,String Logoimage, String sFontImg) {
 try { // 宽度 高度
  //String s = "";
  //File fileOne = new File(backJpgPath);
  //BufferedImage bimage = ImageIO.read(fileOne);
  
  //加字体到图片上
  Graphics2D g = bimage.createGraphics();
  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON); // 去除锯齿(当设置的字体过大的时候,会出现锯齿)
  Font font = Font.decode("宋体");
  g.setColor(Color.decode("#FF0000")); // 字的颜色
  g.setFont(font.deriveFont(Float.parseFloat("16")));  
  // 对象并应用新设置字体的大小
  g.drawString(sFontImg, this.getName_x(), this.getName_y()); // 在指定坐标除添加文字
  g.dispose();// 释放资源
  
        //图片加在背景图上面
  File filelogo = new File(Logoimage);
  BufferedImage bimageLogo = ImageIO.read(filelogo);
  int width = bimageLogo.getWidth();
  int height = bimageLogo.getHeight();   
  // 从图片中读取RGB
  int[] ImageArrayOne = new int[width * height];
  ImageArrayOne = bimageLogo.getRGB(0, 0, width, height,
    ImageArrayOne, 0, width);
  bimage.setRGB(this.getLogo_x(), this.getLogo_y(), width, height,
    ImageArrayOne, 0, width);
 } catch (Exception e) {
  System.out.println("createJpgByFont Failed!");
  e.printStackTrace();
 }
 return bimage;
}
 
 

 public int getLogo_x() {
  return logo_x;
 }

 public int getLogo_y() {
  return logo_y;
 }

 public int getName_x() {
  return name_x;
 }

 public int getName_y() {
  return name_y;
 }

 public void setName_y(int name_y) {
  this.name_y = name_y;
 }

 public void setName_x(int name_x) {
  this.name_x = name_x;
 }

 public void setLogo_y(int logo_y) {
  this.logo_y = logo_y;
 }

 public void setLogo_x(int logo_x) {
  this.logo_x = logo_x;
 }

}

 

进行了修正,主要保证图片不失真。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值