I have the following java method, which successfully creates the png file:
TakesScreenshot scrShot = ((TakesScreenshot) webdriver);
File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);
File DestFile = new File(fileWithPath + featureFileName + ".png");
// BufferedImage img = ImageIO.read(SrcFile);
// ImageIO.write(img, "jpg", new File(fileWithPath + featureFileName + ".jpg"));
FileUtils.copyFile(SrcFile, DestFile);
I'm trying to convert the image to jpg using the 2 commented lines, but jpg output file is not being produced. No error. No file. I can't figure out why. Thanks in advance for any help.
You are likely using OpenJDK that is having number of issues with JPG encoding, especially when you convert from png.
https://bugs.openjdk.java.net/browse/JDK-8211748
ImageIO not able to write a JPEG file
So that your workaround would be to convert image BufferedImage to another BufferedImage and then save it like:
try {
TakesScreenshot scrShot = ((TakesScreenshot) driver);
File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);
BufferedImage pngImage = ImageIO.read(SrcFile);
int height = pngImage.getHeight();
int width = pngImage.getWidth();
BufferedImage jpgImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
jpgImage.createGraphics().drawImage(pngImage, new AffineTransform(1f,0f,0f,1f,0,0), null);
ImageIO.write(jpgImage, "jpg", new File("/your_path/output.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
Related
I'm trying to take pages from one PDF, scale them down, and put them side-by-side in another PDF. To do this I make an intermediate PDF that has all of the pages from the source scaled down to the size I need to place them side-by-side. Then I go thought the scaled PDF and copy the pages two at a time to the final PDF. My thinking is that I'm down with the scaled PDF so I can close it but when I do that I get an error trying to save the final PDF that says
COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
I'm not sure why the intermediate doc should matter when I try to save the final doc. It could be that I'm doing something wrong in the copying of pages? Here's the code I use for that:
private PDDocument sideBySide(PaperSize paperSize, PaperSize pageSize) throws IOException {
PDRectangle targetPaperSize = getRect(paperSize);
PDRectangle targetPageSize = getRect(pageSize);
PDDocument scaledDoc = scaleDoc(pageSize, doc);
PDDocument outputDoc = new PDDocument();
final double theta = Math.PI / 2;
for (int offset = 0; offset < scaledDoc.getNumberOfPages() - 1; offset+=2) {
PDPage twoUp = new PDPage(targetPaperSize);
twoUp.setRotation(90);
twoUp.setResources(new PDResources());
outputDoc.addPage(twoUp);
PDPage leftPage = scaledDoc.getPage(offset);
PDPage rightPage = scaledDoc.getPage(offset + 1);
PDFormXObject leftObject = importAsXObject(outputDoc, leftPage);
twoUp.getResources().add(leftObject);
PDFormXObject rightObject = importAsXObject(outputDoc, rightPage);
twoUp.getResources().add(rightObject);
PDPageContentStream content = new PDPageContentStream(outputDoc, twoUp);
AffineTransform leftTrans = AffineTransform.getRotateInstance(theta);
leftTrans.concatenate(AffineTransform.getTranslateInstance(0, -targetPageSize.getHeight()));
AffineTransform rightTrans = AffineTransform.getRotateInstance(theta);
rightTrans.concatenate(AffineTransform.getTranslateInstance(targetPageSize.getWidth(), -targetPageSize.getHeight()));
leftObject.setMatrix(leftTrans);
rightObject.setMatrix(rightTrans);
content.drawForm(leftObject);
content.drawForm(rightObject);
content.close();
}
scaledDoc.close();
return outputDoc;
}
I am using org.apache.commons.net.ftp.FTPClient for retrieving files from a ftp server. It is crucial that I preserve the last modified timestamp on the file when its saved on my machine. Do anyone have a suggestion for how to solve this?
This is how I solved it:
public boolean retrieveFile(String path, String filename, long lastModified) throws IOException {
File localFile = new File(path + "/" + filename);
OutputStream outputStream = new FileOutputStream(localFile);
boolean success = client.retrieveFile(filename, outputStream);
outputStream.close();
localFile.setLastModified(lastModified);
return success;
}
I wish the Apache-team would implement this feature.
This is how you can use it:
List<FTPFile> ftpFiles = Arrays.asList(client.listFiles());
for(FTPFile file : ftpFiles) {
retrieveFile("/tmp", file.getName(), file.getTimestamp().getTime());
}
You can modify the timestamp after downloading the file.
The timestamp can be retrieved through the LIST command, or the (non standard) MDTM command.
You can see here how to do modify the time stamp: that: http://www.mkyong.com/java/how-to-change-the-file-last-modified-date-in-java/
When download list of files, like all files returned by by FTPClient.mlistDir or FTPClient.listFiles, use the timestamp returned with the listing to update timestemp of local downloaded files:
String remotePath = "/remote/path";
String localPath = "C:\\local\\path";
FTPFile[] remoteFiles = ftpClient.mlistDir(remotePath);
for (FTPFile remoteFile : remoteFiles) {
File localFile = new File(localPath + "\\" + remoteFile.getName());
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile));
if (ftpClient.retrieveFile(remotePath + "/" + remoteFile.getName(), outputStream))
{
System.out.println("File " + remoteFile.getName() + " downloaded successfully.");
}
outputStream.close();
localFile.setLastModified(remoteFile.getTimestamp().getTimeInMillis());
}
When downloading a single specific file only, use FTPClient.mdtmFile to retrieve the remote file timestamp and update timestamp of the downloaded local file accordingly:
File localFile = new File("C:\\local\\path\\file.zip");
FTPFile remoteFile = ftpClient.mdtmFile("/remote/path/file.zip");
if (remoteFile != null)
{
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile));
if (ftpClient.retrieveFile(remoteFile.getName(), outputStream))
{
System.out.println("File downloaded successfully.");
}
outputStream.close();
localFile.setLastModified(remoteFile.getTimestamp().getTimeInMillis());
}
I am trying to generate the barcode from barcode4j library(code128bean, other barcode beans) and try to add to the existing pdf. The barcode image is getting created locally using the below code.
//Create the barcode bean
Code128Bean code128Bean = new Code128Bean();
final int dpi = 150;
code128Bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar
//width exactly one pixel
//bean.setCodeset(2);
code128Bean.doQuietZone(false);
//Open output file
File outputFile = new File("D:/barcode4jcod128.png"); //I dont want to create it
OutputStream code128Stream = new FileOutputStream(outputFile);
try {
//Set up the canvas provider for monochrome PNG output
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
code128Stream, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
//Generate the barcode
code128Bean.generateBarcode(canvas1, "123456");
//Signal end of generation
canvas1.finish();
} finally {
code128Stream.close();
}
My problem is I don't want to create an image and save it locally in filesystem and then add it as image to pdf. I just want to create dynamically i mean just create the barcode image dynamically and add it to the pdf.
How do I set the pagesize (like PDPage.PAGE_SIZE_A4) to the existing PDPages which I retrieved from catalog.getAllPages() method, like (List<PDPage> pages = catalog.getAllPages();)
Can somebody help on this?
Thank you so much for your help Tilman. Here is what i did
public static BufferedImage geBufferedImageForCode128Bean(String barcodeString) {
Code128Bean code128Bean = new Code128Bean();
final int dpi = 150;
code128Bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar
code128Bean.doQuietZone(false);
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0
);
//Generate the barcode
code128Bean.generateBarcode(canvas1, barcodeString);
return canvas1.getBufferedImage();
}
// main code
PDDocument finalDoc = new PDDocument();
BufferedImage bufferedImage = geBufferedImageForCode128Bean("12345");
PDXObjectImage pdImage = new PDPixelMap(doc, bufferedImage);
PDPageContentStream contentStream = new PDPageContentStream(
finalDoc, pdPage, true, true, true
);
contentStream.drawXObject(pdImage, 100, 600, 50, 20);
contentStream.close();
finalDoc.addPage(pdPage);
finalDoc.save(new File("D:/Test75.pdf"));
The barcode is getting created the but it is created in vertical manner. i would like to see in horizontal manner. Thanks again for your help.
1) add an image to an existing page while keeping the content:
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0
);
code128Bean.generateBarcode(canvas1, "123456");
canvas1.finish();
BufferedImage bim = canvas1.getBufferedImage();
PDXObjectImage img = new PDPixelMap(doc, bim);
PDPageContentStream contents = new PDPageContentStream(doc, page, true, true, true);
contents.drawXObject(img, 100, 600, bim.getWidth(), bim.getHeight());
contents.close();
2) set the media box to A4 on an existing page:
page.setMediaBox(PDPage.PAGE_SIZE_A4);
My target is to read a file in SD Card and then process it in my program.
All THINGS WORKS FINE IN ANDROID EMULATOR!!!!
Unfortunately, when I work on my smartphone it didn't work at all!
public void receiveVideoRawData() throws IOException{
byte[] buf_rcv = new byte[153600];
File file = new File("/mnt/sdcard/Bluetooth/ardrone.raw");
ByteArrayOutputStream ous = new ByteArrayOutputStream();
InputStream ios = new FileInputStream(file);
int read = 0;
while ( (read = ios.read(buf_rcv)) != -1 ) {
ous.write(buf_rcv, 0, read);
}
ous.close();
ios.close();
ReadRawFileImage readMyRawData=new ReadRawFileImage();
image = readMyRawData.readUINT_RGBImage(buf_rcv);
File outputfile = new File("/mnt/sdcard/Bluetooth/ardroneCVT1.jpg");
OutputStream _outStream = new FileOutputStream(outputfile);
Bitmap pBitmap = image ;
pBitmap.compress(Bitmap.CompressFormat.JPEG, 90, _outStream);
_outStream.flush();
_outStream.close();
}
}
You are not supposed to hardcode the path to external storage directly because this may differ between devices, instead use getExternalStorageDirectory():
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), filename);
I m trying to attach an swf file to a pdf document. Below is my code (excerpted from the pdfbox-examples). while i can see that the file is attached based on the size of the file - with & without the attachment, I can't see / locate it in the pdf document. I do see textual content correctly displayed. Can someone tell me what I m doing wrong & help me fix the issue?
doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage( page );
PDFont font = PDType1Font.HELVETICA_BOLD;
String inputFileName = "sample.swf";
InputStream fileInputStream = new FileInputStream(new File(inputFileName));
PDEmbeddedFile ef = new PDEmbeddedFile(doc, fileInputStream );
PDPageContentStream contentStream = new PDPageContentStream(doc, page,true,true);
//embedded files are stored in a named tree
PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
//first create the file specification, which holds the embedded file
PDComplexFileSpecification fs = new PDComplexFileSpecification();
fs.setEmbeddedFile(ef);
//now lets some of the optional parameters
ef.setSubtype( "swf" );
ef.setCreationDate( new GregorianCalendar() );
//now add the entry to the embedded file tree and set in the document.
Map<String, COSObjectable> efMap = new HashMap<String, COSObjectable>();
efMap.put("My first attachment", fs );
efTree.setNames( efMap );
//attachments are stored as part of the "names" dictionary in the document catalog
PDDocumentNameDictionary names = new PDDocumentNameDictionary( doc.getDocumentCatalog() );
names.setEmbeddedFiles( efTree );
doc.getDocumentCatalog().setNames( names );
After struggling with the same thing, I've discovered this is a known issue. Attachments haven't worked for a while I guess.
Here's a link to the issue on the apache forum.
There is a hack suggested here that you can use.
I tried it and it worked!
the other work around i found is after you call setNames on your PDEmbeddedFilesNameTreeNode remove the limits: ((COSDictionary
)efTree.getCOSObject()).removeItem(COSName.LIMITS); ugly hack, but it
works, without having to recompile pdfbox
Attachment works fine with new version of PDFBox 2.0,
public static boolean addAtachement(final String fileName, final String... attachements) {
if (Objects.isNull(fileName)) {
throw new NullPointerException("fileName shouldn't be null");
}
if (Objects.isNull(attachements)) {
throw new NullPointerException("attachements shouldn't be null");
}
Map<String, PDComplexFileSpecification> efMap = new HashMap<String, PDComplexFileSpecification>();
/*
* Load PDF Document.
*/
try (PDDocument doc = PDDocument.load(new File(fileName))) {
/*
* Attachments are stored as part of the "names" dictionary in the
* document catalog
*/
PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
/*
* First we need to get all the existed attachments, after that we
* can add new attachments
*/
PDEmbeddedFilesNameTreeNode efTree = names.getEmbeddedFiles();
if (Objects.isNull(efTree)) {
efTree = new PDEmbeddedFilesNameTreeNode();
}
Map<String, PDComplexFileSpecification> existedNames = efTree.getNames();
if (existedNames == null || existedNames.isEmpty()) {
existedNames = new HashMap<String, PDComplexFileSpecification>();
}
for (String attachement : attachements) {
/*
* Create the file specification, which holds the embedded file
*/
PDComplexFileSpecification fs = new PDComplexFileSpecification();
fs.setFile(attachement);
try (InputStream is = new FileInputStream(attachement)) {
/*
* This represents an embedded file in a file specification
*/
PDEmbeddedFile ef = new PDEmbeddedFile(doc, is);
/* Set some relevant properties of embedded file */
ef.setCreationDate(new GregorianCalendar());
fs.setEmbeddedFile(ef);
/*
* now add the entry to the embedded file tree and set in
* the document.
*/
efMap.put(attachement, fs);
}
}
efTree.setNames(efMap);
names.setEmbeddedFiles(efTree);
doc.getDocumentCatalog().setNames(names);
doc.save(fileName);
return true;
} catch (IOException e) {
System.out.println(e.getMessage());
return false;
}
}
To 'locate' or see an attached file in the PDF, you can't flip through its pages to find any trace of it there (like, an annotation).
In Acrobat Reader 9.x for example, you have to click on the "View Attachments" icon (looking like a paper-clip) on the left sidebar.