ImageIo類常用方法以及圖片操作
常用方法
-
讀
從圖中可以看到有三個重載的方法,返回的類型都是BufferedImage
2. 實際操作@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(); } } }
結尾
僅作爲練習和學習
參考鏈接:https://blog.csdn.net/tielan/article/details/43760301#commentBox
如有侵權聯繫刪除,謝謝。