Image Autoscale to fit width of a container - java

I am trying to create a google+ like app were a post contains an image. I use a box layout as the container for the image, but the image does not take up the width of the parent and auto scale the height to preserve the pixel aspect ratio on android as in google plus. I can achieve this on the browser using the css rule by setting the max-width of the image to 100%.
To better understand this, see the code below
private void loadImageArea(){
BundleContext context=getBundleContext();
UIBuilder builder=context.getUIBuilder();
Container boxHeader=(Container) builder.findByName("contentWrap", getForm());
Container imageWrap=(Container) builder.findByName("imageWrap", getForm());
Label imageLabel=new Label();
imageLabel.setUIID("ImageLabel");
Image icon=getImage();
ImageIO io;
URLImage img;
Image dst=null;
int containerWidth=Display.getInstance().getDisplayWidth()- boxHeader.getStyle().getPadding(Component.LEFT)-boxHeader.getStyle().getPadding(Component.RIGHT);
int width=icon.getWidth();
if(width>containerWidth){
Log.p("Container width: "+containerWidth);
width=containerWidth;
dst=icon.scaledWidth(width);
}
imageLabel.setIcon(dst);
imageWrap.removeAll();
imageWrap.addComponent(imageLabel);
}
1)On the simulator it works after I calculate and set the scaled width of the image as the parent container returns a width of zero. Also Scaling is very poor and pixelated. I am trying to implement some filter algorithms on top of Pisces for better results, not sure of performance and memory, but would try
2)On Android ,Why is the image not using the set width and and scaling accordingly. As you can see from the output, it is centralized and down scaled.
Kind Regards!

You can set the image as bgImage and select the option background behavior as scale to fit which will preserve aspect ratio while showing the whole image in the available space of the component.
You can also just get a scaled version of the image using the appropriate methods of the Image class.

Related

ImageView Scaling for No Apparent Reason

I'm using Android Studio, and have created an ImageView in activity_mail.xml. The layout width and height are set to "match_parent". The scaleType is set to "center" and the source image is 70x70 pixels. When I run the app in the emulator the Image appears on the screen larger than 70x70 pixels. I mean it takes up more than 70x70 pixels on the screen of the phone. I don't know why it's doing this. The app is exclusively in Landscape mode, that might be relevant. Forgive me if I have included extraneous details, I genuinely don't know what information would be relevant to include. I intend to make it so that the image takes up 70x70 pixels on the screen of the phone. What is causing the unintended result, and how could I fix it?
change ScaleType, maybe fit_center instead of just center + android:adjustViewBounds="true” will fit your purposes... another way is to create ImageView with wrap_content sizes placed in some container (e.g. RelativeLayout) with match_parent sizes
also check this visual guide for ScaleType
note that 70px image will be big on devices with HD resolution and significantly smaller on those with e.g. full HD. you should have few versions of your image in proper density buckets (mdpi, hdpi etc.) or just download proper size if your image comes frome some API, so then you can say that you have image with 70dp dimension, not 70px

Optimal solution for Image scaling where i need to preserve aspect ratio and retain good quality

So here is the problem: I am loading an image which is of unknown resolution. I need to scale it to 500px width to fit within my UI, preserving aspect ratio. The image quality must be good as i need to use it later, specifically to calculate an angle between two objects within the image.
So far i have tried the following:
In the following examples, we have an existing UI element ImageView imageView;
Method one: using scale on load in JavaFX image constructor:
public void setImage(String path){
Image image = new Image(path, 550, 412, true, true);
imageView.setImage(image);
}
This method achieves the required scaling, but lacks the quality required for an accurate calculation of angle later on.
Method two: using ImageView scaling to scale the displayed image:
public void setImage(String path){
Image image = new Image(path);
imageView.setFitWidth(550);
imageView.setPreserveRatio(true);
imageView.setImage(image);
}
This achieves the required quality and scaling, but the image contained within the view is not actually scaled, which means the calculations done later down the line are using an image not representative of the one being displayed.
Is there a way i can alter either of these solutions to work better or is there a better approach i can take? thank you.
You will have to use,
imageView.setSmooth(true);
to achieve better quality image after scaling.
EDIT:
It is because, the two methods are essentially different. The first method loads a bad quality image in the first place (with lesser pixels). One thing you can try is, to use AWT Image class for loading the image, using ImageIO, and call scaledImage = image.getScaledInstance(width, height). Then use something like (pseudo code):
create bufferedImage of scaled size
bufferedImage.createGraphics()
.drawImage(scaledImage, 0, 0, null)
to get scaled AWT Image. Then use SwingFXUtils to convert to image for FX application. This whole thing can probably be done in 3 lines of code by chaining the methods. I was able to achieve better quality this way in one of my previous projects.

