Image.getScaledInstance() doesn't work - java

I would like scale image. I am trying use getScaledInstance() method by doesn't work for me. Image which I write is the same size as the funniest...
It is my code:
Image image = ImageIO.read(file.getInputStream());
image.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
BufferedImage bImage = (BufferedImage) image;
ImageIO.write(bImage, "jpg", new File(urlImage));

Image.getScaledInstance() returns an instance of the same image, scaled to the desired size without modifying your existing image. Assuming you are trying to save the scaled instance to file, you merely need to move the call to the line below.
E.g:
Image image = ImageIO.read(file.getInputStream());
BufferedImage bImage = (BufferedImage) image.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
ImageIO.write(bImage, "jpg", new File(urlImage));

Related

How to extract a subimage using PixelReader and JavaFX?

I have a .png image, and I want to extract one part of that image using the PixelReader class, and rebuild it as an image :
Image image = new Image("file:ressources/spritesheets/Zelda_Overworld.png");
byte[] buffer = new byte[1024];
PixelReader pr = image.getPixelReader();
pr.getPixels(0, 0, 16, 16, PixelFormat.getByteBgraInstance(), buffer, 0, 64);
Image tile = new Image(new ByteArrayInputStream(buffer));
I can display image and buffer seems to contain values, but I can't display tile, tile.getPixelReader() returns null, tile.getWidth() and tile.getHeight() return 0.0.
Do you know what I am doing wrong?
Paul
Let WritableImage do this for you:
Image image = new Image("file:ressources/spritesheets/Zelda_Overworld.png");
Image tile = new WritableImage(image.getPixelReader(), x, y, width, height);
Depending on the use of tile doing this may not be necessary at all. ImageView has a viewport property that allows you to choose the part of the image to display and GraphicsContext provides an overloaded version of the drawImage method to draw a part of the image to the Canvas.

Save a BufferedImage to BMP/PNG/JPG and then open it back

I have made a project like Paint from windows and now I want to make the save/open buttons. I have found out how to save the bufferedImage, but the problem is how do I open it back in the correct location and be able to draw on it again?
To read in an image, use ImageIO.
File myPath = new FIle("path to image");
BUfferedImage img = ImageIO.read(myPath);
Also what you could (should) do is load the image into your user space so that you don't edit the original image:
public static BufferedImage userSpace(BufferedImage image)
{
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics = newImage.createGraphics();
graphics.drawRenderedImage(image, null);
graphics.dispose();
return newImage;
}

How to convert Image into BufferedImage

Right now I have an image and I want to convert it into a BufferedImage...
This is my code -
private BufferedImage toBufferedImage(Image img, int width, int height){
// Create a buffered image with transparency
BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics bGr = bimage.getGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
// Return the buffered image
return bimage;
}
But it doesn't work. Can someone explain what's wrong?
Try the new JRE that includes JavaFX.
You could use SwingFXUtils. There is a method which does what you ask for:
BufferedImage image = SwingFXUtils.fromFXImage(fxImage, null);
or
BufferedImage image = ImageIO.read(new File(filename));
BufferedImage extends Image, so you can cast it:
BufferedImage buffered = (BufferedImage) image;
But you must check the type of your image object in runtime, because it can contain other kind of Image.
UPDATE
To convert an Image to a BufferedImage you should check this class and try it:
http://www.dzone.com/snippets/converting-images

Efficient way to crop a strip of a BufferedImage

I want to remove a strip (widthways) of an image by moving the top part of the image to the bottom. Currently I'm doing this but I think that maybe there is a more efficiently way to achieve this without creating a new BufferedImage.
Snippet code:
BufferedImage myImage = ...;
...
BufferedImage imgPart_1 = myImage.getSubimage(0, 0, myImage.getWidth(), (myImage.getHeight()/2)-50);
BufferedImage imgPart_2 = myImage.getSubimage(0, myImage.getHeight()/2, myImage.getWidth(), myImage.getHeight()/2);
BufferedImage newImage = new BufferedImage(myImage.getWidth(), myImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = newImage.createGraphics();
g.drawImage(imgPart_1, 0, 0, null);
g.drawImage(imgPart_2, 0, imgPart_1.getHeight(), null);
myImage = newImage;
...
Thanks in advance.
You will have to create a new Image, but you don't have to paint it yourself.
You can use the CropImageFilter to get your image.
Toolkit toolkit = Toolkit.getDefaultToolkit();
CropImageFilter cropFilter = new CropImageFilter
(x, y, imageWidth, imageHeight);
Image croppedImage = toolkit.createImage(new FilteredImageSource
(image.getSource(), cropFilter));
Looks pretty efficient to me: are you really sure there is a performance problem here?
If you really want to avoid creating a new bufferedimage, you can use myImage as the destination, i.e. just do:
Graphics g = myImage.createGraphics();
g.drawImage(imgPart_1, 0, 0, null);
g.drawImage(imgPart_2, 0, imgPart_1.getHeight(), null);
I think this will work OK in your case, although you will need to test (there can be some odd effects when the source and destination areas are overlapping!)

How can I create an image with Java?

I would like to create a gif image of a filled red circle on a green background. What is the easiest way to do it in Java?
If you already have an image file or image URL, then you can use Toolkit to get an Image:
Image img = Toolkit.getDefaultToolkit().createImage(filename);
If you need to construct a new image raster and paint into the image, then you can use BufferedImage. You can paint onto a BufferedImage, by calling its createGraphics function and painting on the graphics object. To save the BufferedImage into a GIF, you can use the ImageIO class to write out the image.
The best way is to generate a BufferedImage:
BufferedImage img = new BufferedImage(int width, int height, int imageType)
// you can find the Types variables in the api
Then, generated the Graphics2D of this image, this object allows you to set a background and to draw shapes:
Graphics2D g = img.createGraphics();
g.setBackground(Color color) ; //Find how to built this object look at the java api
g.draw(Shape s);
g.dispose(); //don't forget it!!!
To built the image:
File file = new File(dir, name);
try{
ImageIO.write(img, "gif", file);
}catch(IOException e){
e.printStackTrace();
}
Create a BufferedImage and then write it to a file with ImageIO.write(image, "gif", fileName).

Categories

Resources