I'm writing a program that is supposed to taking in a bunch of tiff's and put them together. I got it to work for most of the image files I read in but a large batch of them throw out an error when I try to read them in.
Here is a snippet of code I have:
int numPages = 0;
inStream = ImageIO.createImageInputStream(imageFile);
reader.setInput(inStream);
while(true){
bufferedImages.add(reader.readAll(numPages, reader.getDefaultReadParam()));
numPages++;
}
Yes I catch the out of bounds exception so we don't have to worry about that. My problem is that I get the following error:
javax.imageio.IIOException: I/O error reading image metadata!
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader.readMetadata(TIFFImageReader.java:340)
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader.seekToImage(TIFFImageReader.java:310)
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader.prepareRead(TIFFImageReader.java:971)
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader.read(TIFFImageReader.java:1153)
at javax.imageio.ImageReader.readAll(ImageReader.java:1067)
at sel.image.appender.ImageAppender.mergeImages(ImageAppender.java:59)
at sel.imagenow.processor.AetnaLTCProcessor.processBatch(AetnaLTCProcessor.java:287)
at sel.imagenow.processor.AetnaLTCProcessor.processImpl(AetnaLTCProcessor.java:81)
at sel.processor.AbstractImageNowProcessor.process(AbstractImageNowProcessor.java:49)
at sel.RunConverter.main(RunConverter.java:37)
Caused by: java.io.EOFException
at javax.imageio.stream.ImageInputStreamImpl.readShort(ImageInputStreamImpl.java:229)
at javax.imageio.stream.ImageInputStreamImpl.readUnsignedShort(ImageInputStreamImpl.java:242)
at com.sun.media.imageioimpl.plugins.tiff.TIFFIFD.initialize(TIFFIFD.java:194)
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageMetadata.initializeFromStream(TIFFImageMetadata.java:110)
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader.readMetadata(TIFFImageReader.java:336)
... 9 more
I did make sure to add in the right JAI lib and my reader is using the "TIFF" type so the reader (and writer) is correct but for some reason the metadata is wrong. Now I can open and view all these images normally in windows so they really aren't corrupted or anything. Java just doesn't want to read them in right. Since I'm just using the stream meatadata to write them out later I don't care that much about the metadata I just need it to read in the file to the list so I can append it. I did find a writer.replaceImageMetaData method on the writer but the TIFFwriter version of IOWriter doens't have code for it. I'm stuck, anyone anything? Is there maybe a way to read in parts of the metadata to see what is wrong and fix it?
For anyone that would like to know I ended up fixing my own issue. It seems the the image metadata was a bit screwed up. Since I was just doing a plain merge and since I knew each image was one page I was able to use a buffered image to read in the picture then make it a IIOImage with null metadata. I used the stream metadata (which worked) to merge the images. Here is my complete method I use to merge a list of images:
public static File mergeImages(List<File> files, String argID, String fileType, String compressionType) throws Exception{
//find the temp location of the image
String location = ConfigManager.getInstance().getTempFileDirectory();
logger_.debug("image file type [" + fileType + "]");
ImageReader reader = ImageIO.getImageReadersByFormatName(fileType).next();
ImageWriter writer = ImageIO.getImageWritersByFormatName(fileType).next();
//set up the new image name
String filePath = location + "\\" + argID +"." + fileType;
//keeps track of the images we copied from
StringBuilder builder = new StringBuilder();
List<IIOImage> bufferedImages = new ArrayList<IIOImage>();
IIOMetadata metaData = null;
for (File imageFile:files) {
//get the name for logging later
builder.append(imageFile.getCanonicalPath()).append("\n");
if (metaData == null){
reader.setInput(ImageIO.createImageInputStream(imageFile));
metaData = reader.getStreamMetadata();
}
BufferedImage image = ImageIO.read(imageFile);
bufferedImages.add(new IIOImage(image, null, null));
}
ImageWriteParam params = writer.getDefaultWriteParam();
if (compressionType != null){
params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
params.setCompressionType(compressionType);
}
ImageOutputStream outStream = null;
try{
outStream = ImageIO.createImageOutputStream(new File(filePath));
int numPages = 0;
writer.setOutput(outStream);
for(IIOImage image:bufferedImages){
if (numPages == 0){
writer.write(metaData, image, params);
}
else{
writer.writeInsert(numPages, image, params);
}
numPages++;
}
}
finally{
if (outStream != null){
outStream.close();
}
}
//set up the file for us to use later
File mergedFile = new File(filePath);
logger_.info("Merged image into [" + filePath + "]");
logger_.debug("Merged images [\n" + builder.toString() + "] into --> " + filePath);
return mergedFile;
}
I hope this help someone else because I know there isn't much on this issue that I could find.
Related
I am facing a problem in finding a way using java through which I can locate location points(Lat,Long) from large CSV file to google map.
I was able to read the locations data from the big dataset but I face a problem in placing the points in streaming way to google maps.
I am not expert in programming but I started with below code:
JFrame test = new JFrame("Google Maps");
try {
String latitude = "45.714728";
String longitude = "-73.998672";
String imageUrl = "https://maps.googleapis.com/maps/api/staticmap?center="+ latitude+ ","+ longitude+ "&zoom=11&size=612x612&scale=2&maptype=roadmap";
String destinationFile = "image.jpg";
// read the map image from Google
// then save it to a local file: image.jpg
URL url = new URL(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
// create a GUI component that loads the image: image.jpg
ImageIcon imageIcon = new ImageIcon((new ImageIcon("image.jpg"))
.getImage().getScaledInstance(630, 600,
java.awt.Image.SCALE_SMOOTH));
test.add(new JLabel(imageIcon));
// show the GUI window
test.setVisible(true);
test.pack();
appreciated any help,
Thanks in advance
The dataset being huge, as you have mentioned, you shouldn't be going for URL based solution. URL has a character limit and hence beyond a point your solution won't work. You should look for some api to get you plotted locations. Check this.
If the number of points you want to plot is relatively less you can use following approach.
String center = centerLat + "," + centerLong;
String points[] = {lat1+","+long1, lat2+","+long2};//points from csv
String plottedPoints = new String();
for(String point: points) {
plottedPoints = plottedPoints + point + "|";
}
//Finally construct the url
String imageUrl = "http://maps.google.com/maps/api/staticmap?center=" + center + "&size=512x512&maptype=roadmap&sensor=false&markers="+plottedPoints;
And then read the resulting image as you have already done.
recently i'm facing problem when try to display an image file. Unfortunately, the image format is TIFF format which not supported by major web browser (as i know only Safari support this format). Due to certain constraint, i have to convert this format to others format that supported by major browser. However, it bring a lots of problem for me when i try to converting the format.
I had search through the web and although there been posted similar issue in this link How do I convert a TIF to PNG in Java?" but i can't have the result as it proposed..
Therefore i raise this Question again to wish that can have better explanation and guideline from you all..
There were few issue i'm faced during go through with the solution that proposed:
1) According to the answer that proposed by Jonathan Feinberg, it need to install JAI and JAI/ImageIO.
However, after i installed both of them i still couldn't import the file in Netbean 7.2. NetBean 7.2 remain propose import default imageIO library.
2) when i'm using default ImageIO library Read method, it will return NULL value and i cannot continue to proceed.
3) I also tried others method such as convert TIFF file to BIN File by using BufferedOutputStream method but the result file is greater than 11 MB which is too large to load and end up loading failed.
if (this.selectedDO != null) {
String tempDO = this.selectedDO.DONo;
String inPath = "J:\\" + tempDO + ".TIF";
String otPath = "J:\\" + tempDO + ".bin";
File opFile = new File(otPath);
File inFile = new File(inPath);
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(inPath), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(new FileOutputStream(otPath), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
try {
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Hence, hope that can get help and advise from you all so that i can convert TIFF format to other format such as JPEG/PNG.
Had gone through some study and testing, found a method to convert TIFF to JPEG and sorry for pending so long only uploaded this answer.
SeekableStream s = new FileSeekableStream(inFile);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
RenderedImage op = dec.decodeAsRenderedImage(0);
FileOutputStream fos = new FileOutputStream(otPath);
JPEGEncodeParam jpgparam = new JPEGEncodeParam();
jpgparam.setQuality(67);
ImageEncoder en = ImageCodec.createImageEncoder("jpeg", fos, jpgparam);
en.encode(op);
fos.flush();
fos.close();
otPath is the path that you would like to store your JPEG image.
For example: "C:/image/abc.JPG";
inFile is the input file which is the TIFF file
At least this method is workable to me. If there is any other better method, kindly share along with us.
EDIT: Just want to point out an editing in order to handle multipage tiff. Obviously you also have to handle the different names for the resulting images:
for (int page = 0; page < dec.getNumPages(); page++) {
RenderedImage op = dec.decodeAsRenderedImage(page );
...
}
Add dependency
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.3.1</version> </dependency>
https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core
https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core/1.3.1
Coding
final BufferedImage tif = ImageIO.read(new File("test.tif"));
ImageIO.write(tif, "png", new File("test.png"));
In case of many pages, working following:
add dependency:
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.4.0</version>
</dependency>
use following Java8 code
public void convertTiffToPng(File file) {
try {
try (InputStream is = new FileInputStream(file)) {
try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(is)) {
Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInputStream);
if (iterator == null || !iterator.hasNext()) {
throw new RuntimeException("Image file format not supported by ImageIO: " + file.getAbsolutePath());
}
// We are just looking for the first reader compatible:
ImageReader reader = iterator.next();
reader.setInput(imageInputStream);
int numPage = reader.getNumImages(true);
// it uses to put new png files, close to original example n0_.tiff will be in /png/n0_0.png
String name = FilenameUtils.getBaseName(file.getAbsolutePath());
String parentFolder = file.getParentFile().getAbsolutePath();
IntStream.range(0, numPage).forEach(v -> {
try {
final BufferedImage tiff = reader.read(v);
ImageIO.write(tiff, "png", new File(parentFolder + "/png/" + name + v + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
If your target is Android, you could try this great Java library on Github that provides many utilities for handling, opening, and writing .tiff files.
A simple example from that Git showng how to convert TIFF to JPEG:
TiffConverter.ConverterOptions options = new TiffConverter.ConverterOptions();
//Set to true if you want use java exception mechanism
options.throwExceptions = false;
//Available 128Mb for work
options.availableMemory = 128 * 1024 * 1024;
//Number of tiff directory to convert;
options.readTiffDirectory = 1;
//Convert to JPEG
TiffConverter.convertTiffJpg("in.tif", "out.jpg", options, progressListener);
First, take a look to What is the best java image processing library/approach?. For your code you can use
javax.imageio.ImageIO.write(im, type, represFile);
like you can see in write an image to file example.
I am a freshman in Neo4J. I think I am also a freshman in Java though I have learn it for neary 2 years.
I want to save and read a picture in neo4j database, I have a InputStream instance, Its cotent is a picture data. I have a Resoucre Object. it has a byte[] property used to save the picture data. so I do that
public static Resource getResourceInstance(InputStream in, String title) throws IOException{
StringBuilder sb = new StringBuilder();
BufferedInputStream input = new BufferedInputStream(in);
int b;
while((b = input.read()) != -1){
sb.append(b);
}
input.close();
in.close();
return new Resource(sb.toString().getBytes(), title, 0, 0);
}
then I use a transaction to save it to neo4j. and I check it by neo4j-server. in database, the byte array is number like 51,52,45 and so on
the second step I want to read the byte array from database.
I put it in Resource Object. and use FileOutputStream read it the code like this
images = resource.getImage();
String titleString = resource.getTitle();
String path = "images" + File.separator + titleString + ".jpg";
System.out.println(Paths.get(path).toRealPath());
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(path)));
out.write(images);
out.close();
this is a Java web project.
I don't know why I have to create a file in path(String path = "images" + File.separator + titleString + ".jpg";) at first.
though I do so, I can't open the file like a picture.
I am very dispirited now. and I don't konw how to do. can you help me?
thank you very much.
PS:
my english is poor,bet your tolerating.
don't do this in the first place.
store the picture on a filesystem or a public storage like s3, dropbox etc. and save the url or filename in the neo4j property.
If you want to read a file into a byte[] create an array of the file.length() size and read into that array using the right offset until is. read() returns -1
recently i'm facing problem when try to display an image file. Unfortunately, the image format is TIFF format which not supported by major web browser (as i know only Safari support this format). Due to certain constraint, i have to convert this format to others format that supported by major browser. However, it bring a lots of problem for me when i try to converting the format.
I had search through the web and although there been posted similar issue in this link How do I convert a TIF to PNG in Java?" but i can't have the result as it proposed..
Therefore i raise this Question again to wish that can have better explanation and guideline from you all..
There were few issue i'm faced during go through with the solution that proposed:
1) According to the answer that proposed by Jonathan Feinberg, it need to install JAI and JAI/ImageIO.
However, after i installed both of them i still couldn't import the file in Netbean 7.2. NetBean 7.2 remain propose import default imageIO library.
2) when i'm using default ImageIO library Read method, it will return NULL value and i cannot continue to proceed.
3) I also tried others method such as convert TIFF file to BIN File by using BufferedOutputStream method but the result file is greater than 11 MB which is too large to load and end up loading failed.
if (this.selectedDO != null) {
String tempDO = this.selectedDO.DONo;
String inPath = "J:\\" + tempDO + ".TIF";
String otPath = "J:\\" + tempDO + ".bin";
File opFile = new File(otPath);
File inFile = new File(inPath);
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(inPath), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(new FileOutputStream(otPath), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
try {
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Hence, hope that can get help and advise from you all so that i can convert TIFF format to other format such as JPEG/PNG.
Had gone through some study and testing, found a method to convert TIFF to JPEG and sorry for pending so long only uploaded this answer.
SeekableStream s = new FileSeekableStream(inFile);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
RenderedImage op = dec.decodeAsRenderedImage(0);
FileOutputStream fos = new FileOutputStream(otPath);
JPEGEncodeParam jpgparam = new JPEGEncodeParam();
jpgparam.setQuality(67);
ImageEncoder en = ImageCodec.createImageEncoder("jpeg", fos, jpgparam);
en.encode(op);
fos.flush();
fos.close();
otPath is the path that you would like to store your JPEG image.
For example: "C:/image/abc.JPG";
inFile is the input file which is the TIFF file
At least this method is workable to me. If there is any other better method, kindly share along with us.
EDIT: Just want to point out an editing in order to handle multipage tiff. Obviously you also have to handle the different names for the resulting images:
for (int page = 0; page < dec.getNumPages(); page++) {
RenderedImage op = dec.decodeAsRenderedImage(page );
...
}
Add dependency
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.3.1</version> </dependency>
https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core
https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core/1.3.1
Coding
final BufferedImage tif = ImageIO.read(new File("test.tif"));
ImageIO.write(tif, "png", new File("test.png"));
In case of many pages, working following:
add dependency:
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.4.0</version>
</dependency>
use following Java8 code
public void convertTiffToPng(File file) {
try {
try (InputStream is = new FileInputStream(file)) {
try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(is)) {
Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInputStream);
if (iterator == null || !iterator.hasNext()) {
throw new RuntimeException("Image file format not supported by ImageIO: " + file.getAbsolutePath());
}
// We are just looking for the first reader compatible:
ImageReader reader = iterator.next();
reader.setInput(imageInputStream);
int numPage = reader.getNumImages(true);
// it uses to put new png files, close to original example n0_.tiff will be in /png/n0_0.png
String name = FilenameUtils.getBaseName(file.getAbsolutePath());
String parentFolder = file.getParentFile().getAbsolutePath();
IntStream.range(0, numPage).forEach(v -> {
try {
final BufferedImage tiff = reader.read(v);
ImageIO.write(tiff, "png", new File(parentFolder + "/png/" + name + v + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
If your target is Android, you could try this great Java library on Github that provides many utilities for handling, opening, and writing .tiff files.
A simple example from that Git showng how to convert TIFF to JPEG:
TiffConverter.ConverterOptions options = new TiffConverter.ConverterOptions();
//Set to true if you want use java exception mechanism
options.throwExceptions = false;
//Available 128Mb for work
options.availableMemory = 128 * 1024 * 1024;
//Number of tiff directory to convert;
options.readTiffDirectory = 1;
//Convert to JPEG
TiffConverter.convertTiffJpg("in.tif", "out.jpg", options, progressListener);
First, take a look to What is the best java image processing library/approach?. For your code you can use
javax.imageio.ImageIO.write(im, type, represFile);
like you can see in write an image to file example.
I thought this would be a quick fun project, but it's turning into a problem. :(
I want to load some images into an Oracle table and then later retrieve them with hibernate through a servlet.
So here's the portion of the loader that inserts the image.
String imageFileName = row[col++];
String ext = imageFileName.substring(imageFileName.lastIndexOf('.') + 1);
String imageFilePath = imageDir + imageFileName;
String mimeType = "image/" + ext;
image.setImageType(mimeType);
Image found = imageDAO.retrieve(imageId);
if(found==null){
//create a new one
byte[] bytes = loadImage(imageFilePath, mimeType);
image.setImageData(bytes);
image = imageDAO.create(image);
++created;
}
else{
//check if an update is needed
if(updateDate.after(found.getUpdated())){
byte[] bytes = loadImage(imageFilePath, mimeType);
found.setImageData(bytes);
found.setUpdated(updateDate);
image = imageDAO.update(found);
++updated;
}
}
}
and here's the servlet innards:
#Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
String imageId = request.getParameter(KeyNames.PARM_IMAGE_ID);
if(imageId==null){
throw new NullPointerException("Image ID parameter is required");
}
Image image = getImageDAO().retrieve(imageId);
if(image==null){
throw new IllegalArgumentException(imageId + " is not a valid image ID");
}
response.setContentType(image.getImageType());
response.getOutputStream().write(image.getImageData());
response.getOutputStream().close();
return null;
}
Seems simple to me, but when I hit the URL using my browser, I get:
"The image [url] cannot be displayed because it contains errors"
Since the servlet is wrapping successful, I must guess that either in the loading or in the retrieving, I've corrupted the image data. I'm out of guesses as to what to do next, so any advice is appreciated.
Shame on me! This is what I get for running code off the internet without actually studying it to find out what it does!
So the answer was that I was running the image through a writable raster in order to store in the DB. Well, DUH! when you rasterize the image, it's not a png anymore.
So I simply do a byte copy from the input file and store that in the blob and it works fine.
Shoot me now.
Thanks for the help!