基于int数组创建WritableRaster [英] Create a WritableRaster based on int array

查看:173
本文介绍了基于int数组创建WritableRaster的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个int数组并将其转换为BufferImage。我真的没有任何关于这个主题的背景知识,我从互联网上学到了所有这些就是我要做的事情:
从BufferedImage创建一个数组(完成),将这个数组转换成IntBuffer(完成) - (稍后我需要通过IntBuffer对图像做一些操作),将IntBuffer中更改的值放入新数组(完成),然后将此数组转换为WritableRaster。
(如果我对这个过程的描述不对,请告诉我)

I need to take an int array and turn it into BufferImage. I really don't have any background on this subject and I learn it all from the internet so here's what I'm trying to do: Create an array from BufferedImage(done), turn this array into IntBuffer(done) - (Later i'll need to do some opertions on the image through the IntBuffer), put the changed values from the IntBuffer in new array(done), and turn this array into WritableRaster. (If something isn't right in my understading of the process please tell me)

以下是我处理WritableRaster的行:

Here's the line where I deal with the WritableRaster:

WritableRaster newRaster= newRaster.setPixels(0, 0, width, height, matrix);

Eclipse将此标记为错误并说''类型不匹配:无法从void转换为WritableRaster

Eclipse marks this as a mistake and says ''Type mismatch:Cannot convert from void to WritableRaster"

请帮助!我有点迷失。

也很抱歉英语不好。

编辑:
矩阵:

The matrix:

         int height=img.getHeight();
         int width=img.getWidth();
         int[]matrix=new int[width*height];

我尝试向Raster插入值的代码部分:

The part of the code where I try to insert values to the Raster:

    BufferedImage finalImg = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
    WritableRaster newRaster= (WritableRaster)finalImg.getData();
    newRaster.setPixels(0, 0, width, height, matrix);

错误消息:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10769
at java.awt.image.SinglePixelPackedSampleModel.setPixels(Unknown Source)
at java.awt.image.WritableRaster.setPixels(Unknown Source)


推荐答案

您可以从<$ c $创建 WritableRaster 和/或 BufferedImage c> int 这样的数组:

You can create a WritableRaster and/or BufferedImage from an int array like this:

int w = 300;
int h = 200;
int[] matrix = new int[w * h];

// ...manipulate the matrix...

DataBufferInt buffer = new DataBufferInt(matrix, matrix.length);

int[] bandMasks = {0xFF0000, 0xFF00, 0xFF, 0xFF000000}; // ARGB (yes, ARGB, as the masks are R, G, B, A always) order
WritableRaster raster = Raster.createPackedRaster(buffer, w, h, w, bandMasks, null);

System.out.println("raster: " + raster);

ColorModel cm = ColorModel.getRGBdefault();
BufferedImage image = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);

System.err.println("image: " + image);

这篇关于基于int数组创建WritableRaster的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