resize images in android - java

I want to resize my images (original size is 1080p) but they don't resize properly and I don't know why. The Images just don't have the right size sometimes. On my emulator and my old 800*480 smartphone it works fine but on my nexus 4 with 1280*768 things don't look right. There is no problem reading the right screen resolution. There is just a bug with my resize procedure. Please help me. Heres a Snippet:
private Bitmap bitmap,bitmap1;
private float factor;
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displaymetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displaymetrics);
width = displaymetrics.widthPixels;
height = displaymetrics.heightPixels;
factor = (float)height/1080;
Int bitmapheight,bitmapwidth;
bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.picture),(int)(factor*BitmapFactory.decodeResource(getResources(), R.drawable.picture).getwidth() ,(int)(factor*BitmapFactory.decodeResource(getResources(), R.drawable.picture).getHeight(), true);
canvas.drawBitmap(bitmap,.....
In the end the height is not resized to 768/1080*bitmapheight on my nexus and i don't know why. Note everything else works. Mathmatics indicates that the height should be the same on every phone.
These are screenshots of my programm showing the images have not the same height
First image:
Second:
As you can see the Images are not equal in terms of height. On my emulator and my old smartphone they look right. The Images should not touch the bottom but on my nexus 4 they do touch the bottom.

For anyone who is interessted why it didnt work. Finally i found out.
Now
BitmapFactory.decodeResource(getResources(), R.drawable.picture)
not only loads the pictures but also scales it depending on your resolution ( not always just on some resolution) thats why it confused me. I thought it just loads the damn picture. Now how to solve this:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
BitmapFactory.decodeResource(getResources(), R.drawable.picture,opts)

Related

Getting screen width with newer phones

I'm using a Samsung Galaxy S20 phone and I'm trying to get the width of the display using:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
height2 = metrics.heightPixels;
width2 = metrics.widthPixels;
This returns 810, but seeing the phone specifications on the internet the actual screen width is 1080. I started having problems in code because of this, is there a way to get the actual screen width in Java?

How do I get the ID of a drawable and give it to a function?

I have this scrollview in which I want to inflate a 1920x1080 background image. If I add it statically via xml, my app lags in animations and general responsiveness. I checked StackOverflow and it's something that is expected to happen because images are uncompressed in memory.
Now, in the same thread linked above, a suggested answer is to use a setBackground function which uses BitmapFactory to dynamically resize and set the image as follows:
fun setBackground(context: Context, view: View, drawableId: Int) {
var bitmap = BitmapFactory.decodeResource(context.getResources(), drawableId)
val width = Resources.getSystem().getDisplayMetrics().widthPixels
val height = Resources.getSystem().getDisplayMetrics().heightPixels
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true)
val bitmapDrawable = BitmapDrawable(context.getResources(), bitmap)
view.background = bitmapDrawable
}
However, as can be seen, this image requires the drawable's ID, which I haven't been able to get. I checked similar questions and found conflicting answers with some saying that it's not possible to get drawable IDs, and other's suggesting some ImageView-related methods that aren't related to my problem (inflating a scrollview). So, can anybody please name a method in Java/Kotlin that allows me to get my drawable image's ID? If that's not possible, can you please suggest another workaround?
Have you tried:
val resID = resources.getIdentifier("drawable_name", "drawable", packageName)

Image is in landscape instead of portrait mode after scaling

I have an image and want to scale it down using imgscalr [1].
The source image's size is 1836 x 3264 (so it's portrait mode) and destination resolution is 1336 x 768.
The failure is that the image is in landscape mode and not portrait mode anymore after scaling. The scaling itself works like a charm.
My code:
BufferedImage src = ImageIO.read(new File("sourceimage.jpg"));
BufferedImage scaled = Scalr.resize(src, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_HEIGHT, 1336, 768, Scalr.OP_ANTIALIAS);
ImageIO.write(scaled, "jpg", f);
The metadata of the file looks correct (orientation = 1).
My expactation is when downscaling an portrait picture that it is still a portrait picture after the scaling process.
I also tried some other libraries/classes like this: https://frickelblog.wordpress.com/2009/06/08/fast-image-scaling-in-java/ but the effect is the same.
Can you please help me? This one drives me crazy! I'm pretty sure there is just a detail to change.
[1] http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/#maven
edit:
Here's the original image: http://i.stack.imgur.com/NEc8j.jpg
the scaled version: http://i.stack.imgur.com/SYx6p.jpg
So the problem isn't with ImgScar but with ImageIO, ImageIO won't read the orientation of the image, so it was been read in as 3264x1836 - Landscape.
So what I ended up doing was rotating the image 90 degrees...
BufferedImage src = ImageIO.read(new File("/Users/swhitehead/Downloads/original.jpg"));
System.out.println(src.getWidth() + "x" + src.getHeight());
BufferedImage rotated = Scalr.rotate(src, Scalr.Rotation.CW_90, Scalr.OP_ANTIALIAS);
BufferedImage scaled = Scalr.resize(rotated, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_HEIGHT, 1336, 768, Scalr.OP_ANTIALIAS);
System.out.println(scaled.getWidth() + "x" + scaled.getHeight());
ImageIO.write(scaled, "jpg", new File("Scaled.jpg"));
Which seems to have fixed "this" problem.
You can have a look at ImageIO.read( ) always rotates my uploaded picture for more details
Browsers implement <img> differently.
Firefox will not read the EXIF-information so portrait pictures are displayed in landscape.
Chrome does read out the EXIF-information so portait stays portrait.
I fixed it with this CSS declaration:
image-orientation: from-image;

