Image getting printed with low resolution - java

I have a requirement to create pdf with images for printing. I am adding a high resolution image to pdf using iText. It's reducing the quality to 72 DPI.
Resulting in poor quality of image after printing.
The original image resolution is 2549*3304 and DPI (300).
I tried below options
image.scaleAbsolute(2549*.24 ,3304*.24 );
image.setDpi(300,300);
image.scaleToFit(2549*.24 ,3304*.24 );
current code looks like this
Document document = new Document(PageSize.LETTER);
document.open();
Image image = Image.getInstance("C:/Project/bg.png");
image.setAbsolutePosition(0,0);
image.scalePercent(24);
document.add(image);
document.close();
I went through some threads (Adding an image to a PDF using iTextSharp and scale it properly) still not able to solve the problem
Can anyone please help me on this to get better image quality while printing?

The setDpi() method is irrelevant in your code, so is the DPI of your original image. Pixels are treated as points in iText. This means that adding an image as-is will result in having 72 pixels per inch.
You scale the image to 24 percent of the original size. This means that you increase the resolution: you are showing the same number of pixels using only 24% of the space. In this case, you are showing 2549 pixels distributed over 611.76 points. This is about 8.5 inch, which means you indeed have a resolution of 300 DPI.
I think the problem isn't caused by the resolution of the image inside the PDF (but it's hard to tell because we can't inspect the PDF). I think the problem is caused by the printing process that prints the document using a resolution that is different from the resolution in the PDF.
In any case: iText does not reduce the number of pixels if you use the methods scalePercent(), scaleAbsolute() or scaleToFit().
Extra info based on Comments:
Asking a PDF for its "resolution" doesn't make sense, because a PDF doesn't have any resolution (although images inside a PDF may have one). I have no idea why Photoshop tells you the resolution is 72 DPI. Maybe that's a default value because the measurment unit in PDF corresponds with a point and there are 72 points in one inch.
I have examined the PDF that you shared. I don't see any loss of resolution when I look at it on my screen. I can see that the document measures 8.5 by 11 inch. As for the image, please take a look at the report generated by Acrobat:
It says width/height: 2550/3300 in pixel.
2550 / 8.5 = 300
3300 / 11 = 300
Hence the resolution is 300 pixels per inch. Or: the PDF is created exactly the way you want it.
However: you say that the resolution is worse when you print the document. This can be caused by many different things: maybe you're printing on a page that is larger than 8.5 by 11 inch, maybe the printer can't print at that resolution, maybe the PDF viewer can only print using "degraded printing",...
My advice is that you test this PDF on different printers using different viewers to find the culprit.

Related

PDFBox extracted image is much bigger than original page

I have an image-only PDF file that looks like a scan of a really big page. Preview shows me that it is about 42x30 inches, and 3047x2160 pixelss. I guess it was scanned at 72dpi resolution.
I'm extracting this image with PDFBox by looking for instances of PDImageXObject, similar to https://stackoverflow.com/a/37664125/10026.
However, for this image, PDImageXObject.getWidth() and PDImageXObject.getHeight() give me 16928 and 12000, respectively. When I call PDImageXObject.getImage(), it creates an enormous BufferedImage in memory.
Is there a better way to get the image out of so that it keeps the original pixel size?

Improving jpeg image quality using scaling in java

I am generating a jpg image using phantomjs. I want image with 600x400px dimension and 144 dpi/ppi.
But I am only able to generate a image with 72dpi. So, I am trying to generate the image with 1200x800px dimensions in 72 dpi and then resizing it to 600x400px.
so my idea is,
1200x800 px has dpi 72
so,
600x400px will have dpi 72*2 = 144 (because 1200/2=600px , 800/2=400px )
I am using java. But whenever I resize my image the quality becomes less.
Approaches I tried :
I tried to manipulate the metadata information of image as explained in the post below, but still it gives image with poor quality.
Increasing Resolution and Reducing Size of an Image in Java
Write dpi metadata to a jpeg image in Java
I tried using AffineTransform as given in following post, still not satisfied with results:
Java image scaling improve quality?
Am I using a wrong approach? If yes, then please suggest some alternatives.
If the approach is right then please guide me on how to implement it correctly?
UPDATE
I don't want to reduce the no. of pixels. I want to keep the no. of pixels same but reduce the height/width of image while increasing the dpi.
example, In an image of 1200x800px having 72 dpi, I will reduce it to 600x400 px while increasing dpi to 144 .
So , if you see the no of pixels are same and the image has higher dpi value.
Finally solved it using itext renderer. Refer to the following post on
using itext for scaling the large image to increase resolution.
Image Quality using Itext
I solved it using Itext.
File file = new File("demo.png");
Image img = Image.getInstance(file.getName());
img.scaleAbsolute(width, height); //where width,height is the reduced width,height; in my case 600,400.
Read it through Image class and use scaleAbsolute().
For more details please read this post:
Image Quality using Itext
PS: This is only helpful if you are trying to put image in pdf.
If anyone knows how to convert img to a png file then please comment.

Jpeg to Svg and Image Tracing

