image upload resolution problems - java

when i am uploading an image with image io it is converting the resolutions of the image
what i mean to say is i am uploading a image with 300 dpi it get converts into 96 dpi why?
i am not using any resizing

Guess, you don't copy image metadata, here goes simplistic example how this can be done:
ImageInputStream iis =ImageIO.createImageInputStream(new File("test.jpg"));
ImageReader reader = (ImageReader) ImageIO.getImageReaders(iis).next();
reader.setInput(iis, true);
IIOMetadata meta = reader.getImageMetadata(0);
BufferedImage image = reader.read(0);
/*
do image manipulations here
*/
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("out.jpg"));
ImageWriter writer = ImageIO.getImageWriter(reader);
writer.setOutput(ios);
writer.write(meta, new IIOImage(image, null, null), null);
Since you've not provided any specific details, i've tested it on local files. However, i hope that it provides you a hint.

Related

How to convert PDF (it contains only tiff Images) to JPG Image in java

When I try to convert a PDF (330 KB contains 3 tiff images) to JPG images each image is converted to very high size, total images is more than 1MB. How can one reduce the size back to 330 KB with good or high JPG image quality?
Extracting each PDF page and passing to jpgwriter.
PDPage pages = document.getPage(page);
BufferedImage bim = pdfRenderer.renderImage(page);
ImageWriter jpgWriter =
ImageIO.getImageWritersByFormatName("jpg").next();
ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpgWriteParam.setCompressionQuality(0.7f);
ImageOutputStream outputStream = createOutputStream();
jpgWriter.setOutput(outputStream);
IIOImage outputImage = new IIOImage(image, null, null);
jpgWriter.write(bim, outputImage, jpgWriteParam);
jpgWriter.dispose();
We have new methods and service providers which provide this feature as SAAS. PDF4me is best among them. You can use their JAVA library to do this easily. Please refer https://developer.pdf4me.com/docs/api/basic-functionality/create-image/ to get the library and to use it in JAVA.
--Thanks

Write GeoTIFF metadata from one file to other

My task is to take one GeoTIFF, make some image segmentation on in, and save it to new GeoTIFF(with existing coordinates). If I understand correctly, the coordinates are preserved in GeoTIFF metadata.
So I grab metadata from the original file:
File file = new File(inputFilePath);
ImageInputStream iis = ImageIO.createImageInputStream(file);
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
IIOMetadata metadata=null;
ImageReader reader=null;
if (readers.hasNext()) {
// pick the first available ImageReader
reader = readers.next();
// attach source to the reader
reader.setInput(iis, true);
// read metadata of first image
metadata = reader.getImageMetadata(0);
}
And when I do
System.out.println("Metadata: "+metadata);, I see the correct XML tree of metatags.
So I'm do some magic with image
System.out.println("Starting segmentation");
BufferedImage image = UtilImageIO.loadImage(inputImage);
// Select input image type. Some algorithms behave different depending on image type
ImageType<MultiSpectral<ImageFloat32>> imageType = ImageType.ms(3, ImageFloat32.class);
ImageSuperpixels alg = FactoryImageSegmentation.fh04(new ConfigFh04(500, 30), imageType);
// Convert image into BoofCV format
ImageBase color = imageType.createImage(image.getWidth(), image.getHeight());
ConvertBufferedImage.convertFrom(image, color, true);
// Segment and display results
performSegmentation(alg, color);
System.out.println("Segmentation finished");
In result I obtain a BufferedImage(resultBufferedImage) with successfully image segmentation.
And here starts my problems, I'm trying to save this BufferedImage with old metadata:
BufferedOutputStream out;
ImageWriter writer = ImageIO.getImageWriter(reader);
ImageOutputStream imgout = null;
FileOutputStream fos =null;
fos = new FileOutputStream(outputImage);
out = new BufferedOutputStream(fos);
imgout = ImageIO.createImageOutputStream(out);
writer.setOutput(imgout);
ImageWriteParam param = writer.getDefaultWriteParam();
IIOImage destIIOImage = new IIOImage(resultBufferedImage, null, metadata);
System.out.println("Before write");
writer.write(null, destIIOImage, null);
System.out.println("After write");
I get printed "After write". But program is still running, I tried to wait, but no results. So when I kill process the file is created successfully, even with geodata. How can I determine the finish of writing and stop program?
p.s. Image in default Ubuntu viewer seems to be nice, but when I opened it in QGIS I have transparent fields, and how can I make gray background transparent?
Not a real answer, but here's two answers on how to make a TIFF transparent:
QGis problem with raster transparent
How to make transparent the background of a topographic map in QGis 1.8.0?

antialiasing in java image scaling library

