How to resize .bmp image in Java? - java

So I am trying to resize a .bmp image using the following code:
void resizeSeenAreaToFitRetina(String BMPImageFileName, int newWidth, int newHeight) throws IOException {
BufferedImage bmpImage = ImageIO.read(getClass().getResource(BMPImageFileName));
Image scaledBMPImage = bmpImage.getScaledInstance(newWidth, newHeight, Image.SCALE_FAST);
BufferedImage scaledBufferedBMPImage = (BufferedImage) scaledBMPImage; // I know I can't do this but ImageIO.write(only_takes_BufferedImage_class_here, ...)
ImageIO.write(scaledBufferedBMPImage, "bmp", new File("ResizedArray2DTest.bmp"));
}
how can I write the resized image to a file?

I hope these links helps you:
how to resize Image in java?
http://www.mkyong.com/java/how-to-resize-an-image-in-java/
http://www.mkyong.com/java/how-to-write-an-image-to-file-imageio/

Try these links
http://www.journaldev.com/615/java-image-resize-program-using-graphics2d-example
resizing image in java
How can I resize an image using Java?

Related

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

Converting BufferedImage to Mat (OpenCV) in Java [duplicate]

This question already has answers here:
Converting `BufferedImage` to `Mat` in OpenCV
(9 answers)
Closed 6 years ago.
I've tried this link and have the code below. My program imports images in a BufferedImage format and then displays it to the users. I'm using the matchingTemplate function in OpenCV which requires me to convert it to a Mat format.
The code works if I import the image -> convert it to Mat then save the image using imwrite. The program also allows the user to crop an image and then use the Template matching to compare it to another image. The issue come when I tried to convert the cropped image to Mat I need to convert it from Int to Byte using this code:
im = new BufferedImage(im.getWidth(), im.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
This however results in a black image. But if I get rid of it it only works with imported images and not cropped. What is going on here? I'm certain it is something to do with the coversion process as I have tested the template matching function using read in images.
// Convert image to Mat
public Mat matify(BufferedImage im) {
// Convert INT to BYTE
//im = new BufferedImage(im.getWidth(), im.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
// Convert bufferedimage to byte array
byte[] pixels = ((DataBufferByte) im.getRaster().getDataBuffer())
.getData();
// Create a Matrix the same size of image
Mat image = new Mat(im.getHeight(), im.getWidth(), CvType.CV_8UC3);
// Fill Matrix with image values
image.put(0, 0, pixels);
return image;
}
You can try this method, to actually convert the image to TYPE_3BYTE_BGR (your code simply created a blank image of the same size, that is why it was all black).
Usage:
// Convert any type of image to 3BYTE_BGR
im = toBufferedImageOfType(im, BufferedImage.TYPE_3BYTE_BGR);
// Access pixels as in original code
And the conversion method:
public static BufferedImage toBufferedImageOfType(BufferedImage original, int type) {
if (original == null) {
throw new IllegalArgumentException("original == null");
}
// Don't convert if it already has correct type
if (original.getType() == type) {
return original;
}
// Create a buffered image
BufferedImage image = new BufferedImage(original.getWidth(), original.getHeight(), type);
// Draw the image onto the new buffer
Graphics2D g = image.createGraphics();
try {
g.setComposite(AlphaComposite.Src);
g.drawImage(original, 0, 0, null);
}
finally {
g.dispose();
}
return image;
}

Convert imagicon to image

I need to convert an ImageIcon into an Image for a program how should I go about doing it? I there a method of casting I can use or a built in method to do it?
Building on the answer from the question I linked. I'm not sure which Image you mean, save it as a file or get a java Image instance, but you can use just the second method if you mean the latter, or use the former (which depends on the latter) if you need that.
// format should be "jpg", "gif", etc.
public void saveIconToFile(ImageIcon icon, String filename, String format) throws IOException {
BufferedImage image = iconToImage(icon);
File output = new File(filename);
ImageIO.write(bufferedImage, format, outputfile);
}
public BufferedImage iconToImage(ImageIcon icon) {
BufferedImage bi = new BufferedImage(
icon.getIconWidth(),
icon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
// paint the Icon to the BufferedImage.
icon.paintIcon(null, g, 0,0);
g.dispose();
return bi;
}

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