Java将两张照片合成一张util

package com.xxxxxx.util;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;


/**
* @author yjl
* @date 2023/6/28 20:05
*/
public class Pic {

private Font font = new Font("宋体", Font.PLAIN, 12); // 添加字体的属性设置

private Graphics2D g = null;

private int fontsize = 0;

private int x = 0;

private int y = 0;

/**
* 导入本地图片到缓冲区
*/
public BufferedImage loadImageLocal(String imgName) {
try {
return ImageIO.read(new File(imgName));
} catch (IOException e) {
System.out.println(e.getMessage());
}
return null;
}

/**
* 导入网络图片到缓冲区
*/
public BufferedImage loadImageUrl(String imgName) {
try {
URL url = new URL(imgName);
return ImageIO.read(url.openStream());
} catch (IOException e) {
System.out.println(e.getMessage());
}
return null;
}

/**
* 生成新图片到本地
*/
public void writeImageLocal(String newImage, BufferedImage img) {
if (newImage != null && img != null) {
try {
File outputfile = new File(newImage);
ImageIO.write(img, "jpg", outputfile);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

/**
* 设定文字的字体等
*/
public void setFont(String fontStyle, int fontSize) {
this.fontsize = fontSize;
this.font = new Font(fontStyle, Font.PLAIN, fontSize);
}

/**
* 修改图片,返回修改后的图片缓冲区(只输出一行文本)
*/
public BufferedImage modifyImage(BufferedImage img, Object content, int x, int y) {

try {
int w = img.getWidth();
int h = img.getHeight();
g = img.createGraphics();
g.setBackground(Color.WHITE);
g.setColor(Color.orange);//设置字体颜色
if (this.font != null)
g.setFont(this.font);
// 验证输出位置的纵坐标和横坐标
if (x >= h || y >= w) {
this.x = h - this.fontsize + 2;
this.y = w;
} else {
this.x = x;
this.y = y;
}
if (content != null) {
g.drawString(content.toString(), this.x, this.y);
}
g.dispose();
} catch (Exception e) {
System.out.println(e.getMessage());
}

return img;
}

/**
* 修改图片,返回修改后的图片缓冲区(输出多个文本段) xory:true表示将内容在一行中输出;false表示将内容多行输出
*/
public BufferedImage modifyImage(BufferedImage img, Object[] contentArr, int x, int y,
boolean xory) {
try {
int w = img.getWidth();
int h = img.getHeight();
g = img.createGraphics();
g.setBackground(Color.WHITE);
g.setColor(Color.RED);
if (this.font != null)
g.setFont(this.font);
// 验证输出位置的纵坐标和横坐标
if (x >= h || y >= w) {
this.x = h - this.fontsize + 2;
this.y = w;
} else {
this.x = x;
this.y = y;
}
if (contentArr != null) {
int arrlen = contentArr.length;
if (xory) {
for (int i = 0; i < arrlen; i++) {
g.drawString(contentArr[i].toString(), this.x, this.y);
this.x += contentArr[i].toString().length() * this.fontsize / 2 + 5;// 重新计算文本输出位置
}
} else {
for (int i = 0; i < arrlen; i++) {
g.drawString(contentArr[i].toString(), this.x, this.y);
this.y += this.fontsize + 2;// 重新计算文本输出位置
}
}
}
g.dispose();
} catch (Exception e) {
System.out.println(e.getMessage());
}

return img;
}

/**
* 修改图片,返回修改后的图片缓冲区(只输出一行文本)
*
* 时间:2007-10-8
*
* @param img
* @return
*/
public BufferedImage modifyImageYe(BufferedImage img) {

try {
int w = img.getWidth();
int h = img.getHeight();
g = img.createGraphics();
g.setBackground(Color.WHITE);
g.setColor(Color.blue);//设置字体颜色
if (this.font != null)
g.setFont(this.font);
g.drawString("www.hi.baidu.com?xia_mingjian", w - 85, h - 5);
g.dispose();
} catch (Exception e) {
System.out.println(e.getMessage());
}

return img;
}

public BufferedImage modifyImagetogeter(BufferedImage a,BufferedImage b) throws IOException {

byte[] bytes = new byte[0];
// 读取待合并的文件
BufferedImage prosImg;
BufferedImage consImg;
// 图像压缩
prosImg = resize(a, 1000, 1000,true);
consImg = resize(b, 1000, 1000,true);

// 合并后图像
BufferedImage mergeImg = mergeImage(prosImg, consImg, false);

// 压缩 后大概100K-300K
mergeImg = resize(mergeImg, 1000, 1000,false);
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// ImageIO.write(mergeImg, "jpg", baos);
// bytes = baos.toByteArray();
// baos.close();
return mergeImg;

}


/**
* 压缩图片
*/
private static BufferedImage resize(BufferedImage source, int targetW,int targetH,boolean isRotate ) {
int type = source.getType();
// targetW,targetH分别表示目标长和宽
// int type = source.getType() == 0 ?
// BufferedImage.TYPE_INT_ARGB : source
// .getType();

BufferedImage target = null;
int width = source.getWidth();
int height = source.getHeight();
// 图片宽度小于高度 需要 则调整 宽高 值
if (width < height && isRotate) {
width = height;
height = source.getWidth();
}

double sx = (double) targetW / width;
double sy = (double) targetH / height;
// 这里想实现在targetW,targetH范围内实现等比缩放
if (sx > sy) {
sx = sy;
targetW = (int) (sx * source.getWidth());
} else {
sy = sx;
targetH = (int) (sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) {
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(targetW,
targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else {
target = new BufferedImage(targetW, targetH, type);
}
Graphics2D g = target.createGraphics();
// smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}
/**
* 待合并的两张图必须满足这样的前提,如果水平方向合并,则高度必须相等;如果是垂直方向合并,宽度必须相等。
* mergeImage方法不做判断,自己判断。
*
* @param img1
* 待合并的第一张图
* @param img2
* 带合并的第二张图
* @param isHorizontal
* 为true时表示水平方向合并,为false时表示垂直方向合并
* @return 返回合并后的BufferedImage对象
* @throws IOException
*/
private static BufferedImage mergeImage(BufferedImage img1,BufferedImage img2, boolean isHorizontal){
int w1 = img1.getWidth();
int h1 = img1.getHeight();
int w2 = img2.getWidth();
int h2 = img2.getHeight();

// 从图片中读取RGB
int[] ImageArrayOne = new int[w1 * h1];
// 逐行扫描图像中各个像素的RGB到数组中
ImageArrayOne = img1.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1);
int[] ImageArrayTwo = new int[w2 * h2];
ImageArrayTwo = img2.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2);
// 生成新图片
BufferedImage destImage = null;
if (isHorizontal) {
// 水平方向合并
destImage = new BufferedImage(w1 + w2, h1, BufferedImage.TYPE_INT_RGB);
// 设置上半部分或左半部分的RGB
destImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1);
destImage.setRGB(w1, 0, w2, h2, ImageArrayTwo, 0, w2);
} else { // 垂直方向合并
destImage = new BufferedImage(w1, h1 + h2, BufferedImage.TYPE_INT_RGB);
// 设置上半部分或左半部分的RGB
destImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1);
// 设置下半部分的RGB
destImage.setRGB(0, h1, w2, h2, ImageArrayTwo, 0, w2);
}
return destImage;
}

/**
* 在图片中添加条形码
* @param b
* @param d
* @return
*/
public BufferedImage modifyImageTogeterForBarcode(BufferedImage b, BufferedImage d){
try {
int w = b.getWidth();
int h = b.getHeight();

g = d.createGraphics();
g.drawImage(b, 100, 20, w, h, null);
g.dispose();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return d;
}

public static void main(String[] args) throws IOException {

Pic tt = new Pic();

BufferedImage a = tt.loadImageUrl("http://47.101.153.93:9000/mycarservice/2023/07/07/d7abc8e3-7f1f-4817-ab8f-a0c38e0b6d95.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minio%2F20230710%2Fcn%2Fs3%2Faws4_request&X-Amz-Date=20230710T130220Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=002c0eed65c74ace6d8155a5edd8e42590368a18ee7024e79d27d44c78d44581");
BufferedImage b = tt.loadImageUrl("http://47.101.153.93:9000/mycarservice/2023/07/07/49043bc2-585b-43b2-bbb3-1d0df68de4ba.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minio%2F20230710%2Fcn%2Fs3%2Faws4_request&X-Amz-Date=20230710T130220Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=40b4e4d022b2059140d9d54109e56ad1a10ac9678d7f3b34494f96f5608d0ae6");
// 往图片上写文件
System.out.println(a);
System.out.println(b);
// tt.writeImageLocal("E:\\ploanshare\\2\\22.jpg", tt.modifyImage(d, "000000", 90, 90));
System.out.println(tt.modifyImagetogeter(a, b));
tt.writeImageLocal("/Users/yangle/Downloads/test7.jpg", tt.modifyImagetogeter(a, b));
//将多张图片合在一起
System.out.println("success");
}

}



-----------------------------



 

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
* @author Monica
* @date 2023/7/10 19:44
*/
public class IDCardMergerUtil {


/**
* 导入网络图片到缓冲区
*/
public BufferedImage loadImageUrl(String imgName) {
try {
URL url = new URL(imgName);
return ImageIO.read(url.openStream());
} catch (IOException e) {
System.out.println(e.getMessage());
}
return null;
}


public static void main(String[] args) throws Exception {

IDCardMergerUtil tt = new IDCardMergerUtil();

// 读取身份证正反面图片
BufferedImage frontImage = tt.loadImageUrl("http://47.101.153.93:9000/mycarservice/2023/07/10/7febb3c5-9ed0-48c2-95a8-6f23f296ff8f.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minio%2F20230710%2Fcn%2Fs3%2Faws4_request&X-Amz-Date=20230710T125555Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=45a52c08189ff33c76646360664aa0407fd46a06227e27d807380883b1121479");
BufferedImage backImage = tt.loadImageUrl("http://47.101.153.93:9000/mycarservice/2023/07/10/2cb1a764-7c54-4a53-adc0-6a0bc57e932e.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minio%2F20230710%2Fcn%2Fs3%2Faws4_request&X-Amz-Date=20230710T125555Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=7be4736b0f474e19f4b7798677554df2aced52d81dc47731b7cb2e064310650e");

// 创建新的BufferedImage对象,用于存储合成后的图片
BufferedImage mergedImage = new BufferedImage(
frontImage.getWidth() +
backImage.getWidth(),
Math.max(frontImage.getHeight(), backImage.getHeight()),
BufferedImage.TYPE_INT_RGB);

// 将身份证正反面图片绘制到新的BufferedImage对象上
Graphics2D g2d = mergedImage.createGraphics();
g2d.drawImage(frontImage, 0, 0, null);
g2d.drawImage(backImage, frontImage.getWidth(), 0, null);
g2d.dispose();

// 将合成后的图片保存到本地文件
ImageIO.write(mergedImage, "jpg", new File("/Users/yangle/Downloads/test9.jpg"));
}


posted @ 2023-07-11 10:44  风骚羊肉串  阅读(243)  评论(0编辑  收藏  举报