I have an image, I read the image, add a few things to image (like some text etc).
All this I do inside a JPanel.
Now, I want to save the resulting image to a .png file.
I think, there is a way to do this for a buffered image using ImageIO.write()
But I cannot convert the dynamically created image to a BufferedImage.
Is there a way I can go about this ?
You can use the Screen Image class.
It will create a BufferedImage of your JPanel. The class also has code to write the image to a file.
All this I do inside a JPanel.
Do it instead in another BufferedImage displayed in a JLabel. The code can get a Graphics2D object using BufferedImage.createGraphics() method. Paint the image and text to the new Graphics2D instance and you then the new image can be saved directly, along with changes.
Use Following method it worked for me...
void TakeSnapShot(JPanel panel,String Locatefile){
BufferedImage bi = new BufferedImage(panel.getSize().width, panel.getSize().height,BufferedImage.TYPE_INT_RGB);
panel.paint(bi.createGraphics());
File image = new File(Locatefile);
try{
image.createNewFile();
ImageIO.write(bi, "png", image);
}catch(Exception ex){
}
}
Related
I am reading a gif image from internet url.
// URL of a sample animated gif, needs to be wrapped in try-catch block
URL imageUrl = new Url("http://4.bp.blogspot.com/-CTUfMbxRZWg/URi_3Sp-vKI/AAAAAAAAAa4/a2n_9dUd2Hg/s1600/Kei_Run.gif");
// reads the image from url and stores in BufferedImage object.
BufferedImage bImage = ImageIO.read(imageUrl);
// creates a new `java.io.File` object with image name
File imageFile = new File("download.gif");
// ImageIO writes BufferedImage into File Object
ImageIO.write(bImage, "gif", imageFile);
The code executes successfully. But, the saved image is not animated as the source image is.
I have looked at many of the stack-overflow questions/answers, but i am not able to get through this. Most of them do it by BufferedImage frame by frame which alters frame-rate. I don't want changes to the source image. I want to download it as it is with same size, same resolution and same frame-rate.
Please keep in mind that i want to avoid using streams and unofficial-libraries as much as i can(if it can't be done without them, i will use them).
If there is an alternative to ImageIO or the way i read image from url and it gets the thing done, please point me in that direction.
There is no need to decode the image and then re-encode it.
Just read the bytes of the image, and write the bytes, as is, to the file:
try (InputStream in = imageUrl.openStream()) {
Files.copy(in, new File("download.gif").toPath());
}
I am Currently using ICEPDF to render PDF files and display it in my java Swing application (in Internal Frame). Now I want to add crop features to my Java application. Like, if I click a button, I can drag required portion of PDF and save it as Image in my local storage.
Is there an efficient way to crop out PDF and save as image (.JPG) via java program?
Ghost4J library (http://ghost4j.sourceforge.net), is your best option:
3 simple step:
Load PDF files:
PDFDocument document = new PDFDocument();
document.load(new File("test.pdf"));
Create the renderer:
SimpleRenderer renderer = new SimpleRenderer();
// set resolution (in DPI)
renderer.setResolution(600);
Render:
List<Image> images = renderer.render(document);
Then you can do what you want with your image objects, for example, you can write them as JPG like this:
for (int i = 0; i < images.size(); i++) {
ImageIO.write((RenderedImage) images.get(i), "jpg", new File((i + 1) + ".jpg"));
}
Ghost4J uses the native Ghostscript API so you need to have a Ghostscript installed.
EDIT: investigating a bit, if you convert the PDF to Image you won't have much problem to crop them:
BufferedImage is a(n) Image, so the implicit cast that you're doing in the second line is able to be compiled directly. If you knew an Image was really a BufferedImage, you would have to cast it explicitly like so:
Image image = ImageIO.read(new File(file));
BufferedImage buffered = (BufferedImage) image;
Then you can crop it with BufferedImage::getSubimage method:
private BufferedImage cropImage(BufferedImage src, Rectangle rect) {
BufferedImage dest = src.getSubimage(rect.x, rect.y, rect.width, rect.height);
return dest;
}
I am trying to upload an image on a GUI. Here is what my code reads right now:
ImageIcon icon = new ImageIcon(nameSearched.getImage());
myLabelK = new JLabel();
myLabelK.setBounds(500,100,200,200);
myLabelK.setIcon(icon);
myPanel.setLayout(null);
myPanel.add(myLabelK);
Validate();
add(myPanel);
setVisible(true);
When I run my program this image is not popping up on my GUI.
The nameSearched.getImage() is calling a method in a different class which returns an image entered into the system. For example, Peter.jpg.
Please help me figure out how to get my image on the screen.
If you are just using Peter.jpg as you relative path name,
Using an IDE, you image should be ditrectly below the ProjectRoot directory
ProjectRootDir
Peter.jpg
src
If you wanted to have your image in an images folder:
use "images\Peter.jpg"
ProjectRootDir
images
Peter.jpg
src
Maybe you want to retrieve the image like this
ImageIcon icon = new ImageIcon("Peter.jpg");
Another possibility, If you do this:
ImageIcon icon = new ImageIcon(nameSearched.getImage());
then nameSearched.getImage() must return either a String fie path of the image
or an Image object.
Maybe you just want nameSearched.getImage() to return a String from an array of String paths. Not sure what nameSearched.getImage() actually does so I can't go into further explanation or correction
i have a the path of an image in a string i want to display it on a jlabel using the path. please help me.
if(!ar.get(16).equals("empty")){
String photo=(String)ar.get(16);
System.out.println(photo);
// if(!photo.equals(""))
// pic.setText(photo);
Image image=Toolkit.getDefaultToolkit().getImage(photo);;
ImageIcon img=new ImageIcon(image.getScaledInstance(view.jLabel5.getWidth(), view.jLabel5.getHeight(), 0));
//JpegReader jrdr=new JpegReader();
//view.jLabel5.setSize(img, image.getWidth());
view.jLabel5.setPreferredSize(new Dimension(100, 100));
view.jLabel5.setIcon(img);
}
If the image is an embedded resources (ie lives within the application context/is bundled with the application Jar), then you need to use getResource to gain access to it..
Toolkit.getDefaultToolkit().getImage expects that the String passed to it is a file on the file system.
If the image is embedded, then you will need to use something more like...
Toolkit.getDefaultToolkit().getImage(getClass().getResource(photo))
To load it.
If the image is being loaded from the file system, you could use
File file = new File(photo);
if (file.exists()) {
// Attempt to load the image
} else {
// Show error message.
}
Because of the way Toolkit#getImage works, it will not provide any details if the image fails to load for some reason.
Instead, you should be using ImageIO, which will throw an IOException if it was unable to load the image for some reason...
BufferedImage img = ImageIO.read(getClass().getResource(photo));
or
BufferedImage img = ImageIO.read(new File(photo));
Depending on where the image is located.
Take a look at Reading/Loading an Image.
You should also avoid calling setPreferredSize explicitly and simply allow JLabel to make it's own choices...
Put the image file in the project. Under a seperate folder.
ImageIcon image = new ImageIcon(this.getClass().getResource("/images/abc.jpg"));
JLabel imageLabel = new JLabel(image, JLabel.CENTER);
If you want to load the image for any other location on your computer then,
ImageIcon image = new ImageIcon("C:/images/image.png");
JLabel imagelabel = new JLabel(image);
Make sure your paths are correct. If you photo string path is "photo.jpeg", your path should look something like this
ProjectRoot
photo.jpeg
src
If you want to put the photo.jpeg in a directory images, you should use "images/photo.jpeg"
ProjectRoot
images
photo.jpeg
src
This is considering you're using an IDE like Netbeans or Ecplise.
I need to use a JPEG image in my Java applet.
In my applet class, I define the image name and create an object to ImageBuffer class.
String iname= "image1.jpg";
b = new ImageBuffer(iname,this);
In the ImageBuffer class, I call
Image image = null;
image = Toolkit.getDefaultToolkit().getImage(new URL(applet.getCodeBase(),fileName));
While this does not flag an error and image is not null anymore, it does not initialize image correctly. The height and width are -1. The url of the path however appears to be correct : /C:/Users/..../image1.jpg
How do I correctly load the image? It is in the bin file of my Eclipse project currently.
The height and width are -1.
Use a MediaTracker to monitor the asynchronous loading progress of the image. Alternately use ImageIO to load the image prior to the next code line.
If the image is placed in the correct location, then this would fix it
image = new ImageIcon(Toolkit.getDefaultToolkit().getImage(new URL(applet.getCodeBase(),fileName))).getImage();
If the code returns a NullPointerException it means the image is not placed in the correct directory.