前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java:从sRGB字节流(byte[])创建BufferedImage

java:从sRGB字节流(byte[])创建BufferedImage

作者头像
10km
发布2019-05-25 22:35:03
1.2K0
发布2019-05-25 22:35:03
举报
文章被收录于专栏:10km的专栏10km的专栏

版权声明:本文为博主原创文章,转载请注明源地址。 https://cloud.tencent.com/developer/article/1433740

有时候我们需要从字节流byte[]创建一个BufferedImage对象,比如将jni层返回的图像数据转为BufferedImage对象并显示出来。BufferedImage类中的BufferedImage(java.awt.image.ColorModel, WritableRaster, boolean, java.util.Hashtable)构造函数可以满足这个要求。

不过你看到这个构造函数,所要求的参数完全不是byte[],所以需要做一些对象创建的工作才能达到我们的目的。

以RGB格式的图像矩阵数据为例,首先要构造 sRGB标准的ColorModel对象,然后再从存储图像矩阵的字节数组(byte[])构造WritableRaster。做完这两步就可以调用BufferedImage(java.awt.image.ColorModel, WritableRaster, boolean, java.util.Hashtable)构造生成 BufferedImage对象了。

下面是完整的代码:

代码语言:javascript
复制
package test;

import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;

public class RGBtoBufferedImage {
    /**
     * 根据指定的参数创建一个RGB格式的BufferedImage
     * @param matrixRGB RGB格式的图像矩阵
     * @param width 图像宽度
     * @param height 图像高度
     * @return
     * @see DataBufferByte#DataBufferByte(byte[], int)
     * @see ColorSpace#getInstance(int)
     * @see ComponentColorModel#ComponentColorModel(ColorSpace, boolean, boolean, int, int)
     * @see Raster#createInterleavedRaster(DataBuffer, int, int, int, int, int[], java.awt.Point)
     * @see BufferedImage#BufferedImage(java.awt.image.ColorModel, WritableRaster, boolean, java.util.Hashtable)
     */
    public static BufferedImage createRGBImage(byte[] matrixRGB,int width,int height){
        // 检测参数合法性
        if(null==matrixRGB||matrixRGB.length!=width*height*3)
            throw new IllegalArgumentException("invalid image description");
        // 将byte[]转为DataBufferByte用于后续创建BufferedImage对象
        DataBufferByte dataBuffer = new DataBufferByte(matrixRGB, matrixRGB.length);
        // sRGB色彩空间对象
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        int[] nBits = {8, 8, 8};
        int[] bOffs = {0, 1, 2};
        ComponentColorModel colorModel = new ComponentColorModel(cs, nBits, false, false,
                                             Transparency.OPAQUE,
                                             DataBuffer.TYPE_BYTE);        
        WritableRaster raster = Raster.createInterleavedRaster(dataBuffer, width, height, width*3, 3, bOffs, null);
        BufferedImage newImg = new BufferedImage(colorModel,raster,false,null);
       /* try {
            //写入文件测试查看结果
            ImageIO.write(newImg, "bmp", new File(System.getProperty("user.dir"),"test.bmp"));
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        return  newImg;     
    }
}

如果你的图像数据是Gray或ARGB格式的,如何构造一个BufferedImage对象呢?

其实也差不多,

可以参照BufferedImage中构造函数BufferedImage(int width, int height, int imageType)的源码,耐心研究一下就明白了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年07月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档