Zoom out an image with java - java

I have a program whereby the user will enter the url for a picture, then a program in java should zoom out the picture with a width and height which are both multiple of 135px.
Can someone help??
for e.g. this is an image 1100X1121
but i want width and the height of the image to become both multiple of 135 and less than 700px as below, now the image is 675X675 which is both a multiple of 135px and less than 700px:

I am using java-image-scaling-library for scaling and it works fine. It makes it easier to determine the width and/or height of the new image.
(Note that it is most often called "scaling")

You can try this simple Java library: Imgscalr
Or is your problem to determine the dimensions of new image?

Related

LibGDX different assets for different resolutions like Android

I have a game developed natively for Android, and now my users also want an iOS version. I thought LibGDX would be the better choice because it'll let me reuse Java code from the game, and also I already have some experience with it.
In my game I have different image sizes for different device densities (in drawable-hdpi, drawable-xhdpi and so on).
So, my question is: how can I achieve the same, but using LibGDX (also taking care of the new densities required by iOS device resolutions, if any change is required)?
Thank you.
Yes you can achieve the same, but it wont be automatic like on Android unless you write some native code as well. I have found that the best way to manage it is simply to do it yourself:
1) When your app starts you can get the screen size and density using Gdx.graphics.getHeight(), getWidth(), Gdx.graphics.getDensity()
2) Depending on the size and density you can change the location path to the correct folder where your assets should be loaded from.
3) Now when any asset loading code is run make sure that it uses your pre-set path from the step above, so that you get the correct assets for that display size/density.
Most of the time you can use the largest image and use `Viewports' to handle resolution and aspect ratio for you. The larger images will be scaled down and this will result in some loss of detail of course.
Viewports will automatically scale the size you want to show of your game world to the screen it displays it. For example FitViewport(100, 100) will create a viewport that shows 100 x 100 "game units". If you would play this on a 1920 x 1080 device it will scale that 100 x 100 game world to a 1080 x 1080 area and leave an empty bar of 840 x 1080.
The size of the game world has nothing to do with pixels. You could create a enemy with the size of 0.5f x 0.5f world units and give that a texture of 256 x 256 pixels. Your viewport scales this for you to the correct size.
Unless you want a pixel perfect game this should be good enough. On some bigger screens but low resolutions devices you might get some minor artefacts due to filtering, setting the filtering for your textures Texture.setFilter(TextureFilter.Nearest, TextureFilter.Linear) might fix some.
All I ever think about when designing graphics are the pixels in my art should represent roughly or at least 1 screen pixel. Usually I just draw pixel perfect for HD and it looks fine on a 800 x 480 screen. If you want to squeeze out a bit more performance you could use MipMaps, I think TexturePacker generates them automatically with the right Filter settings but I have no experience with them.
This can be done using
com.badlogic.gdx.assets.loaders.resolvers.ResolutionFileResolver.
Here is javadoc for it.

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.

Calculate a fitting text size

I want to calculate the size my Font needs, so my text will be displayed fully in one line without clipping.
Example
|-100px---HERE'S MY TEXT---100px-|
I have the DPI and all there stuff. Testing isn't a way, I am using Libgdx and the text ist display with an Button (Scene2D ui).
Call BitmapFont.getBounds(). The TextBounds it returns will tell you what you need. You can try a medium font, the go up or down based on how big or small the bounds are. I use this method to help scale UI sizes from old Droid 1s up to the new HD displays.
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/BitmapFont.html

How to find the largest image using Java?

I am trying to parse a html page to find the most prominent image. So, after parsing the html page to extract all img tags, i am trying to find the largest image by comparing the dimension of the image.
Is it right to compare the images by calculating the area as (width * height)?
That depends entirely on your definition of 'largest'. width * height is certainly a valid approach, but it has the flaw that a 1x1000 image is 'larger' than a 30x30 one even though the latter could very well be more noticeable. It also has the problem that a large image that's mostly the same as the background color will be more 'noticeable' than a medium image that isn't, which might not be the case.
In order to figure out how to determine how to find the 'largest' image, you need to specify why you want it.

Resize Image files

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).

Categories