Currently we have a requirement where we have an image depicting the blueprint of the mall (red specifies the booked up areas and white specifies the available areas) and the image is available in a raster (JPEG) format.
We would like to drag and drop some icons onto the available areas of the image (in white). There should also be zoom in and zoom out functionality to be given for the above image as well
Since the JPEG has a lossy scaling, zooming after a certain limit can result in a jagged image. One proposed solution is to convert the image to SVG (Scalable Vector graphics).
Going with the expanded form of SVG, it simply tells us that image is:
s=>scalable (i.e. you can zoom to any level without compromising the quality)
v=>vectorized (i.e co-ordinates are available)
So by simply looking at the XML format of the image, we can predict whether to allow dropping an object at fill=red or fill=white where red and white are the two colors in the image. This might not be appropriate solution, but I'm just guessing it this way
Now the problems I see with this approach is:
Converting an image with some open source tool (InkSpace) - if we trace it with ink-space, which uses portace inside it to trace the image, it can handle only black and white colors.
Note-: Most of the tools comes with some license.
Problem with inkspace is that it embeds the image into the SVG map and does not create the co-ordinates. If you trace it with inkspace, it only creates the outline of the image.
Converting it with some online utility - Not recommended in our case, but doing so results in a large size of the SVG image. For a 700 KB file, the SVG generated is about 39 MB, which when opened up on a browser crashes the browser.
Most of the time when the image is converted to an SVG, it becomes way too large a big factor to worry about. There are utilities available like Gzip to compress files, but this is a two way route - first you convert, then you compress.
Using delinate (which employs a portace and autotrace engines in it) - the quality of the image produced is not good.
Using Java code - Again the quality suffers. Java graphics are not fully developed to handle the conversion (size is again way too large)
Converting the image to PDF, then to SVG - this also embeds the image into the SVG file, which is useless as no co-ordinates are available
Does anybody got any idea on this ,how to deal with this situation?,Can we handle the drag and drop on raster(jpeg,png...etc) images itself?
Thanks
Dishant Anand

JPanel to be set to A4 size

I'm creating a text editor in Java that will have output to PDF. In order to sync data that appears in my program and the output PDF as close as I can, I'd have to have a JPanel which has the same size as an A4 paper (or at least to be in scale). I have tried converting its mm dimension (297x210) to pixels, but opening a regular size A4 document in PDF results in a bigger page than my JPanel.
Would certain size in pixels match the size of every document (.doc,.pdf,etc) created in A4 size (displaying it at 100%), or are there variations from program to program?
I'm trying to make sense of the whole conversion deal... Do I have to visually match the size in the Adobe reader, or is there some kind of factor that you multply with page size in inches or milimeters? Is there anyone who knows how does the whole page size format deal works?
In my experience, there is no such thing as a printer standard. The behavior of printers or software like Adobe Acrobat Reader vary dramatically. Some might automatically shrink your A4-sized panel to fit in a single page with margins while others might print off 4 pages with a single pixel column on page two and a single pixel row on page 3.
Unfortunately you must go about it by trial and error. Make it work for Acrobat Reader and then try printing it out and seeing if it comes out the same. It helps to provide any and all hints as to how to format the page. Lacking these hints, much of this software tends to guess about what your intentions are.
have look at 2D Graphics tutorial an to take Graphics to the BufferedImage, becasue direct output from Swing GUI to creates 1pixes == 1 DPI
there are
Working with Images
Printing
a few examples
From my experience all printable sizes use 72 pixels=1 inch based measurements. There could be difference because default win DPI=96.

Full Resolution Camera Access in j2me

I'm trying to do an image capture on a high end Nokia phone (N95). The phone's internal camera is very good (4 megapixels) but in j2me I only seem to be able to get a maximum of 1360x1020 image out. I drew largely from this example http://developers.sun.com/mobility/midp/articles/picture/
What I did was start with 640x480 and increase the width and height by 80 and 60, respectively until it failed. The line of code is:
jpg = mVideoControl.getSnapshot("encoding=jpeg&quality=100&width=" + width + "&height=" + height);
So the two issues are:
1. The phone throws an exception when getting an image larger than 1360x1020.
2. The higher resolution images appear to be just smoothed versions of the smaller ones. E.g. When I take a 640x480 image and increase it in photoshop I can't tell the difference between this and one that's supposedly 1360x1020.
Is this a limitation of j2me on the phone? If so does anyone know of a way to get a higher resolution from within a j2me application and/or how to access the native camera from within another application?
This explanation on Nokia forum may help you.
It says that "The maximum image size that can be captured depends on selected image format, encoding options and free heap memory available."
and
"It is thus strongly adviced that at least larger images (larger than 1mpix) are captured as JPEG images and in a common image size (e.g. 1600x1200 for 2mpix an so on). Supported common image sizes are dependent on product and platform version."
So I suggest you to take some tries
1. with 1600x1200, 1024x768 and whatever image resolution your N95 guide mentions
2. with BMP and PNG as well.
Anyway, based on my earlier experiences (that could be outdated), j2me implementations are full of bugs, so there may not be a working solution to your problem.
Your cameras resolution is natively:
2582 x 1944 . Try capturing there to see how that goes.
This place:
http://developers.sun.com/mobility/midp/articles/picture/index.html
Mentions the use of:
byte[] raw = mVideoControl.getSnapshot(null);
Image image = Image.createImage(raw, 0, raw.length);
The use of raw seems interesting, to get the native resolution.
The 'quality' of a JPEG (As interpreted by the code) is nothing to do with the resolution. Rather it is to do with how compressed the image is. A 640x480 image at 100 quality will be noticably better looking than a 640x480 image at 50, but will use more storage space.
Try this instead:
jpg = mVideoControl.getSnapshot("encoding=jpeg&quality=100&width=2048&height=1536");

Categories