天天看點

Java BufferImage圖檔處理(擷取寬高、圖檔截取、轉換灰階圖)

Java BufferImage圖檔處理(擷取寬高、截取、轉換灰階圖)

    • 1. 效果圖
    • 2. 源碼
    • 參考

這篇部落格将介紹如何使用Java讀取圖檔為byte[]數組,或者BufferedImage及互相轉換,并進行了轉換圖檔為灰階圖,截取部分區域等;

1. 效果圖

原始圖如下:

Java BufferImage圖檔處理(擷取寬高、圖檔截取、轉換灰階圖)

截取部分區域(右下角櫻桃)彩色圖 VS 截取部分區域并轉灰階圖:

Java BufferImage圖檔處理(擷取寬高、圖檔截取、轉換灰階圖)

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[] 轉 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 轉 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 圖檔絕對路徑
     * @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 圖檔絕對路徑
     * @param width     要截取的圖檔區域寬度
     * @param height    要截取的圖檔區域高度
     * @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;
    }

    /**
     * 圖檔截取并轉灰階圖
     *
     * @param src    原始圖檔
     * @param width  要截取的圖檔區域寬度
     * @param height 要截取的圖檔區域高度
     * @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);

        // 圖檔轉灰階圖
        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";
        // 讀取圖檔為BufferedImage
        BufferedImage bufferedImage = ImageIO.read(new File(imgPath));

        // 讀取圖檔為灰階圖
        BufferedImage gray = readAsGrayImage(imgPath);

        // 截取圖檔區域RGB
        BufferedImage crop = cropImage(imgPath, 240,180);

        // 截取圖檔區域灰階圖
        BufferedImage cropGray = cropImageToGray(bufferedImage, 240,180);

        // BufferedImage轉byte[]
        byte[] bytes = bufferedImageToBytes(bufferedImage);

        // byte[] 轉 BufferedImage
        BufferedImage bi = bytesToBufferedImage(bytes);
    }
}
           

參考

  • Java 圖像轉灰階圖
  • byte[] 與 BufferedImage互轉