赞
踩
最近的项目中遇到ImageIO,因此记录下这个类的用法
一、ImageIO:
这个类中的方法都是静态方法,可以用来进行简单的图片IO操作
1、读入的三种方法
public static BufferedImage read(File input)
File file = new File("/Users/xixi/Documents/aaa.png");
BufferedImage bu = ImageIO.read(file);12
public static BufferedImage read(InputStream input)
public static BufferedImage read(ImageInputStream stream)
2、RenderedImage接口的子类是BufferedImage,因此在这里可以直接出传入BufferedImage的实例化对象,将BufferedImage对象直接写出指定输出流
public static boolean write(RenderedImage im,String formatName,File output)
public static boolean write(RenderedImage im, String formatName,OutputStream output)
public static boolean write(RenderedImage im, String formatName,ImageOutputStream output)
public class Test {
public static void main(String[] args) {
File out = new File("/Users/wangjue/DownLoads/1.jpg");
//将图片写入ImageIO流
try {
BufferedImage img = ImageIO.read(out);
//将图片写出到指定位置(复制图片)
OutputStream ops = new FileOutputStream(new File("/Users/wangjue/DownLoads/1(1).jpg"));
ImageIO.write(img, "jpg", ops);
} catch (IOException e) {
e.printStackTrace();
}
}
}
读
从图中可以看到有三个重载的方法,返回的类型都是BufferedImage
实际操作
@Test public void imageIOTest() { String imagePath = "D:\\test4.jpg"; try { File file = new File(imagePath); String encode = URLEncoder.encode(imagePath, "utf-8"); URL url = new URL("file:///" + encode); InputStream fileInputStrem = new FileInputStream(imagePath); ImageInputStream fileImageInputStream = new FileImageInputStream(file); /** * public static BufferedImage read(File input) */ BufferedImage read_1 = ImageIO.read(file); /** * public static BufferedImage read(InputStream input) */ BufferedImage read_2 = ImageIO.read(fileInputStrem); /** * public static BufferedImage read(ImageInputStream stream) */ BufferedImage read_3 = ImageIO.read(fileImageInputStream); /** * public static BufferedImage read(URL) */ BufferedImage read_4 = ImageIO.read(url); Assert.assertEquals(false, read_1 == null); Assert.assertEquals(false, read_2 == null); Assert.assertEquals(false, read_3 == null); Assert.assertEquals(false, read_4 == null); } catch (IOException e) { e.printStackTrace(); } }
写
可以看到常见的写方法如上。
RenderedImage接口的子类是BufferedImage,因此在这里可以直接出传入BufferedImage的实例化对象,将BufferedImage对象直接写出指定输出流
实际操作
/**
*public static boolean write(RenderedImage im,String formatName,File output)
*/
boolean write_1 = ImageIO.write(read_1, "jpg", new File("e:/test.jpg"));
/**
* public static boolean write(RenderedImage im, String formatName,ImageOutputStream output)
*/
boolean write_2 = ImageIO.write(read_2, "jpg", new FileImageOutputStream(new File("e:/test2.jpg")));
/**
* public static boolean write(RenderedImage im, String formatName,OutputStream output)
*/
boolean write_3 = ImageIO.write(read_2, "jpg", new FileOutputStream("e:/test3.jpg"));
Assert.assertEquals(true, write_1);
Assert.assertEquals(true, write_2);
Assert.assertEquals(true, write_3);
这一小节在参考 https://blog.csdn.net/tielan/article/details/43760301#commentBox 文档的基础上实现。详细看跳转查看
将指定颜色变透明 只能保存 png jpg
/** * 将指定颜色变透明 只能保存 png jpg * * @param imageSrc * @param mask * @return */ public static BufferedImage createImageByMaskColorEx( BufferedImage imageSrc, Color mask) { int x, y; x = imageSrc.getWidth(null); y = imageSrc.getHeight(null); BufferedImage imageDes = new BufferedImage(x, y, BufferedImage.TYPE_4BYTE_ABGR); WritableRaster rasterDes = imageDes.getRaster(); int[] des = new int[4]; while (--x >= 0) for (int j = 0; j < y; ++j) { int rgb = imageSrc.getRGB(x, j); int sr, sg, sb; sr = (rgb & 0xFF0000) >> 16; sg = (rgb & 0xFF00) >> 8; sb = rgb & 0xFF; if (sr == mask.getRed() && sg == mask.getGreen() && sb == mask.getBlue()) { des[3] = 0; } else { des[0] = sr; des[1] = sg; des[2] = sb; des[3] = 255; } rasterDes.setPixel(x, j, des); } return imageDes; }
按倍率缩小图片
/** * 按倍率缩小图片 * * @param imageSrc 读取图片路径 * @param imageDest 写入图片路径 * @param widthRatio 宽度缩小比例 * @param heightRatio 高度缩小比例 */ public static void reduceImageByRatio(String imageSrc, String imageDest, int widthRatio, int heightRatio) { FileOutputStream outputStream = null; try { File file = new File(imageSrc); BufferedImage read = ImageIO.read(file); int width = read.getWidth(); int height = read.getHeight(); /** * 根据缩放比较 构建新的BufferImage 对象 */ BufferedImage destBufferImage = new BufferedImage(width / widthRatio, height / heightRatio, BufferedImage.TYPE_INT_RGB); /** * 绘制 缩小 后的图片 */ destBufferImage.getGraphics().drawImage(read, 0, 0, width / widthRatio, height / heightRatio, null); outputStream = new FileOutputStream(imageDest); ImageIO.write(destBufferImage, "jpg", outputStream); } catch (IOException e) { e.printStackTrace(); } }
按比例放大图片
/** * 按比例方法图片 * * @param imageSrc 读取图片路径 * @param imageDest 写入图片路径 * @param widthRatio 宽度放大比例 * @param heigthRatio 高度放大比例 */ public static void enlargementImageByRatio(@NonNull String imageSrc, @NonNull String imageDest, int widthRatio, int heigthRatio) { FileOutputStream outputStream = null; try { //读取图片构建 BufferImage对象 BufferedImage read = ImageIO.read(new File(imageSrc)); int width = read.getWidth(); int height = read.getHeight(); //构建BufferImage对象 BufferedImage newBufferImage = new BufferedImage(width * widthRatio, height * heigthRatio, BufferedImage.TYPE_INT_RGB); //绘制放大后的图片 newBufferImage.getGraphics().drawImage(read, 0, 0, width * widthRatio, height * heigthRatio, null); //写入文件 outputStream = new FileOutputStream(imageDest); ImageIO.write(newBufferImage, "jpg", outputStream); } catch (Exception e) { e.printStackTrace(); } }
指定图形的长和宽
/** * 指定图形的长和宽 * * @param iamgeSrc * @param imageDest * @param width * @param height * @throws IOException */ public static void resizeImage(String iamgeSrc, String imageDest, int width, int height) { FileOutputStream outputStream = null; try { //读入文件 File file = new File(iamgeSrc); // 构造Image对象 BufferedImage src = javax.imageio.ImageIO.read(file); // 放大边长 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //绘制放大后的图片 tag.getGraphics().drawImage(src, 0, 0, width, height, null); outputStream = new FileOutputStream(imageDest); outputStream = new FileOutputStream(imageDest); ImageIO.write(tag, "jpg", outputStream); } catch (Exception e) { e.printStackTrace(); } }
将图片附加到底图的正中央
/** * 将图片附加到底图的正中央 * * @param negativeImagePath 底图路径 * @param additionImagePath 附加图路径 * @param imagePathDest 保存路径 */ public static void mergeBothImageCenter(String negativeImagePath, String additionImagePath, String imagePathDest) { FileOutputStream outputStream = null; try { BufferedImage negativeBufferImge = ImageIO.read(new File(negativeImagePath)); BufferedImage additionBufferImage = ImageIO.read(new File(additionImagePath)); /** *additionImagePath 绘制在 negativeImagePath 上的 中央区域 */ negativeBufferImge.getGraphics().drawImage(additionBufferImage, (negativeBufferImge.getWidth() - additionBufferImage.getWidth()) / 2, (negativeBufferImge.getHeight() - additionBufferImage.getHeight()) / 2, null); outputStream = new FileOutputStream(imagePathDest); /** * 输出到文件 */ ImageIO.write(negativeBufferImge, "jpg", outputStream); } catch (Exception e) { e.printStackTrace(); } }
图片灰化操作
/** * 图片灰化操作 * * @param srcImage 读取图片路径 * @param toPath 写入灰化后的图片路径 */ public static void grayImage(String srcImage, String toPath) { try { BufferedImage src = ImageIO.read(new File(srcImage)); ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); ColorConvertOp op = new ColorConvertOp(cs, null); src = op.filter(src, null); ImageIO.write(src, "jpg", new File(toPath)); } catch (Exception e) { e.printStackTrace(); } }
在源图片上设置水印文字
/** * 在源图片上设置水印文字 * (AlphaComposite 设置透明度) * Graphics2D (绘制图片) * * @param srcImagePath 原图片路径 * @param alpha 透明度(0<alpha<1) * @param rotate 旋转的角度,以弧度为单位 * @param font 字体(例如:宋体) * @param fontStyle 字体格式(例如:普通样式--Font.PLAIN、粗体--Font.BOLD ) * @param fontSize: 字体大小 * @param color 字体颜色(例如:黑色--Color.BLACK) * @param inputWords 输入显示在图片上的文字 * @param x 文字显示起始的x坐标 * @param y 文字显示起始的y坐标 * @param imageFormat 文字显示起始的y坐标 * @param toPath 写入图片路径 */ public static void word2Image(@NonNull String srcImagePath, @NonNull float alpha, @NonNull double rotate, String font, int fontStyle, int fontSize, Color color, String inputWords, int x, int y, String imageFormat, String toPath) { FileOutputStream outputStream = null; try { BufferedImage bufferedImage = ImageIO.read(new File(srcImagePath)); int width = bufferedImage.getWidth(); int height = bufferedImage.getHeight(); /** * 得到绘图对象 Graphics */ Graphics2D graphics = bufferedImage.createGraphics(); /** * 原图像填充 */ graphics.drawImage(bufferedImage, 0, 0, width, height, null, null); /** * 获取透明度对象AlphaComposite * static int SRC 将源色复制到目标色(Porter-Duff Source 规则)。 static int SRC_ATOP 目标色中的源色部分将被合成到目标色中(Porter-Duff Source Atop Destination 规则)。 static int SRC_IN 目标色中的源色部分将替换目标色(Porter-Duff Source In Destination 规则)。 static int SRC_OUT 目标色以外的源色部分将替换目标色(Porter-Duff Source Held Out By Destination 规则)。 static int SRC_OVER 在目标色之上合成源色(Porter-Duff Source Over Destination 规则)。 */ AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha); /** * 设置响应属性 */ graphics.setComposite(alphaComposite); //设置文字字体名称、样式、大小 graphics.setFont(new Font(font, fontStyle, fontSize)); graphics.setColor(color); graphics.drawString(inputWords, x, y);//输入水印文字及其起始x、y坐标 graphics.dispose(); /** * 输出 */ ImageIO.write(bufferedImage, imageFormat, new File(toPath)); } catch (Exception e) { e.printStackTrace(); } }
在源图像上设置图片水印
/** * 在源图像上设置图片水印 * ---- 当alpha==1时文字不透明(和在图片上直接输入文字效果一样) * * @param srcImagePath 源图片路径 * @param appendImagePath 水印图片路径 * @param alpha 透明度 * @param x 水印图片的起始x坐标 * @param y 水印图片的起始y坐标 * @param width 水印图片的宽度 * @param height 水印图片的高度 * @param imageFormat 图像写入图片格式 * @param toPath 图像写入路径 * @throws IOException */ public void alphaImage2Image(String srcImagePath, String appendImagePath, float alpha, int x, int y, int width, int height, String imageFormat, String toPath) throws IOException { FileOutputStream fos = null; try { BufferedImage image = ImageIO.read(new File(srcImagePath)); //创建java2D对象 Graphics2D g2d = image.createGraphics(); //用源图像填充背景 g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null, null); //设置透明度 AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha); g2d.setComposite(ac); //设置水印图片的起始x/y坐标、宽度、高度 BufferedImage appendImage = ImageIO.read(new File(appendImagePath)); g2d.drawImage(appendImage, x, y, width, height, null, null); g2d.dispose(); fos = new FileOutputStream(toPath); ImageIO.write(image, imageFormat, fos); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { fos.close(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。