Convert imagicon to image - java

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;
}

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

Error: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage

please help me cant do this thing to work to me sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
if (shape.hasImage())
{
// If this shape is an image, extract image to file
String extension = ImageTypeToExtension(shape.getImageData().getImageType());
String imageFileName = MessageFormat.format("Image.ExportImages.{0} Out.{1}", imageIndex, extension);
String strBarCodeImageExtracted = "" + imageFileName;
shape.getImageData().save(strBarCodeImageExtracted);
// Recognize barcode from this image
BarCodeReader reader = new BarCodeReader ((BufferedImage) Toolkit.getDefaultToolkit().getImage(strBarCodeImageExtracted),BarCodeReadType.Code39Standard);
while (reader.read())
{
System.out.println("codetext: " + reader.getCodeText());
}
imageIndex++;
}
EDIT: This answer was accepted after the comment was written, so one has to assume that the comment was the actual solution. The comment was
... to replace Toolkit.getDefaultToolkit().getImage(...) with ImageIO.read(...) ...
Original answer:
You may either try to read the image direcly with ImageIO, or consider painting the image into a newly allocated BufferedImage, e.g. with a method like
public static BufferedImage convertToBufferedImage(Image image)
{
BufferedImage newImage = new BufferedImage(
image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return newImage;
}
And then in your case:
Image image = Toolkit.getDefaultToolkit().getImage(strBarCodeImageExtracted);
BufferedImage bufferedImage = convertToBufferedImage(image);
BarCodeReader reader = new BarCodeReader(bufferedImage,BarCodeReadType.Code39Standard);

How to add text to an image in java?

I need to add some texts to an existing table image (png).
Which means that I need to "write" on the image and I need the option to select the text location.
How can I do it?
It's easy, just get the Graphics object from the image and draw your string onto the image. This example (and output image) is doing that:
public static void main(String[] args) throws Exception {
final BufferedImage image = ImageIO.read(new URL(
"http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));
Graphics g = image.getGraphics();
g.setFont(g.getFont().deriveFont(30f));
g.drawString("Hello World!", 100, 100);
g.dispose();
ImageIO.write(image, "png", new File("test.png"));
}
Output (test.png):

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