用java 对图片的截图、拼接、合成处理、添加文字


import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

/**
 * @Description:图片处理工具
 * @author:liuyc
 * @time:2016年5月27日 上午10:18:00
 */
public class ImageHandleHelper {

	/**
	 * @Description:截图
	 * @author:liuyc
	 * @time:2016年5月27日 上午10:18:23
	 * @param srcFile源图片、targetFile截好后图片全名、startAcross 开始截取位置横坐标、StartEndlong开始截图位置纵坐标、width截取的长,hight截取的高
	 */
	public static void cutImage(String srcFile, String targetFile, int startAcross, int StartEndlong, int width,
			int hight) throws Exception {
		// 取得图片读入器
		Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");
		ImageReader reader = readers.next();
		// 取得图片读入流
		InputStream source = new FileInputStream(srcFile);
		ImageInputStream iis = ImageIO.createImageInputStream(source);
		reader.setInput(iis, true);
		// 图片参数对象
		ImageReadParam param = reader.getDefaultReadParam();
		Rectangle rect = new Rectangle(startAcross, StartEndlong, width, hight);
		param.setSourceRegion(rect);
		BufferedImage bi = reader.read(0, param);
		ImageIO.write(bi, targetFile.split("\\.")[1], new File(targetFile));
	}

	/**
	 * @Description:图片拼接 (注意:必须两张图片长宽一致哦)
	 * @author:liuyc
	 * @time:2016年5月27日 下午5:52:24
	 * @param files 要拼接的文件列表
	 * @param type1  横向拼接, 2 纵向拼接
	 */
	public static void mergeImage(String[] files, int type, String targetFile) {
		int len = files.length;
		if (len < 1) {
			throw new RuntimeException("图片数量小于1");
		}
		File[] src = new File[len];
		BufferedImage[] images = new BufferedImage[len];
		int[][] ImageArrays = new int[len][];
		for (int i = 0; i < len; i++) {
			try {
				src[i] = new File(files[i]);
				images[i] = ImageIO.read(src[i]);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
			int width = images[i].getWidth();
			int height = images[i].getHeight();
			ImageArrays[i] = new int[width * height];
			ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
		}
		int newHeight = 0;
		int newWidth = 0;
		for (int i = 0; i < images.length; i++) {
			// 横向
			if (type == 1) {
				newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();
				newWidth += images[i].getWidth();
			} else if (type == 2) {// 纵向
				newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();
				newHeight += images[i].getHeight();
			}
		}
		if (type == 1 && newWidth < 1) {
			return;
		}
		if (type == 2 && newHeight < 1) {
			return;
		}

		// 生成新图片
		try {
			BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
			int height_i = 0;
			int width_i = 0;
			for (int i = 0; i < images.length; i++) {
				if (type == 1) {
					ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0,
							images[i].getWidth());
					width_i += images[i].getWidth();
				} else if (type == 2) {
					ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth);
					height_i += images[i].getHeight();
				}
			}
			//输出想要的图片
			ImageIO.write(ImageNew, targetFile.split("\\.")[1], new File(targetFile));

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * @Description:小图片贴到大图片形成一张图(合成)
	 * @author:liuyc
	 * @time:2016年5月27日 下午5:51:20
	 */
	public static final void overlapImage(String bigPath, String smallPath, String outFile) {
		try {
			BufferedImage big = ImageIO.read(new File(bigPath));
			BufferedImage small = ImageIO.read(new File(smallPath));
			Graphics2D g = big.createGraphics();
			int x = (big.getWidth() - small.getWidth()) / 2;
			int y = (big.getHeight() - small.getHeight()) / 2;
			g.drawImage(small, x, y, small.getWidth(), small.getHeight(), null);
			g.dispose();
			ImageIO.write(big, outFile.split("\\.")[1], new File(outFile));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
<span style="white-space:pre">	/**
<span style="white-space:pre">	</span> * @Description:给图片添加文字信息
<span style="white-space:pre">	</span> * @author:liuyc
<span style="white-space:pre">	</span> * @time:2016年5月31日 上午10:23:36
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static void drawStringForImage(String filePath, String content, Color contentColor, float qualNum,String targetFile) {
<span style="white-space:pre">		</span>ImageIcon imgIcon = new ImageIcon(filePath);
<span style="white-space:pre">		</span>Image theImg = imgIcon.getImage();
<span style="white-space:pre">		</span>int width = theImg.getWidth(null) == -1 ? 200 : theImg.getWidth(null);
<span style="white-space:pre">		</span>int height = theImg.getHeight(null) == -1 ? 200 : theImg.getHeight(null);
<span style="white-space:pre">		</span>BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
<span style="white-space:pre">		</span>Graphics2D g = bimage.createGraphics();
<span style="white-space:pre">		</span>g.setColor(contentColor);
<span style="white-space:pre">		</span>g.setBackground(Color.red);
<span style="white-space:pre">		</span>g.drawImage(theImg, 0, 0, null);
<span style="white-space:pre">		</span>// 设置字体、字型、字号
<span style="white-space:pre">		</span>g.setFont(new Font(null, Font.BOLD, 13));
<span style="white-space:pre">		</span>// 写入文字
<span style="white-space:pre">		</span>g.drawString(content, 10, height / 2); 
<span style="white-space:pre">		</span>g.dispose();
<span style="white-space:pre">		</span>FileOutputStream out =null;
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>out = new FileOutputStream(targetFile);
<span style="white-space:pre">			</span>JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
<span style="white-space:pre">			</span>JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
<span style="white-space:pre">			</span>param.setQuality(qualNum, true);
<span style="white-space:pre">			</span>encoder.encode(bimage, param);
<span style="white-space:pre">			</span>out.flush();
<span style="white-space:pre">		</span>} catch (Exception e) {
<span style="white-space:pre">			</span>throw new RuntimeException(e);
<span style="white-space:pre">		</span>}finally {
<span style="white-space:pre">			</span>if (out != null) {
<span style="white-space:pre">				</span>try {
<span style="white-space:pre">					</span>out.close();
<span style="white-space:pre">				</span>} catch (IOException e) {
<span style="white-space:pre">					</span>out = null;
<span style="white-space:pre">					</span>throw new RuntimeException(e);
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}</span>

}


  • 1
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用Java的Graphics2D类来实现多张图片拼接。以下是一个示例代码,可以将多张图片按照指定的行数和列数排列并拼接成一张图片: ```java import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageConcatenator { public static void main(String[] args) throws IOException { // 设置行数和列数 int rows = 3; int cols = 3; // 读取图片 BufferedImage[] images = new BufferedImage[rows * cols]; for (int i = 0; i < rows * cols; i++) { images[i] = ImageIO.read(new File("image" + (i + 1) + ".jpg")); } // 计算拼接图片的大小 int width = 0; int height = 0; for (int i = 0; i < rows * cols; i++) { width += images[i].getWidth(); height += images[i].getHeight(); } // 创建拼接后的图片 BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 绘制每张图片 Graphics2D g2 = result.createGraphics(); int x = 0; int y = 0; for (int i = 0; i < rows * cols; i++) { g2.drawImage(images[i], x, y, null); x += images[i].getWidth(); if (i % cols == cols - 1) { x = 0; y += images[i].getHeight(); } } g2.dispose(); // 保存拼接后的图片 ImageIO.write(result, "jpg", new File("result.jpg")); } } ``` 在上面的代码中,我们首先设置了行数和列数,然后读取了每张图片并存储在一个数组中。接着,我们计算了拼接图片的大小,并创建了一个空白的拼接后的图片。最后,我们使用Graphics2D类的drawImage方法绘制每张图片,并将它们拼接成一张图片。最后,我们将拼接后的图片保存到文件中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值