If you have an int array representing individual pixels' RGB and Alpha values, how can I turn this into an image? Thanks in advance.
You might want to use BufferedImage and setRGB method, see the docs and look for following method:
public void setRGB(int startX,
int startY,
int w,
int h,
int[] rgbArray,
int offset,
int scansize)
Related
I have extended ImageIcon to colorize my icons for different purposes as needed.
I've tried quite a lot now, but when resizing the window it needs > 3 sec. to repaint about 60 32x32 Icons.
First off, this only happens if I color them in XOR Mode. If I leave them as they are it's a matter of milliseconds.
I tried converting the images to a better format with GraphicsConfiguaration.createCompatibleImage but that didn't help.
Now a strange thing: if I draw onto the component.getGraphics instead to the Graphics provided in the paintIcon method, it does so lightning fast even in XOR Mode, but only shows it in a short flicker until it gets overdrawn.
I draw them by adding JButtons into a JPanel with a FlowLayout.
Anything else I can try?
Slow:
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
g.setXORMode(color);
g.drawImage(getImage(), x, y, null);
}
Fast but flickering:
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
gr = c.getGraphics();
gr.setXORMode(color);
gr.drawImage(getImage(), x, y, null);
}
Fast but without Xor
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
g.drawImage(getImage(), x, y, null);
}
Edit:
Found a solution now with a combination of a BufferedImage and AlphaComposite:
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
int w = getIconWidth();
int h = getIconHeight();
BufferedImage bimage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D bg2 = bimage.createGraphics();
bg2.setColor(co);
bg2.fillRect(0, 0, w, h);
bg2.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN));
bg2.drawImage(getImage(),0,0,null);
bg2.dispose();
g.drawImage(bimage,0,0,null);
return;
}
Leaving the question open for some hours to see if someone comes up with a better solution though.
Currently I want to get an array with the pixels of an image. I'm using this code now:
int[] pixels;
int width = firstfloorimg.getWidth();
int height = firstfloorimg.getHeight();
firstfloorimg.getRGB(0, 0, width, height, pixels, 0, width);
But than when i want to use the pixels array it gives a NullpointerException. I have used this code before with no errors.
Why does it occure and how do can I make this work?
The Class BufferedImage offers two variants of getRGB() method:
First one, int getRGB(int x, int y) which will return a single pixel as the return type says.
Second one:
int[] getRGB(int startX, int startY, int w, int h,
int[] rgbArray, int offset, int scansize)
Which Returns an array of integer pixels in the default RGB color model. However if your passed rgbArray is null this function will create a new rgbArray inside of it and return it:
public int[] getRGB(int startX, int startY, int w, int h,
int[] rgbArray, int offset, int scansize) {
// other code
if (rgbArray == null) {
rgbArray = new int[offset+h*scansize];
}
// other code
return rgbArray;
}
But again, you will have to assign the returned array to pixels before using it. The array that was created inside the getRGB function can't change the reference of pixels array which was null before passing to this function.
Consider using getPixel(x, y) function over the second one, because, unlike the second one, getPixel(x, y) does not throw away the optimizations made by Java2D. Discussing about it is out of the scope of this question.
Reference:
BufferedImage.getRGB
I was wondering how i could construct an int array in java into a buffered image. I know you can get an int array in java by doing
int[] srcpixels = ((DataBufferInt)in.getRaster().getDataBuffer()).getData();
but i dont know how to do it the other way. I need this to apply a fisheye effect to a buffered image which i found out how to do here http://popscan.blogspot.com/2012/04/fisheye-lens-equation-simple-fisheye.html but it only works with int arrays. Please help, thanks.
Use a WritableRaster:
final int w = bitmap.getWidth();
final int h = bitmap.getHeight();
final WritableRaster wr = bitmap.getData();
int []data = wr.getPixels(0, 0, w, h, data);
// do processing here
wr.setPixels(0, 0, w, h, data);
I have an MxN array of ints representing colors (say RGBA format, but that is easily changeable). I would like to convert them to an MxN Bitmap or something else (such as an OpenGL texture) that I can render to the screen. Is there a fast way to do this? Looping through the array and drawing them to the canvas is far too slow.
Try this, it will give you the bitmap:
// You are using RGBA that's why Config is ARGB.8888
bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
// vector is your int[] of ARGB
bitmap.copyPixelsFromBuffer(IntBuffer.wrap(vector));
Or you can generate IntBuffer from the following native method:
private IntBuffer makeBuffer(int[] src, int n) {
IntBuffer dst = IntBuffer.allocate(n*n);
for (int i = 0; i < n; i++) {
dst.put(src[i]);
}
dst.rewind();
return dst;
}
Why not use Bitmap.setPixel? It's even API level 1:
int[] array = your array of pixels here...
int width = width of "array"...
int height = height of "array"...
// Create bitmap
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// Set the pixels
bitmap.setPixels(array, 0, width, 0, 0, width, height);
You can play with offset/stride/x/y as needed.
No loops. No additional allocations.
Yeah, sounds like you have all the info you need. If M is the width and N is the height, you can create a new bitmap with Bitmap.createBitmap, and you can fill in the ARGB values with the setPixels method which takes an int array.
Bitmap.createBitmap
Bitmap.setPixels
I am beginning in java (I'm learning in microedition) and I got this error: "int cannot be dereferenced" in the following class:
class DCanvas extends Canvas{
public DCanvas(){
}
public void drawString(String str, int x, int y, int r, int g, int b){
g.setColor(r, g, b); //The error is here
g.drawString(str, x, y, 0); //and here
}
public void paint(Graphics g){
g.setColor(100, 100, 220);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
What am I doing wrong here?
Well I came from PHP and ECMAScripts where I was able to pass my function arguments this way so I really don't understand this error.
The g in drawString is the color value you've passed in, not your Graphics reference. So the error is when you're trying to call a method on an int, which you can't do.
// Passing an integer 'g' into the function here |
// V
public void drawString(String str, int x, int y, int r, int g, int b){
// | This 'g' is the integer you passed in
// V
g.setColor(r, g, b);
g.drawString(str, x, y, 0);
}
You are calling the setColor and fillRect methods on g, which is a parameter of type int.
Since int is not a reference type, you cannot call methods on it.
You probably want to add a Graphics parameter to the function.
While g is in the paint-method an object of the class Graphics (that contains methods named setColor, fillRect and also drawString) in the method drawString is g defined as an Integer that conatins the value for the color green. Especially in the line g.setColor(r, g, b); you use g to set a color on it and also as the argument for setting the color. int has no method setColor (that also doesn't make sense), so you get an error. You probably want to get an Graphics-object also in this method. As you extend canvas, you can get a graphics-object by calling getGraphics(), so your example could look like this:
public void drawString(String str, int x, int y, int r, int g, int b){
getGraphics().setColor(r, g, b);
getGraphics().drawString(str, x, y, 0);
}