Which image formats can be resized in JavaFX

There are about 100 jpeg & png color images used in our JavaFX-built desktop app which, when the window is resized, become stretched and blurry so I'd like to have all the graphics remade in a format that will allow them to be dynamically resized without losing quality. What image format or procedure should be used to do this?
Currently, each image is simply in an ImageView and resized as follows, but I'm open to other suggestions:
if(isSmall){
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
double sh = visualBounds.getHeight();
Scale scale = new Scale(sh, sh, 0, 0);
root.getTransforms().setAll(scale);
}
As has already been mentioned SVG is probably the way to go for you. JavaFX does not support SVG directly but you can find support here
javafxsvg and here svg-to-fxml-converter for example.
You can't resize an image to be bigger than it is without it getting blurry for most common formats. Instead make sure your images are big enough so you only need to downscale them.
The only format I ever heard of that could upscale further was using fractal compression, but AFAIK it is not in common use.

Splash screen which fills the whole samrtphone screen, but not crop

I want to have a splash screen, something like a full picture, which doesn't crop in heigth or width on different smartphone screens.
Now I achieved a splash screen with android:scaleType="fitXY", but now the image is cropped on top or bottom or if the devices screen size changes to another aspect ratio it is cropped on the left and right.
What do I have to do? I've already read the android developer article Supporting Multiple Screens, but I don't get it how to achieve this.
A simple picture in the middle of the screen is just simple to get, but a picture which fills the screen is hard to get. Can you help me pls?
you should use center_crop per this purpose. From the doc
Scale the image uniformly (maintain the image's aspect ratio) so that
both dimensions (width and height) of the image will be equal to or
larger than the corresponding dimension of the view (minus padding).
There is no way to create one single asset and expect it to do not be cropper and to do not create black areas when the application is deployed in different screen sizes.
The android platform is designed to work dynamically with multiple screen sizes that any manufacture can change at any time, including new resolutions that you haven't thought about it yet.
Android can specify minimums for screen hight/width categories in which your resources will fall, but those are generics.
In order to use them, you will have to specify qualifiers in your drawables and create a different splash screen for every qualifier, as for example if you use drawable-w420dp, all the resources there will be used when the screen has a minimum width of 420dp (notice that are not pixels)
So you have two options:
You can use one single splash image and design margins of that image flexible enough in order to cope with the image being cropped in certain cases. You can play with different scaleTypes in your ImageView and take as a reference this website http://etcodehome.blogspot.co.uk/2011/05/android-imageview-scaletype-samples.html even though as commented before, "center-crop" will be your best shot.
You can programatically use a specific image for a specific resolution.
2.1 Put in the assets directory, all the splash images that you want for all the specific resolutions or aspect ratios that you want to use
2.1 Get the screen size of the device with Get screen dimensions in pixels
2.2 Now you can load from the assets the image that you want dynamically
Use the below code
android:layout_width="match_parent"
android:layout_height="match_parent"
which will fill the entire screen.
Try Using Width and Height of image to "match_parent"

how to scale screenshot with out losing quality?

Now I am developing photography app for that I am developing pinch zooming and all apply effects selected image this is my code:
view = new SandboxView(getApplicationContext(), bmp);
And am adding this view to my frame layout. I am tacking screenshot of this layout for saving purpose,but it will gives based on screen width and screen height but I want with out losing image quality with 500*500 image.
What do you mean exactly when you say "without losing quality"? The only thing you can do is to use a smooth (but slow) scaling method. But when you scale down your image will lose information (that's a mathematical fact) and therefore you will lose quality. A good scaling algorithm will be able to show a picture that still looks nice to a human but there will be a loss.
And when you scale it to a bigger size using whatever scaling algorithm it will either look unsharp or bricky. There you do not lose information but the visual quality is not as you would expect it from a native image of the upscaled size.
So you only can save the original image and scale it to whateve size as soon as somebody wants to see it.

Categories

Resources