I am trying to read and write an image dataset in Hadoop using java. I am only convenient with the normal bufferImage.
folder = new File("");
img = ImageIO.read(f);
ImageIO.read accepts an InputStream
You can get an InputStream from an HDFS file using the FileSystem class's open function
Related
I got a strange issue with a GIF image in Java. The image is provided by an XML API as Base64 encoded string. To decode the Base64, I use the commons-codec library in version 1.13.
When I just decode the Base64 string and write the bytes out to a file, the image shows properly in browsers and MS Paint (nothing else to test here).
final String base64Gif = "[Base64 as provided by API]";
final byte[] sigImg = Base64.decodeBase64(base64Gif);
File sigGif = new File("C:/Temp/pod_1Z12345E5991872040.org.gif");
try (FileOutputStream fos = new FileOutputStream()) {
fos.write(sigImg);
fos.flush();
}
The resulting file opened in MS Paint:
But when I now start consuming this file using Java (for example creating a PDF document from HTML using the openhtmltopdf library), it is corrupted and does not show properly.
final String htmlLetterStr = "[HTML as provided by API]";
final Document doc = Jsoup.parse(htmlLetterStr);
try (FileOutputStream fos = new FileOutputStream(new File("C:/Temp/letter_1Z12345E5991872040.pdf"))) {
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.useFastMode();
builder.withW3cDocument(new W3CDom().fromJsoup(doc), "file:///C:/Temp/");
builder.toStream(fos);
builder.useDefaultPageSize(210, 297, BaseRendererBuilder.PageSizeUnits.MM);
builder.run();
fos.flush();
}
When I now open the resulting PDF, the image created above looks like this. It seems that only the first pixel lines are printed, some layer is missing, or something like that.
The same happens, if I read the image again with ImageIO and try to convert it into PNG. The resulting PNG looks exactly the same as the image printed in the PDF document.
How can I get the image to display properly in the PDF document?
Edit:
Link to original GIF Base64 as provided by API: https://pastebin.com/sYJv6j0h
As #haraldK pointed out in the comments, the GIF file provided via the XML API does not conform to the GIF standard and thus cannot be parsed by Java's ImageIO API.
Since there does not seem to exist a pure Java tool to repair the file, the workaround I came up with now is to use ImageMagick via Java's Process API. Calling the convert command with the -coalesce option will parse the broken GIF and create a new one that does conform to the GIF standard.
// Decode broken GIF image and write to disk
final String base64Gif = "[Base64 as provided by API]";
final byte[] sigImg = Base64.decodeBase64(base64Gif);
Path gifPath = Paths.get("C:/Temp/pod_1Z12345E5991872040.tmp.gif");
if (!Files.exists(gifPath)) {
Files.createFile(gifPath);
}
Files.write(gifPath, sigImg, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
// Use the Java Process API to call ImageMagick (on Linux you would use the 'convert' binary)
ProcessBuilder procBuild = new ProcessBuilder();
procBuild.command("C:\\Program Files\\ImageMagick-7.0.9-Q16\\magick.exe", "C:\\Temp\\pod_1Z12345E5991872040.tmp.gif", "-coalesce", "C:\\Temp\\pod_1Z12345E5991872040.gif");
Process proc = procBuild.start();
// Wait for ImageMagick to complete its work
proc.waitFor();
The newly created file can be read by Java's ImageIO API and be used as expected.
I have an InputStream which I would like to convert to a PDF, and save that PDF in a directory. Currently, my code is able to convert the InputStream to a PDF and the PDF does show up in the correct directory. However, when I try to open it, the file is damaged.
Here is the current code:
InputStream pAdESStream = signingServiceConnector.getDirectClient().getPAdES(this.statusReader.getStatusResponse().getpAdESUrl());
byte[] buffer = new byte[pAdESStream.available()];
pAdESStream.read(buffer);
File targetFile = new File(System.getProperty("user.dir") + "targetFile2.pdf");
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
Originally, the InputStream was a pAdES-file (https://en.wikipedia.org/wiki/PAdES). However, it should be able to be read as just a regular PDF.
Does anyone know how to convert the InputStream to a PDF, without getting a damaged PDF as a result?
Hello it might be a bit late but you can use PDFBOX api (or itextpdf)
https://www.tutorialkart.com/pdfbox/create-write-text-pdf-file-using-pdfbox/
here is a tuto of the process gl
I have a REST webservice built with Jersey that does OCR (Optical Character Recognition) using Tesseract via the Tess4J Java binding. Now the Tess4J library expects you to send it an image file (png, jpg, tif amongst others), but with Jersey processing I get an InputStream that contains the image.
How do I convert this InputStream to a file type that Tesseract would recognise? I've tried the following:
import org.apache.commons.io.IOUtils;
.....
private static File stream2file (InputStream in) throws IOException {
final File tempFile = File.createTempFile("stream2file", ".tmp");
tempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(in, out);
}
return tempFile;
}
But then the Tesseract library throws an exception saying that it doesn't accept the file type I'm sending (Which now in this case is 'tmp'). I've tried changing that little 'tmp' to 'tif' and other supported file types but that just yielded the same results, so I'm obviously missing something here.
So how can I take an InputStream, convert it, and forward it to Tesseract as one of the supported file types that it expects?
The file extension of the temp file has to match that of the original input image file.
Besides File type, Tess4J also accepts BufferedImage as input. Just convert inputstream to it, as follows:
BufferedImage image = ImageIO.read(is);
try (FileOutputStream out = new FileOutputStream(tempFile)). You have got an error at this line.
You should use FileOutputStream (String) not FileOutputStream(File).
So it should be FileOutputStream(tempfile.getName()).
The parameter you pass to the constructor of FileOutputStream is a string that is the path to the real file or the name of the file. It's not a File object.
This question already has answers here:
Including Images with an executable jar
(2 answers)
Closed 9 years ago.
I want to read a bunch of image files from my current package because I want to get access to the files when my package which includes my image files is exported to others' computer.
I just want to make sure my program can read the images no matter if the package is in my computer.
I tried
File file = new File("images.jpg"); // It is wrong because the path is wrong.
// I want to assign the image as BuffuredImage
BufferedImage dealerCardImage1 = ImageIO.read(file);
I was wondering how the path of the files should be. What should I do?
put your image file inside the package of current class file and try this:
BufferedImage dealerCardImage1 = ImageIO.read(getClass().getResourceAsStream("images.jpg"));
Note that, getClass().getResourceAsStream(path) returns an InputStream that points to a path that starts from current package.
For example if you have a class file named HelloWorld inside package com.example, then HelloWorld.class.getResourceAsStream("images.jpg") returns an InputStream to image with this path: com/example/images.jpg
if you read from file use
Image image = new Image();
image = ImageIO.read(file);
// Read from an input stream
InputStream is = getClass()
.getResourceAsStream("/com/statement/SamplePDFStatementFile.txt");
image = ImageIO.read(is);
//if you read from url
// Read from a URL
URL url = new URL("http://hostname.com/image.gif");
image = ImageIO.read(url);
ImageIO.read(file); will return null if no registered ImageReader is not found.
Please check whether you have registered any ImageReader or not.
I think this code snippet could help you
File file = new File("images.jpg"); // Assuming images.jpg in my working directory
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis); //reading the image file
You just need to wrap the file into an FileInputStream and then pass it to read()
I am creating .PNG file using BufferedImage with some test. Now after creating image I am trying to convert .PNG image to .TIF, which is working fine. Now once I create TIF image, I want to delete PNG image. But because of some reason, I am not able to do this. There is no any exception for this.
Here is my code
File pngFile = null;
FileOutputStream fOut = null;
try {
pngFile = new File("C:\\Test.PNG");
fOut = new FileOutputStream ("C:\\Test.TIF");
RenderedOp src = JAI.create("fileload", "C:\\Test.PNG");
TIFFImageEncoder encoder = new TIFFImageEncoder (fOut, null);
encoder.encode (src);
}catch(Exception e) {
}finally {
fOut.close();
System.out.println(pngFile.delete());
}
Well there's definitely no exception since your catch block is empty.
Something may be still holding a handle to the file, not allowing it to be deleted.
I would examine JAI.create, RenderedOp and the TiffEncoder.
Instead of providing the file path as string you can provide input stream and in finally first close the input stream and then delete the file. This may work.
I was facing same problem sometime before. The best way to do it in this is to first dispose the resources using the image object you have create, like below-
var image = Image.FromFile(pngTarget); // here pngTarget is my PNG file's name along with complete path.
// your code to convert png to tiff
.
.
.
at the end of the method you can write below -
image.Dispose(); // the image object I have created above
File.Delete(pngTarget); // delete the file
Also, don't forget to flush/close the memory stream, if using any.
Thanks.