Java BufferImage图片处理(获取宽高、图片截取、转换灰度图)
生活随笔
收集整理的這篇文章主要介紹了
Java BufferImage图片处理(获取宽高、图片截取、转换灰度图)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Java BufferImage圖片處理(獲取寬高、截取、轉(zhuǎn)換灰度圖)
- 1. 效果圖
- 2. 源碼
- 參考
這篇博客將介紹如何使用Java讀取圖片為byte[]數(shù)組,或者BufferedImage及互相轉(zhuǎn)換,并進(jìn)行了轉(zhuǎn)換圖片為灰度圖,截取部分區(qū)域等;
1. 效果圖
原始圖如下:
截取部分區(qū)域(右下角櫻桃)彩色圖 VS 截取部分區(qū)域并轉(zhuǎn)灰度圖:
2. 源碼
package com.ocr.util;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;/**************************************Class Name: ImageUtils*Description: <圖片處理工具類>*@author: Seminar*@since 1.0.0*************************************/
public class ImageUtils {/*** byte[] 轉(zhuǎn) BufferedImage** @param bytes* @return* @throws IOException*/public static BufferedImage bytesToBufferedImage(byte[] bytes) throws IOException {ByteArrayInputStream in = new ByteArrayInputStream(bytes); // 將bytes作為輸入流BufferedImage image = ImageIO.read(in); //將in作為輸入流,讀取圖片存入image中,而這里in可以為ByteArrayInputStream();return image;}/*** BufferedImage 轉(zhuǎn) byte[]** @param image* @return* @throws IOException*/public static byte[] bufferedImageToBytes(BufferedImage image) throws IOException {ByteArrayOutputStream outputStream = new ByteArrayOutputStream();ImageIO.write(image, "jpg", outputStream);return outputStream.toByteArray();}/*** 讀取圖片為灰度圖** @param imagePath 圖片絕對(duì)路徑* @return* @throws IOException*/public static BufferedImage readAsGrayImage(String imagePath) throws IOException {BufferedImage src = ImageIO.read(new File(imagePath)); //BufferedImage 類的圖片資源BufferedImage grayImage = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_GRAY);for (int i = 0; i < src.getWidth(); i++) {for (int j = 0; j < src.getHeight(); j++) {int rgb = grayImage.getRGB(i, j);grayImage.setRGB(i, j, rgb);}}return grayImage;}/*** 讀取圖片后截取** @param imagePath 圖片絕對(duì)路徑* @param width 要截取的圖片區(qū)域?qū)挾? @param height 要截取的圖片區(qū)域高度* @return BufferedImage 截取后的圖片* @throws IOException*/public static BufferedImage cropImage(String imagePath, int width, int height) throws IOException {BufferedImage src = ImageIO.read(new File(imagePath)); //BufferedImage 類的圖片資源BufferedImage crop = src.getSubimage(src.getWidth() - width, src.getHeight() - height, width, height);return crop;}/*** 圖片截取并轉(zhuǎn)灰度圖** @param src 原始圖片* @param width 要截取的圖片區(qū)域?qū)挾? @param height 要截取的圖片區(qū)域高度* @return BufferedImage 截取后的圖片* @throws IOException*/public static BufferedImage cropImageToGray(BufferedImage src, int width, int height) throws IOException {// 圖片截取BufferedImage crop = src.getSubimage(src.getWidth() - width, src.getHeight() - height, width, height);File cropOutputFile = new File("E:\\mat\\mvt\\java-ocr-demo\\images\\crop.jpg");ImageIO.write(crop, "jpg", cropOutputFile);// 圖片轉(zhuǎn)灰度圖BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);for (int i = 0; i < width; i++) {for (int j = 0; j < height; j++) {int rgb = crop.getRGB(i, j);grayImage.setRGB(i, j, rgb);}}// 保存圖片到本地File outputfile = new File("E:\\mat\\mvt\\java-ocr-demo\\images\\crop_gray.jpg");ImageIO.write(grayImage, "jpg", outputfile);return crop;}public static void main(String[] args) throws IOException {String imgPath = "E:\\mat\\mvt\\java-ocr-demo\\images\\yt.jpg";// 讀取圖片為BufferedImageBufferedImage bufferedImage = ImageIO.read(new File(imgPath));// 讀取圖片為灰度圖BufferedImage gray = readAsGrayImage(imgPath);// 截取圖片區(qū)域RGBBufferedImage crop = cropImage(imgPath, 240,180);// 截取圖片區(qū)域灰度圖BufferedImage cropGray = cropImageToGray(bufferedImage, 240,180);// BufferedImage轉(zhuǎn)byte[]byte[] bytes = bufferedImageToBytes(bufferedImage);// byte[] 轉(zhuǎn) BufferedImageBufferedImage bi = bytesToBufferedImage(bytes);}
}
參考
- Java 圖像轉(zhuǎn)灰度圖
- byte[] 與 BufferedImage互轉(zhuǎn)
總結(jié)
以上是生活随笔為你收集整理的Java BufferImage图片处理(获取宽高、图片截取、转换灰度图)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python,OpenCV图像金字塔cv
- 下一篇: Python,OpenCV进行直方图反投