Creating BufferedImage with width, height and bit depth - java

I would like to create a BufferedImage with certin width, height (800x600) and bit depth of 24 and I would like image background to be transparent.
How do I do that?
Thanking you.

You use the constructor and pass a suitable format parameter:
new BufferedImage(w, h, BufferedImage.TYPE_?);
where TYPE_? is on of the constants that define a format with alpha channel, like TYPE_INT_ARGB.
Edit: Straight from BufferedImage
/**
* Represents an image with 8-bit RGBA color components packed into
* integer pixels. The image has a <code>DirectColorModel</code>
* with alpha. The color data in this image is considered not to be
* premultiplied with alpha. When this type is used as the
* <code>imageType</code> argument to a <code>BufferedImage</code>
* constructor, the created image is consistent with images
* created in the JDK1.1 and earlier releases.
*/
public static final int TYPE_INT_ARGB = 2;

These are the options you have with BufferedImage:
BufferedImage(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable<?,?> properties)
BufferedImage(int width, int height, int imageType)
BufferedImage(int width, int height, int imageType, IndexColorModel cm)

Related

Scaling BufferedImage and writing to file [duplicate]

I have viewed this question, but it does not seem to actually answer the question that I have. I have a, image file, that may be any resolution. I need to load that image into a BufferedImage Object at a specific resolution (say, for this example, 800x800). I know the Image class can use getScaledInstance() to scale the image to a new size, but I then cannot figure out how to get it back to a BufferedImage. Is there a simple way to scale a Buffered Image to a specific size?
NOTE I I do not want to scale the image by a specific factor, I want to take an image and make is a specific size.
Something like this? :
/**
* Resizes an image using a Graphics2D object backed by a BufferedImage.
* #param srcImg - source image to scale
* #param w - desired width
* #param h - desired height
* #return - the new resized image
*/
private BufferedImage getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}
You can create a new BufferedImage of the size you want and then perform a scaled paint of the original image into the new one:
BufferedImage resizedImage = new BufferedImage(new_width, new_height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(image, 0, 0, new_width, new_height, null);
g.dispose();
see this website Link1
Or
This Link2

Apply tint to grayscale png image with transparency in Java

I need to apply a tint to a grayscale BufferedImage with transparency without influencing the transparency itself, this is the method i use, but when i try to draw it on top of another image inside a canvas, the transparency of the original image is gone, leaving a square of the raw selected color...
private BufferedImage colorize(BufferedImage original,Color tint) { //TODO fix!!!
BufferedImage img = new BufferedImage(original.getWidth(), original.getHeight(),BufferedImage.TYPE_4BYTE_ABGR);
int redVal=tint.getRed();
int greenVal=tint.getGreen();
int blueVal=tint.getBlue();
for (int x=0;x<original.getWidth();x++) for (int y=0;y<original.getHeight();y++) {
Color pixelVal=new Color(original.getRGB(x, y));
int grayValue=pixelVal.getRed(); //Any basic color works the same
int alpha=pixelVal.getAlpha();
int newRed= (redVal*(grayValue))/255;
int newGreen= (greenVal*grayValue)/255;
int newBlue= (blueVal*grayValue)/255;
img.setRGB(x, y, new Color(newRed,newGreen,newBlue,alpha).getRGB());
}
return img;}
Any hint?
Use the Color constuctor that takes two arguments:
Color pixelVal = new Color(original.getRGB(x, y), true);
This constructor creates a Color with an ARGB color, if the hasAlpha parameter is true.
The single argument constructor Color(int) does not take transparency into account. From the JavaDoc:
Creates an opaque sRGB color with the specified combined RGB value [...] Alpha is defaulted to 255.
Apart from that, your code should probably work. :-)

How to manipulate BufferedImage

Processing has a class named PImage from which you can get an array of int that contains values for all pixels. Then, you manipulate this array and call updatePixels() and voila you have applied an effect to the image.
I was wondering if the same can be done in BufferedImage with some appropriate mechanism. I found that BufferedImage does indeed have a method to get pixels as int[]:
public int[] getRGB(int startX,
int startY,
int w,
int h,
int[] rgbArray,
int offset,
int scansize)
Returns an array of integer pixels in the default RGB color model (`TYPE_INT_ARGB`) and default sRGB color space, from a portion of the image data.
Color conversion takes place if the default model does not match the image `ColorModel`.
There are only 8-bits of precision for each color component in the returned data when using this method.
How do I modify these pixels and show the appropriate changes in BufferedImage ?
I guess I will need to obtain a WritableRaster for the image and use
public void setPixels(int x,
int y,
int w,
int h,
int[] iArray)
but I am still unsure.
To create a WritableRaster, you have to choose a ColorModel first.
I think the RGB default should suit your needs.
ColorModel colorModel = ColorModel.getRGBdefault();
WritableRaster raster = colorModel.createCompatibleWritableRaster(width, height);
Then, you can fill it with your pixels and create a new BufferedImage.
raster.setPixels(0, 0, width, height, pixels);
BufferedImage image = new BufferedImage(colorModel, raster, true, null);
And just as a reminder, here is a way to extract pixels from a BufferedImage:
Raster raster = bufferedImage.getRaster();
int[] pixels = raster.getPixels(0, 0, raster.getWidth(), raster.getHeight(), (int[]) null);
The PImage class integrates a bit with the BufferedImage class:
//BufferedImage to PImage
PImage img = new PImage(yourBufferedImageInstance);
//PImage to BufferedImage
BufferedImage img = (BufferedImage)yourPImageInstance.getNative();

How to scale text on a texture (quad object)?

I have found a good way to draw text in openGL by creating a Bitmap, drawing text on it, and making it into a texture.
I have screenCoordinates(320,~~480) and gameCoordinates(20,28), in which quads' dimensions are defined. Example constructor: GameObject(x, y, width, height, text).
To create the bitmap I create a Canvas with this size:
// get width and height in px (p is a Paint() object)
p.setTextSize(textSize * Main.getMainResources().getDisplayMetrics().density);
final int width = (int) p.measureText(text);
final int height = (int) (p.descent() + -p.ascent());
canvas.drawText(text, 0, -p.ascent(), p);
Create a bitmap of this, scaled to the next power of 2, and you have your texture-bitmap.
However the problem is that because the quad's dimensions are not adapted to the bitmap(text) dimensions, the text isn't very smooth. I have the possibility to change the quad's dimensions with quad.setDimensions(width, height), but what would be the size I'd have to give it, and also shouldn't I also have to worry about the textSize(sp) of the text i'm drawing?
This all is in 2D, and some simple math's might get involved, but how should I get the right dimensions?

Java: understanding Image Rasters

to my understanding the following code
int [] pixels = image.getRaster().getPixels(0, 0, width, height, (int[])null);
should generate an array which has exactly the size width x height, but in practice it seems to be much larger, why?
image may be a bufferedimage, toolkitimage or volatileimage.
it generates a array with
new int[numBands * w * h]; // The number of bands of the image data.
from the SampleModel from your Raster

Categories

Resources