Activity as Dialog Just On Larger Screen

I'm developing an Android app that needs to support 2.3+. I'm using HoloEverywhere as a layout library. My app runs perfectly on smartphones and tablets, but for now I'm using the same layout for all devices, so, in tablets there is a lot of blank space in a lot of layouts. So, I thinking if is possible to show this layout as a popup (AlertDialog) just on large screens.
I searched the internet for a response but every response I look isn't applying to my case. I don't want to change my layouts files (and, of course, I don't want to create new layout files).
Can anyone give a direction to solve my problem, or the unique way is to create new layouts for large screens?
You can use your own layout file as the content view of a dialog. With a custom view you can create other/bigger dialogs for your tablets. It is also possible to create a dialog from an activity. See http://developer.android.com/guide/topics/ui/dialogs.html#ActivityAsDialog or http://developer.android.com/guide/topics/ui/dialogs.html#FullscreenDialog
You can change the theme of activity to dialog in Manifest file. See:
<activity
android:name="Activity"
android:label="Home"
android:theme="#android:style/Theme.Holo.Dialog">
</activity>
You can use this code to change the height and width of my activity in onCreate()..It provides better flexibility than specifying dialog theme
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
requestWindowFeature(Window.FEATURE_ACTION_BAR);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
android.view.WindowManager.LayoutParams params =
getWindow().getAttributes();
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
params.height = LayoutParams.WRAP_CONTENT;
params.width = (int) (width / 1.2); // fixed width
params.alpha = 1.0f;
params.dimAmount = 0.5f;
getWindow().setAttributes((android.view.WindowManager.LayoutParams)
params);

displaying a custom map and placing pin/images on the map

i wanted to create a directory app for a grocery. i already have the floor plan of the place. i just wanted to display it in a view and i want to put some markers/pins/images to it.. i have no idea where to start. someone told me to use bitmap and canvass but i have no idea how to use it as the other tutorials in google is quite vague and hard to understand..
what my app requires:
display a custom map from an image source
enable me to put markers,pins and some images on top of it
A simple way is to use use ImageView:
//make a bitmap from your floorPlanImage, example from file
Bitmap floorPlanImage = BitmapFactory.decodeFile(filePath, options);
//make a mutable bitmap, so it can be changed
Bitmap mutableFloorPlanImage = floorPlanImage.copy(Bitmap.Config.ARGB_8888, true);
//create a canvas to draw on your floorPlanImage
Canvas mCanvas = new Canvas(mutableFloorPlanImage);
//draw a marker or another image on the floorPlanImage, you can position it using left and top //coordinates, in this example both 0
mCanvas.drawBitmap(markerBitmap, 0, 0, null);
//show the resulting bitmap in ImageView
imageView.setImageBitmap(mutableFloorPlanImage);

Categories