I'm writing a custom image crop.
I have an image view and a rectangle to choose area for cropping on it.
After cropping I need to know a top-left point of rectangle, that present cropping area.
But when i try to get it, Rect gives me a X, Y and width coordinates of the screen, not image. How can I get a real coordinates of the image?
I guess you could apply a simple proportionnality rule using the actual size in pixels of the device screen (assuming the image is displayed in full screen). You can easily get the screen size using this very complete answer: https://stackoverflow.com/a/1016941/1417179
From there, if your image dimensions is wh and the screen dimensions wshs, you can get the coordinates in image space using image_coordinates = screen_coordinates*w/ws.
You may have to cheat a little if the image displayed is not of the same aspect ratio than the screen, but the idea remains the same.
Hope this helps!
Related
When dealing with the PreviewView "FILL" scaling types (e.g. PreviewView.ScaleType.FILL_CENTER), the docs seem to imply that the image will not be scaled if it is higher resolution than the view.
Scale the preview, maintaining the source aspect ratio, so it fills the entire PreviewView, and center it in the view
In cases where the image is lower resolution than the view, I can understand why the image is scaled, as otherwise there would have to be black borders (i.e. would not fill the entire view), which FILL is guaranteeing will not be displayed. In cases where the image is higher resolution than the view, the preview image already fills the entire view (i.e. doesn't need to show black borders) so my assumption would be that the preview does not need to be scaled.
For example, your image is 1920x1080 and your view is 800x800, I would expect PreviewView to not scale the image and just crop an 800x800 square out of the centre of the existing image (using FILL_CENTER).
However when looking into the code I could not find any condition which actually stops scaling from happening when the resolution is higher.
For example:
preview.setScaleX(surfaceRectInPreviewView.width() / mResolution.getWidth());
preview.setScaleY(surfaceRectInPreviewView.height() / mResolution.getHeight());
preview.setTranslationX(surfaceRectInPreviewView.left - preview.getLeft());
preview.setTranslationY(surfaceRectInPreviewView.top - preview.getTop());
This code is after all the matrix transformations are done (which I don't fully understand), so I may be misinterpreting it. So I just wanted to validate my assumption about whether images are scaled if they are higher resolution than the view.
PreviewView is designed to display the largest possible FOV. If you wish to display a smaller FOV, you can take a look at the zooming APIs: https://developer.android.com/reference/androidx/camera/core/CameraControl#setZoomRatio(float)
The scaling in PreviewView is for getting rid of the black bars in case the aspect ratio of the preview does not match the aspect ratio of the view. It doesn't matter what resolutions they have.
In your example, the output image is 1920x1080 and the view is 800x800. For the FILL_* types, the output image will be scaled to fill the view, in which case it will be scaled to 1422x800, then the extras will be cropped out. For the FIT_* types, the output image will be scaled to fit the view, in which case it will be scaled to 800x450, and placed inside of the view.
Hope this helps. Please let me know if you have more questions.
I am using TouchImageView in my app to display images.
https://github.com/MikeOrtiz/TouchImageView
As far as I know, Android does not have an easy way to show an image Top Cropped. The best I can do is to Center Crop the Image, which causes the top and bottom of the image to extend off of the screen.
Is there a way to programmatically scroll my image after it has been loaded, and then call ImageView.setVisibility(true)?
I've tried image.scrollBy(int x, int y);
but I'm not sure exactly how to use it.
I do have the ability to get pixel values for both my screen, and for my image before it is scaled, but don't know what function I should use those values in.
Thanks for your input.
I know this is a bit unusual, but I want to resize the bounds of an image without the image itself being scaled down.
For example, when I have an 200x200 image, and resize it to 100x200 the result should just be half of the image, where the other half is just cut.
Is that possible?
I don't know how you resize the image in your app, but if you resize any 200x200 image to 100x200, this result will be a part of your initial image, depending on the starting point.(could be the 1st half starting at pixel 0, the 2nd half starting at pixel 100 or any other half in between depending on the starting pixel)
I have to display a splash image which has a round shaped object (a Ball). The Layout for splash is a simple linear layout with just a single Image view to occupy the full screen.
Image : single image with the size of 1280 x 720.
When my splash screen is shown in the App, The round object is shown in different shape in different screen sizes. I hope the aspect ratio and the resolution is the cause for these elongated images.
Could you please suggest an idea / approach to solve this ?
Do I need to consider the aspect ratio or the resolution or both ?
Finally the ball should look like a ball in all the devices :)
Thanks in Advance.
1) Yes, by default Android will scale your image down to fit the ImageView, maintaining the aspect ratio. However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="...". src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView. (You can use a background and a source at the same time though, which can be useful for things like displaying a frame around the main image, using just one ImageView.)
2)You should also see android:adjustViewBounds to make the ImageView resize itself to fit the rescaled image. For example, if you have a rectangular image in what would normally be a square ImageView, adjustViewBounds=true will make it resize the ImageView to be rectangular as well. This then affects how other Views are laid out around the ImageView.
You can change the way it default scales images using the android:scaleType parameter. By the way, the easiest way to discover how this works would simply have been to experiment a bit yourself! Just remember to look at the layouts in the emulator itself (or an actual phone) as the preview in Eclipse is usually wrong.
Reference : How to scale an Image in ImageView to keep the aspect ratio
set imageView property
scaleType="centerInside"
Add scaled versions of the image with the same file name under folders 'res->drawable','res->drawable-ldpi','res->drawable-hdpi' and under xhdpi "http://developer.android.com/guide/practices/screens_support.html#DesigningResources"
I have some images I want to show in thumbnails, and I want them to center crop and not scale. The images themselves are almost always rectangular, and I want to crop them around their center and have them fit into a square ImageView. The bitmaps themselves either have a height that matches the size of the ImageView, or a width that matches. So essentially I just want to crop off the left/right or the top/bottom of each photo, and not lose any quality.
When I try to use ScaleType.CENTER_CROP my images are becoming blurry. Any suggestions?
You should just use ScaleType.CENTER. This will perform no scaling, it will just center the image behind the image frame. Anything falling outside the ImageView will be cropped.