I want to create thousands of dynamic images with Java. I've created a JFrame and a LayeredPane and put a JLabel on this. Then I am writing image contents in this JLabel. Then I create this image of this LayeredPane like this
BufferedImage image = (BufferedImage)comp.createImage(width, hight);
It creates the image perfectly but its size is 20KB (more or less). Can I reduce the image size without losing its quality. I have just text and colour in JLabel.
OR
Is there another way to create images based on Text?
Thanks in Advance
You can reduce its size on disk by saving as JPEG and reducing the Q. This has least impact on quality. Resampling has major impact.
For Text-only images containing only a little number of colors, storing them as GIF files (with a palette of only 2 or 3 colors) could be a lot smaller that JPEG, which is optimized for storing full-color pictures. I would start fiddling around with Paint.NET or some other tool to find out what's possible and which format is the best, before programming the Image-to-file-encoding.
Related
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?
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.
I would like to convert a string of text into an image. The issue is, I want the text to wrap if it is wider than the length of the image, and the height of the image to be dynamically sized to perfectly fit the text, so that I know how much space the text takes up.
I'm working in Java and there are several things I have tried:
Rendering HTML in a JPanel and saving as a BufferedImage. The problem here was that most of the css I used was ignored by the JPanel and the image was unusable.
Using ImageMagick and img4Java. The two big failures with this solution was that I needed the command-line tool installed, which I can't do on our server. The second was that I couldn't easily convert the image to buffered image for use in the rest of the app.
Does anyone know a way to do this in Java?
Thanks!
In this example, an arbitrary panel is rendered into a BufferedImage and displayed in an adjacent panel at half-scale. The example uses a grid of labels, but you can use the wrap feature of JTextArea or the geometry supplied by TextLayout, examined here.
You might use a label containing HTML for the line-wrap, as shown here.
To get an image of that, see LabelRenderTest.
Coming from AWT/Swing, I've started experimenting a bit with JavaFX the last few days. I realized that what I used to do in thousands lines of codes can now be done in a few hundred.
One problem I came across is, however, the following: I'm trying to develop a small painting app where the user can choose brush size and color for its strokes. For all the strokes the user makes, I use the JavaFX class Path and add these paths to a Group (which is added to a Pane) where they are - automagically - painted. Now I want to store the resulting image as a jpg and try to raster all the paths in a BufferedImage. However, I found no functions in the API that help me do that.
I tried to use Canvas and its GraphicsContext, but that did not help. How could I raster all the JavaFX Paths from a list on an image?
Take a snapshot of your Group to get a JavaFX Image.
Use SwingFXUtils to convert your JavaFX image snapshot to a buffered image.
Use ImageIO to convert your buffered image to a jpeg, png, etc
I have a lot of images that taken by my Digital camera with very high resolution 3000 * 4000 and it takes a lot of Hard disk space, I used Photoshop to open each Image and re-size it o be with small resolution, but it needs a lot of time and effort
I think that I can write simple program that open the folder of images and read each file and get it's width and height and if it's very high change it and overwrite the image with the small one.
Here some code I use in a Java-EE project (should work in normal application to:
int rw = the width I needed;
BufferedImage image = ImageIO.read(new File(filename));
ResampleOp resampleOp = new ResampleOp(rw,(rw * image.getHeight()) / image.getWidth() );
resampleOp.setFilter(ResampleFilters.getLanczos3Filter());
image = resampleOp.filter(image, null);
File tmpFile = new File(tmpName);
ImageIO.write(image, "jpg", tmpFile);
The resample filter comes from java-image-scaling library. It also contains BSpline and Bicubic filters among others if you don't like the Lanczos3. If the images are not in sRGB color space Java silently converts the color space to sRGB (which accidentally was what I needed).
Also Java loses all EXIF data, thought it does provide some (very hard to use) methods to retrieve it. For color correct rendering you may wish to at least add a sRGB flag to the file. For that see here.
+1 to what some of the other folks said about not specifically needing Java for this, but I imagine you must have known this and were maybe asking because you either wanted to write such a utility or thought it would be fun?
Either way, getting the image file listing from a dir is straight forward, resizing them correctly can take a bit more leg work as you'll notice from Googling for best-practices and seeing about 9 different ways to actually resize the files.
I wrote imgscalr to address this exact issue; it's a dead-simple API (single class, bunch of static methods) and has some good adoption in webapps and other tools utilizing it.
Steps to resize would look like this (roughly):
Get file list
BufferedImage image = ImageIO.read(files[i]);
image = Scalr.resize(image, width);
ImageIO.write(image);
There are a multitude of "resize" methods to call on the Scalr class, and all of them honor the image's original proportions. So if you scale only using a targetWidth (say 1024 pixels) the height will be calculated for you to make sure the image still looks exactly right.
If you scale with width and height, but they would violate the proportions of the image and make it look "Stretched", then based on the orientation of the image (portrait or landscape) one dimension will be used as the anchor and the other incorrect dimension will be recalculated for you transparently.
There are also a multitude of different Quality settings and FIT-TO scaling modes you can use, but the library was designed to "do the right thing" always, so using it is very easy.
You can dig through the source, it is all Apache 2 licensed. You can see that it implements the Java2D team's best-practices for scaling images in Java and pedantically cleans up after itself so no memory gets leaked.
Hope that helps.
You do not need Java to do this. It's a waste of time and resources. If you have photoshop you can do it with recording actions: batch resize using actions
AffineTransformOp offers the additional flexibility of choosing the interpolation type, as shown here.
You can individually or batch resize with our desktop image resizing application called Sizester. There's a full functioning 15-day free trial on our site (www.sizester.com).