Java实现生成图片验证码、图片切割、图片合并

Java实现生成图片验证码、图片切割、图片合并

工作时用到的一些图片操作代码,总结分享一下

1.从零到有生成验证码图片

/**
	 * 生成图片验证码
	 * 
	 * @param width
	 * @param heigh
	 * @param color
	 * @param outputFilePath
	 *
	 * @author chunyang
	 * @throws IOException
	 * @throws FileNotFoundException
	 * @time 2019年1月21日 上午10:36:56
	 */
	public static void drawImage(int width, int height, Color color, String outputFilePath)
			throws FileNotFoundException, IOException {
		// 绘制 3原色(红、绿、蓝) 图片缓冲
		BufferedImage imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2d = (Graphics2D) imageBuffer.getGraphics();
		// 设置颜色,填充整张图片( == 图片背景)
		g2d.setColor(color);
		g2d.fillRect(0, 0, width, height);
		// 画边框
		g2d.setColor(Color.RED);
		g2d.drawRect(0, 0, width - 1, height - 1);
		// 图片上写字
		g2d.setColor(Color.BLUE);
		g2d.setStroke(new BasicStroke(3));
		g2d.setFont(new Font(Font.DIALOG, Font.PLAIN, 18));
		g2d.drawString("hello world!", 100, 50);
		ImageIO.write(imageBuffer, "JPEG", new FileOutputStream(new File(outputFilePath)));
	}

main方法测试:

public static void main(String[] args) throws FileNotFoundException, IOException {
		drawImage(300, 300, Color.WHITE, "/Users/yangchun/Downloads/test.png");
	}

输出结果:
在这里插入图片描述

2.化整为零图片切割

/**
	 * 图片等分切割
	 * 
	 * @param widthNum
	 * @param heightNum
	 * @param imagePath
	 * @param outputDirPath
	 * @author chunyang
	 * @throws IOException
	 * @time 2019年1月21日 上午11:35:32
	 */
	public static void splitImage(int widthNum, int heightNum, String imagePath, String outputDirPath)
			throws IOException {
		// 读取图片
		FileInputStream fis = new FileInputStream(Paths.get(imagePath).toFile());
		BufferedImage image = ImageIO.read(fis);

		// 切割后小图大小
		int imgType = image.getType();
		int targetWidth = image.getWidth() / widthNum;
		int targetHeight = image.getHeight() / heightNum;

		// 循环切割
		for (int i = 0; i < widthNum; i++) {
			for (int j = 0; j < heightNum; j++) {
				BufferedImage imgbuf = new BufferedImage(targetWidth, targetHeight, imgType);
				Graphics2D g2d = imgbuf.createGraphics();
				g2d.drawImage(image, 0, 0, targetWidth, targetHeight, 
						targetWidth * i, targetHeight * j, targetWidth * (i + 1), targetHeight * (j + 1), null);
				ImageIO.write(imgbuf, "JPEG", new FileOutputStream(Paths.get(outputDirPath, i + "-" + j +".png").toFile()));
				g2d.dispose();
				imgbuf = null;
			}
		}
	}

3.图片合并

public enum MergeDir {
		columu, row
	};
	
	/**
	 * 横向或纵向拼接需要 高度 或 宽度 一致。
	 * @param pics 图片路径
	 * @param imageType 图片类型(常用 "JPEG")
	 * @param outFileStr 输出文件路径
	 * @param dir 凭借方向
	 * @throws IOException
	 *
	 * @author chunyang2
	 * @time 2019年1月21日 上午10:11:27
	 */
	public static void mergeImage(List<String> pics, String imageType, String outFileStr, MergeDir dir)
			throws IOException {
		if (pics.size() <= 1) {
			return;
		}
		int[][] ImageArrays = new int[pics.size()][];
		int num = 0;
		// 输入图片的长宽
		int width = 0, height = 0;
		for (String picPath : pics) {
			File temp = new File(picPath);
			BufferedImage image = ImageIO.read(temp);
			width = image.getWidth();
			height = image.getHeight();
			// get pic RGB color model
			ImageArrays[num] = image.getRGB(0, 0, width, height, ImageArrays[num], 0, width);
			num++;
		}
		BufferedImage ImageNew;
		if (MergeDir.columu.equals(dir)) {
			// 计算图片高度、宽度, 逐层拼接
			ImageNew = new BufferedImage(width, height * pics.size(), BufferedImage.TYPE_INT_RGB);
			int height_new = 0;
			for (int i = 0; i < pics.size(); i++) {
				ImageNew.setRGB(0, height_new, width, height, ImageArrays[i], 0, width);
				height_new += height;
			}
		} else {
			// 计算图片高度、宽度, 逐层拼接
			ImageNew = new BufferedImage(width * pics.size(), height, BufferedImage.TYPE_INT_RGB);
			int width_new = 0;
			for (int i = 0; i < pics.size(); i++) {
				ImageNew.setRGB(width_new, 0, width, height, ImageArrays[i], 0, width);
				width_new += width;
			}
		}
		// 输出图片
		File outFile = new File(outFileStr);
		ImageIO.write(ImageNew, imageType, outFile);
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值