Java BufferImage图片处理(获取宽高、图片截取、转换灰度图)

126 篇文章 7 订阅
8 篇文章 1 订阅

Java BufferImage图片处理(获取宽高、截取、转换灰度图)

这篇博客将介绍如何使用Java读取图片为byte[]数组,或者BufferedImage及互相转换,并进行了转换图片为灰度图,截取部分区域等;

1. 效果图

原始图如下:
在这里插入图片描述

截取部分区域(右下角樱桃)彩色图 VS 截取部分区域并转灰度图:
在这里插入图片描述

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);
    }
}

参考

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序媛一枚~

您的鼓励是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值