纯java处理图片拼接(背景上边添加文字和图片)

好消息,百度网盘专业搜索网站上线了
打开瞧一瞧:[url]http://bitar.cn[/url]

package com.pic;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;


/**
* 纯java处理图片拼接(背景上边添加文字和图片)
* @author Administrator
*
*/
public class JoinMutiImage {
private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) throws IOException {
JoinMutiImage.makePicture();
}
private static void drawImage(int x,int y,BufferedImage ImageNew,BufferedImage ImageOne) throws IOException{
int width = ImageOne.getWidth();//图片宽度
int height = ImageOne.getHeight();//图片高度
int[] ImageArrayTop = new int[width*height];
ImageArrayTop = ImageOne.getRGB(0,0,width,height,ImageArrayTop,0,width);
ImageNew.setRGB(x,y,width,height,ImageArrayTop,0,width);//设置头部的RGB
}

public static byte[] makePicture() throws IOException{

// String imagePath = ImageUtil.class.getResource("/").getPath();
// System.out.println(imagePath);
// int idx1 = imagePath.indexOf("WEB-INF");
// imagePath = imagePath.substring(0, idx1).concat("images/shopping_basket/");

String imagePath = "";

File image_1_top = new File(imagePath.concat("food-cart---save---product_01.png"));
File image_2_food = new File(imagePath.concat("food-cart---save---product_01-02.png"));

File image_3_shadow = new File(imagePath.concat("food-cart---save---product_01-03.png"));
File image_4_space = new File(imagePath.concat("food-cart---save---product_05.png"));
File image_5_code = new File(imagePath.concat("food-cart---save---product_06.png"));


BufferedImage bf_image_1_top =ImageIO.read(image_1_top);
BufferedImage bf_image_2_food =ImageIO.read(image_2_food);
BufferedImage bf_image_3_shadow =ImageIO.read(image_3_shadow);
BufferedImage bf_image_4_space =ImageIO.read(image_4_space);
BufferedImage bf_image_5_code =ImageIO.read(image_5_code);

int width = bf_image_1_top.getWidth();

int heightTotal = 1+1+1;//根据多个内容求和算总高度

BufferedImage ImageNew = new BufferedImage(width,heightTotal,BufferedImage.TYPE_INT_RGB);

int currentY = 0;
//第一个图拼接
drawImage(0,0,ImageNew,bf_image_1_top);
currentY+=bf_image_1_top.getHeight();

//第二个图拼接
// for (int i = 0; i < 集合.size(); i++) {
// drawProduct(0,currentY,ImageNew,bf_image_2_food,foods.get(i));
// currentY+=bf_image_2_food.getHeight();
// }


Graphics g = ImageNew.getGraphics();
Font f2 = new Font("",Font.BOLD,12);
g.setFont(f2);
g.drawString(format.format(new Date()),width-130,heightTotal-30);
g.dispose();

//File outFile = new File(outFilePath);
//ImageIO.write(ImageNew, "png", outFile);//写图片
//
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
ImageIO.write(ImageNew, "JPEG", jpegOutputStream);
byte[] captchaChallengeAsJpeg = null;
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
jpegOutputStream.close();

//InputStream is = new ByteArrayInputStream(captchaChallengeAsJpeg);
//return is;

return captchaChallengeAsJpeg;
//

}

// private static void drawTop(int x,int y,BufferedImage ImageNew,BufferedImage ImageOne) throws IOException{
// int width = ImageOne.getWidth();//图片宽度
// int height = ImageOne.getHeight();//图片高度
// int[] ImageArrayTop = new int[width*height];
// ImageArrayTop = ImageOne.getRGB(0,0,width,height,ImageArrayTop,0,width);
// ImageNew.setRGB(x,y,width,height,ImageArrayTop,0,width);//设置头部的RGB
// }

private static void drawProduct(int x,int y,BufferedImage ImageNew,BufferedImage ImageTwo) throws IOException{
int widthTwo = ImageTwo.getWidth();//图片高度
int heightTwo = ImageTwo.getHeight();//图片高度
int[] ImageArrayTwo = new int[widthTwo*heightTwo];
ImageArrayTwo = ImageTwo.getRGB(0,0,widthTwo,heightTwo,ImageArrayTwo,0,widthTwo);
ImageNew.setRGB(x,y,widthTwo,heightTwo,ImageArrayTwo,0,widthTwo);


URL url = new URL("图片");
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();

BufferedImage ImagePuduct = ImageIO.read(inStream);
int widthPuduct = ImagePuduct.getWidth();
int heightPuduct = ImagePuduct.getHeight();//图片高度

Image image=ImagePuduct.getScaledInstance(100,80,Image.SCALE_DEFAULT);//获取缩略图
/*再创建一个BufferedImage对象 用于创建100*100大小的图像*/
BufferedImage oimage;
oimage = new BufferedImage(100,80,Image.SCALE_DEFAULT);
/*获取图像上下文对象,然后把刚才的Image对象画到BufferedImage中去
切忌, drawImage()方法有很多重载方法,一定要选用下面的这个,它会95%的复制原图的图片质量。其他重载方法你也可以试试,可能生成出来的图片很丑噢~哈哈
*/
oimage.getGraphics().drawImage(image,0,0, null);

widthPuduct = 100;
heightPuduct = 80;
int[] ImageArrayPuduct = new int[widthPuduct*heightPuduct];
ImageArrayPuduct = oimage.getRGB(0,0,widthPuduct,heightPuduct,ImageArrayPuduct,0,widthPuduct);
ImageNew.setRGB(60,y+20,widthPuduct,heightPuduct,ImageArrayPuduct,0,widthPuduct);

drawProductText(x,y,widthPuduct,heightPuduct,ImageNew);
}

private static void drawProductText(int x,int y,int widthProduct,int heightProduct,BufferedImage ImageNew){
Graphics g = ImageNew.getGraphics();
Font f = new Font("",Font.BOLD,24);
g.setColor(Color.decode("#333"));
g.setFont(f);
g.drawString("内容",x+60+widthProduct+20,y+40);


f = new Font("",Font.BOLD,18);
g.setFont(f);
g.setColor(Color.decode("#F4CAC9"));
g.drawString("内容",60+widthProduct+59,y+20+20+32);


//
g.dispose();
}
/*
private static void drawBottom(int x,int y,BufferedImage ImageNew,BufferedImage ImageBottom) throws IOException{
int width = ImageBottom.getWidth();//图片高度
int heightBottom = ImageBottom.getHeight();//图片高度
int[] ImageArrayBottom = new int[width*heightBottom];
// Graphics g = ImageNew.getGraphics();
// int xx =10;
// int yy =10;
// Font f2 = new Font("",Font.BOLD,12);
// g.setFont(f2);
// g.drawString(format.format(new Date()),xx,yy);
// g.dispose();
ImageArrayBottom = ImageBottom.getRGB(0,0,width,heightBottom,ImageArrayBottom,0,width);
ImageNew.setRGB(x,y,width,heightBottom,ImageArrayBottom,0,width);
}
*/
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hvang1988

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值