how to convert BufferedImage to Printable so I can use printJob - java

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.

Related

Screenshot an element which is not visible?

I'm trying to get a screenshot of a component in a JFrame.
The problem is that this component is huge, and you can't see it entirely in the window (you have to scroll down to see the hidden part).
I tried several things, for example this :
Rectangle rect = component.getBounds();
try {
String format = "png";
String fileName = component.getName() + "." + format;
BufferedImage captureImage =
new BufferedImage(rect.width, rect.height,
BufferedImage.TYPE_INT_ARGB);
component.paint(captureImage.getGraphics());
ImageIO.write(captureImage, format, new File(fileName));
System.out.printf("The screenshot of %s was saved!", component.getName());
} catch (IOException ex) {
System.err.println(ex);
}
And this (coming from here) :
BufferedImage bi;
try {
bi = ScreenImage.createImage( component );
ScreenImage.writeImage(bi, "Screen-Image.jpg");
} catch (AWTException | IOException e) {
e.printStackTrace();
}
But in both case, the screenshot I get only shows what I can see in the window.
So if i scrolled down, i miss the part above, and if i scroll up i miss the bottom.
Do you know how I could do to render the whole component in a picture ?

Convert an ImageIcon to a Base64 String and back to an ImageIcon without saving to disk?

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

Lejos NXT: draw line and save image

I've managed to make a LineFollower program; added the feature of "memorizing" the path the robot just followed.
The next step is to draw that path and save the image file in the brick and read it in the PC with the NxjBrowse.
I thought I'd try using the classic java method, with BufferedImage and saving with ImageIO, but it didn't work and it kept giving me Java Heap Space:
My previous question
After that, I've made some research and found that there's a class called javax.microedition.lcdui.Image, so I've created an Image object and used GetGraphics and tried to draw on it; and save it using FileOutputStream, here's my code:
Image img = Image.createImage(300, 300);
Graphics g = img.getGraphics();
g.drawLine(0, 0, 100, 200);
File monFichier = new File("Image2.png");
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(monFichier);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] b = img.getData();
try {
fOut.write(b);
fOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The problem is that it writes on the file which is not recognized as an image, when I connect to my PC; I can't open the created file (checked the size, not empty).
I don't know if the saving is wrong or the method i'm using to draw is wrong.
Short version of the question: How to draw lines with Lejos and save the resultat as an image file?
Thank you.
UPDATE:
I used an ImageOutputStream instead of FileOutputStream; and now it's giving me "Java Heap Space" error; after it got stuck in "linking" for a while.
Java Heap Space
Java.lang.OutOfMemoryError
Image.getData() is an access to the underlying DataBuffer and not a valid PNG or BMP image.
Try ImageIO.write(img, "png", outputfile).

How to print conent of a JFrame by printer

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

Java file operations after opening image with ImageIO

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

Categories

Resources