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 ?
Related
I need to know if an image is landscape oriented or not, for that i´m getting the width and height of a BufferImage but sometimes the values appear as inverted.
This is the code I use to detect if an image is landscape:
public class ProcessImage{
private static final String IMAGE_ORIENTATION_ERROR ="001";
public static void main(String[] args) {
try {
File file = new File("C:\\myFolder\\nothing.jpg");
byte[] fileContent = Files.readAllBytes(file.toPath());
BufferedImage image = createImageFromBytes(fileContent);
validateImage(image);
System.out.println(image.getWidth());
} catch (IOException | ImageOrientationException e) {
e.printStackTrace();
}
}
private static BufferedImage createImageFromBytes(byte[] imageData) {
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
try {
return ImageIO.read(bais);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/* Here is where i validate if the image is landscape
But Values of image.getWidth() and image.getHeight() appear as inverted.*/
private static void validateImage (BufferedImage image)
throws ImageOrientationException{
//if this happens the picture is NOT landscape
if (image.getWidth() < image.getHeight()) {
throw new ImageOrientationException(IMAGE_ORIENTATION_ERROR,
"error oriented picture");
}
}
}
The problem i'm having is that for some images the values of heigth and width are inverted .
For example, for an image which has a width of 3024 and a height of 4032 , the values of width and height the BufferImage returns are width: 4032 and height:3024. But if i edit the same image in Paint (respecting the original sizes) the BufferImage returns width:3024 and height: 4032 (As it should be).
I have tested with other images with the same width and height and the BufferImage gets the rigth values
Do you have any idea of why is this happening? Is there any way to know if the image is really landscape oriented?
Here is a link with the image which has the problem: http://www.mediafire.com/file/n1a8uhy195zgesd/nothing.jpg/file
Thanks in advance!
I need to decode an .png image to use it as a base to create some Rects and to use the image itself as a background for my application, but the try-catch keeps throwing me a Null Pointer Exception, this is the code:
try {
InputStream is = ParamsSingleton.assetManager.open("background.png");
figure = BitmapFactory.decodeStream(is);
height = figure.getHeight();
width = figure.getWidth();
src = new Rect(0, 0, width, height);
first = new Rect();
} catch (IOException e) {
Log.d(TAG, "Image decode fail!");
}
I've tried some logs and the try-catch break right on the InputStream line and prints the 'Log.d' in catch block, I checked the file name and it is alright.
You can directly use getAssets.open method inside decodeStream .
figure = BitmapFactory.decodeStream(getAssets.open("background.png"));
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
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 am searching for a open-source java-library that enables me to render single pages of PDFs as JPG or PNG on server-side.
Unfortunately it mustn't use any other java.awt.* classes then
java.awt.datatransfer.DataFlavor
java.awt.datatransfer.MimeType
java.awt.datatransfer.Transferable
If there is any way, a little code-snippet would be fantastic.
i believe icepdf might have what you are looking for.
I've used this open source project a while back to turn uploaded pdfs into images for use in an online catalog.
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
public byte[][] convert(byte[] pdf, String format) {
Document document = new Document();
try {
document.setByteArray(pdf, 0, pdf.length, null);
} catch (PDFException ex) {
System.out.println("Error parsing PDF document " + ex);
} catch (PDFSecurityException ex) {
System.out.println("Error encryption not supported " + ex);
} catch (FileNotFoundException ex) {
System.out.println("Error file not found " + ex);
} catch (IOException ex) {
System.out.println("Error handling PDF document " + ex);
}
byte[][] imageArray = new byte[document.getNumberOfPages()][];
// save page captures to bytearray.
float scale = 1.75f;
float rotation = 0f;
// Paint each pages content to an image and write the image to file
for (int i = 0; i < document.getNumberOfPages(); i++) {
BufferedImage image = (BufferedImage)
document.getPageImage(i,
GraphicsRenderingHints.SCREEN,
Page.BOUNDARY_CROPBOX, rotation, scale);
try {
//get the picture util object
PictureUtilLocal pum = (PictureUtilLocal) Component
.getInstance("pictureUtil");
//load image into util
pum.loadBuffered(image);
//write image in desired format
imageArray[i] = pum.imageToByteArray(format, 1f);
System.out.println("\t capturing page " + i);
} catch (IOException e) {
e.printStackTrace();
}
image.flush();
}
// clean up resources
document.dispose();
return imageArray;
}
Word of caution though, I have had trouble with this library throwing a SegFault on open-jdk. worked fine on Sun's. Not sure what it would do on GAE. I can't remember what version it was that had the problem so just be aware.
You can apache PDF box APi for this purpose and use following to code to convert two pdfs into JPG page by page .
public void convertPDFToJPG(String src,String FolderPath){
try{
File folder1 = new File(FolderPath+"\\");
comparePDF cmp=new comparePDF();
cmp.rmdir(folder1);
//load pdf file in the document object
PDDocument doc=PDDocument.load(new FileInputStream(src));
//Get all pages from document and store them in a list
List<PDPage> pages=doc.getDocumentCatalog().getAllPages();
//create iterator object so it is easy to access each page from the list
Iterator<PDPage> i= pages.iterator();
int count=1; //count variable used to separate each image file
//Convert every page of the pdf document to a unique image file
System.out.println("Please wait...");
while(i.hasNext()){
PDPage page=i.next();
BufferedImage bi=page.convertToImage();
ImageIO.write(bi, "jpg", new File(FolderPath+"\\Page"+count+".jpg"));
count++;
}
System.out.println("Conversion complete");
}catch(IOException ie){ie.printStackTrace();}
}