亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

來自 RGB byte[] 數組的 BufferedImage

來自 RGB byte[] 數組的 BufferedImage

子衿沉夜 2024-01-05 16:54:05
我有一個包含 RGB 值的 byte[] 數組。我想創建 BufferedImage 而不需要一一設置像素,因為圖像可能很大。我找到了以下片段:        byte[] frame = ...;        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);        img.setData(Raster.createRaster(img.getSampleModel(), new DataBufferByte(frame, frame.length), new Point() ) );這確實工作得很好,但有一個小問題;-)TYPE_3BYTE_BGR期望字節以相反的順序排列。所以問題是:是否可以以某種方式加載我的數組,而無需實際創建具有預期順序的新字節數組?如果不可能,是否有比 for 循環更好的方法將數據從 RGB 格式復制到 BGR?
查看完整描述

2 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

試試這個代碼


BufferedImage img = create3ByteRGBImage(int width, int height, new int[] {8, 8, 8},

                                     new int[] {0, 1, 2});    


private BufferedImage create3ByteRGBImage(width, height, int[] nBits, int[] bOffs) {

            ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);

            ColorModel colorModel =

                new ComponentColorModel(cs, nBits,

                                        false, false,

                                        Transparency.OPAQUE,

                                        DataBuffer.TYPE_BYTE);

            WritableRaster raster =

                Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,

                                               width, height,

                                               width*3, 3,

                                               bOffs, null);

            return new BufferedImage(colorModel, raster, false, null);

        }


查看完整回答
反對 回復 2024-01-05
?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

前面的答案有效,但可以簡化以更具可讀性:


ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);


ColorModel model = new ComponentColorModel(

    colorSpace, 

    false,

    true, 

    Transparency.OPAQUE, 

    DataBuffer.TYPE_BYTE

);


WritableRaster raster = Raster.createInterleavedRaster(

    DataBuffer.TYPE_BYTE, 

    width, 

    height, 

    3, 

    null

);


BufferedImage image = new BufferedImage(model, raster, true, null);


查看完整回答
反對 回復 2024-01-05
  • 2 回答
  • 0 關注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號