I am currently using http://code.google.com/p/java-image-scaling/ this library to generate scaled images for my web app.But when I scale down the image to about 100x100 size there are some leftover artifacts visible in some images. Is this an issue with antialiasing? And how do I use antialiasing with this library.The api documentation doesn't say any thing about it.
Here is the code
File f = new File("C:\\Users\\ad min\\Pictures\\30-whisky-3d-wallpaper-1152x864.jpg");
BufferedImage src = ImageIO.read(f);
//ResampleOp resampleOp = new ResampleOp(76, 76);
ResampleOp resampleOp = new ResampleOp(200,200);
resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.VerySharp);
BufferedImage rescaled = resampleOp.filter(src, null);
ImageIO.write(rescaled, "JPG", new File(
"C:\\Users\\ad min\\Pictures\\scaleddown.jpg"));
what am I doing wrong?
I finally didn't need antialiasing I simply used this code given in the foloowing link and it worked :) whewww
http://www.universalwebservices.net/web-programming-resources/java/adjust-jpeg-image-
compression-quality-when-saving-images-in-java
Iterator<ImageWriter> iter = ImageIO
.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter) iter.next();
// instantiate an ImageWriteParam object with default compression
// options
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(1); // an integer between 0 and 1
// 1 specifies minimum compression and maximum quality
File file = new File("C:\\Users\\ad min\\Pictures\\scaleddown.jpg");
FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image = new IIOImage(rescaled, null, null);
writer.write(null, image, iwp);
writer.dispose();

Reduce JPEG image size without scale its width/height by Java

I want to reduce one jpeg image size(3M reduce to 1M) by Java, without scale(no change for image height and width). IN this site, I could not find a solution. Below is what I have tried:
1 Using ImageIO:
BufferedImage image = ImageIO.read(inputFile);
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
if(iter.hasNext()){
writer = (ImageWriter) iter.next();
}
ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile);
writer.setOutput(ios);
ImageWriteParam iwParam = writer.getDefaultWriteParam();
iwParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwParam.setCompressionQuality(compressionQuality);
writer.write(null, new IIOImage(image, null, null ), iwParam);
For solution 1, I set compressionQuality for jpg but I can not obtain origina image compressQuality and the newImage I get sometimes is bigger than originals.
The compression quality used is not stored with the JPEG image.
If you need to get below a certain threshold you must try several times while lowering the compression quality each time until you reach your limit. Be aware that very low settings give bad images.
I am unfamiliar with the MODE_EXPLICIT flag. It might also be a tunable parameter.
I found the following code example for reducing the quality.
The important part is just to set iwp.setCompressionQuality. Hopes this helps.
BufferedImage bi = null;
bi = ImageIO.read(new File("image.jpg"));
Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter) iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// reduced quality.
iwp.setCompressionQuality(0.1f);
File file = new File("c:/image_low.jpg");
FileImageOutputStream output = null;
output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image = new IIOImage(bi, null, null);
writer.write(null, image, iwp);
writer.dispose();

How to save jpeg Image file size after read--rotate-write operations in Java?

Im trying to read a JPEG image as BufferedImage, rotate and save it as another jpeg image from file system. But there is a problem : after these operations I cannot proceed same file size.
Here the code
//read Image
BufferedImage img = ImageIO.read(new File(path));
//rotate Image
BufferedImage rotatedImage = new BufferedImage(image.getHeight(),
image.getWidth(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2d = (Graphics2D) rotatedImage.getGraphics();
g2d.rotate(Math.toRadians(PhotoConstants.ROTATE_LEFT));
int height=-rotatedImage.getHeight(null);
g2d.drawImage(image, height, 0, null);
g2d.dispose();
//Write Image
Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter)iter.next();
// instantiate an ImageWriteParam object with default compression options
ImageWriteParam iwp = writer.getDefaultWriteParam();
try {
FileImageOutputStream output = null;
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(0.98f); // an integer between 0 and 1
// 1 specifies minimum compression and maximum quality
File file = new File(path);
output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage iioImage = new IIOImage(image, null, null);
writer.write(null, iioImage, iwp);
output.flush();
output.close();
writer.dispose();
Is it possible to access compressionQuality parameter of original jpeg image in the beginning. when I set 1 to compression quality, the image gets bigger size. Otherwise I set 0.9 or less the image gets smaller size. How can i proceed the image size after these operations?
Thank you,
What do you mean - do you want to read the compression rate of the original file?
Please note that even with the same compression settings, the filesize might vary, due to the JPEG compression algorithm. So a rotated image does not always have the exact same size as the unrotated/original version, even if all options (like compression rate, quality settings etc.) are the same.
If there is really now way to read the compression quality from the metadata, as a last resort, what you could do is use a binary search for the quality. Start with 0.5, if the file is too small try 0.75 and so on (up to 5 tries or so). This is a bit slow of course, but depending on your use case it might be OK.

Categories