I would like to print the content of a JFrame/JPanel by printer how to do it? Could sb give me an example of code? I do not understand official tutorial.
EDIT:
I thought it is clear. I do not want to print it to file then print it manually. I want to use java to do this.
BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
this.paint(g);
g.dispose();
// File to save output Image
File imageOut = new File("screenshot.png");
try {
ImageIO.write(image, "png", imageOut);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
with this as a JFrame
Related
I'm trying to store an imageIcon bas a Base64 String.
This is what I have so far:
public ImageIcon getImageIcon() {
if(imageIcon == null || imageIcon.isEmpty()){
return null;
} else {
try {
byte[] btDataFile = Base64.decodeBase64(imageIcon);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(btDataFile));
return new ImageIcon(image);
} catch (IOException ex) {
System.out.println(ex.getLocalizedMessage());
return null;
}
}
}
public void setImageIcon(ImageIcon imageIconIn) {
imageIcon = Base64.encodeBase64String(imageToByteArray(imageIconIn));
}
public static byte[] imageToByteArray(ImageIcon imageIn) {
try {
BufferedImage image = new BufferedImage(imageIn.getIconWidth(), imageIn.getIconHeight(),BufferedImage.TYPE_INT_RGB);
ByteArrayOutputStream b = new ByteArrayOutputStream();
// FIX
Graphics g;
g = image.createGraphics();
imageIn.paintIcon(null, g, 0,0);
// END FIX
ImageIO.write(image, "jpg", b );
g.dispose();
return b.toByteArray();
} catch (IOException ex) {
System.out.println(ex.getLocalizedMessage());
return null;
}
}
I get a black rectangle instead of the image.
I'm using Java 1.8 on Ubuntu 16.04.
What am I doing wrong?
Thanks for your help.
******************************** . FIXED . ******************************
I found a working solution and updated the above code.
******************************** EDIT *********************************
Added g.dispose() after painting icon.
This code creates a brand new BufferedImage, with width & height same as the given image.
BufferedImage image = new BufferedImage(
imageIn.getIconWidth(),
imageIn.getIconHeight(),
BufferedImage.TYPE_INT_RGB);
Note that the image is empty. No content has been written to it. The bytes will all be zero, and RGB 0x000000 is black.
Then, you are writing the bytes of this black image to your ByteArrayOutputStream.
ByteArrayOutputStream b = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", b );
return b.toByteArray();
Of course, when you convert that byte buffer back to an image, it will be black.
You will want to draw/copy imageIn into your new image before you write out the bytes.
But if you don't mind using whatever the current image's format is, you could just write out that image instead of converting it to TYPE_INT_RGB...
Image image = imageIn.getImage();
// write image to ByteArrayOutputStream
I am trying to save an image that is formed with a BufferedImage. I get the BufferedImage by doing
(BufferedImage) fg;
fg is an image of my jPanel's graphics. I am successful at saving the image by hardcoding the path in directly as follows:
ImageIO.write((BufferedImage)fg,"png",new File("C:\\Users\\Geiger\\Documents\\test.png"));
But when I attempt to add the JFileChooser to the mix the image that is saved comes up being blank with nothing but the jPanel's background color.
My code for my attempt at utilizing the JFileChooser is as follows:
JFileChooser jfc = new JFileChooser();
int retVal = jfc.showSaveDialog(null);
if(retVal==JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
String test = f.getAbsolutePath();
ImageIO.write((BufferedImage)fg,"png",new File(test));
}
EDIT:To clarify on the issue a little bit more:
The issue is not that a file doesn't appear its that the graphics don't appear on the image when using the JFileChooser object.
I update my image when the JFrame has a mouse presses event:
fg = jPanel2.createImage(jPanel2.getWidth(), jPanel2.getHeight());
try to put this line of code I think that's what you need:
ImageIO.write(buffer, "png", fileDialog.getSelectedFile());
Hope that helps
Graphics2D graphics2D = image.createGraphics();
scribblePane.paint(graphics2D);
Use these two lines of code to add graphics.
That's works for me
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("*.png", "png"));
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
ImageIO.write((BufferedImage) image, "png", new File(file.getAbsolutePath()));
} catch (IOException ex) {
System.out.println("Failed to save image!");
}
} else {
System.out.println("No file choosen!");
}
}
I am working with Java and combine two images. I save the combined image and want to delete the overlay, but it seems there are still streams open. And i don't know which and how to close them.
f_overlay and f_image are both Files.
// load source images
BufferedImage image = null;
BufferedImage overlay = null;
try {
log.debug(f_image.getAbsolutePath());
log.debug(f_overlay.getAbsolutePath());
image = ImageIO.read(f_image);
overlay = ImageIO.read(f_overlay);
} catch (IOException e) {
log.error(e.getMessage());
}
// create the new image, canvas size is the max. of both image sizes
int w = Math.max(image.getWidth(), overlay.getWidth());
int h = Math.max(image.getHeight(), overlay.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);
// Save as new image
try {
ImageIO.write(combined, "PNG", f_image);
} catch (IOException e) {
log.error(e.getMessage());
}
// we can delete the overlay now
log.debug("Delete overlay: " + f_overlay.delete());
Are there any suggestions?
I can't see anything wrong in your code.
However, I would only delete the file f_overlay if the reading was successful. Important, after you call delete() on the file object, you must not use the object for anything else, so best is to assign f_overlay=null
boolean state = f_overlay.delete();
f_overlay=null;
log.debug("Delete ... "+state);
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):
I have already written code that will let me print my frame, but now I want to use printJob since it gives user more print option(such as selecting which printer to use). Is there a quick way to covert BufferedImage to Printable so i can use printjob in my code? thx
if(command.equals("Print")){
//saves image as temp and then prints it
File out = new File("temp.jpg");
BufferedImage myImage = new BufferedImage((int) frame.getWidth(),
(int) frame.getHeight(),
BufferedImage.TYPE_INT_RGB );
frame.paintAll(myImage.createGraphics());
try{
ImageIO.write(myImage, "jpg", out);
PrintImage.printImage("temp.jpg");
}catch (IOException exception){
exception.printStackTrace();
} catch (Exception e2) {
e2.printStackTrace();
}
}
You can use getGraphics to get the frame's underlying Graphics object, then pass that to the print method of a PrinterJob.