I would like to know how can I crop , scale and rotate a ImageView that have a scaled background image using a fixed width and height on it's parent.
To crop (get subImage) see the following thread:
How to Cut a ImageView to a subimage in the range of four specified points
For scale and rotate use Matrix with setScale and setRotate.
This forum has a number of threads discussing relevant topic, e.g.
Android Scale a bitmap image
Android - Rotate image around center point?
Before publishing a question you better look around :)
Related
I am trying to crop out the portion inside the RoundRect. I have an overlay on top of PreviewView. The horizontal margin is 2% of the screen width and top is 15% of the screen height.
When I take the picture, I want to crop it with the same proportion as the RoundRect displayed. As the resolution of the captured image is different than that of the screen, I am not being able to get the exact portion inside the rounded rectangle. Is there anyway I can use the margin value used for the RoundRect to get the margin value for the captured Image?
I did try scaling it down to the screen size as mentioned here and crop it but even that doesn't help as quality and aspect ratio gets compromised.
I am using bitmap to crop the captured image.
Bitmap.createBitmap(originalBp, left, top, right, bottom);
The outputs are aligned with the crop rect. The crop rect of different use cases should be mapped to the same area on the camera sensor. If you are using PreviewView, a simple way to calculate the transform from Preview to ImageCapture is using CoordinateTransform:
// Build the transform from PreviewView to Capture
OutputTransform source = previewView.getOutputTransform();
OutputTransform source = FileTransformFactory().getOutputTransform(capturedFile);
CoordinateTransform coordinateTransform = new CoordinateTransform(source, target);
coordinateTransform.mapRect(roundRect);
// Then use roundRect to crop the capture file.
ok so this is a hard one (in my head).
mframe, sframe1, sphoto1, sframe2, sphoto2 has its own scales and dimensions:
a width and a height is the dimensions that these objects have.
The plan:
Sframe2 gets dragged on to sframe1. When I let go of the mouse sphoto2's dimensions (which are scaled within the boundaries of sframe2) need to be dropped in the scaled location of sphoto1 (which resides scaled within sframe1).
to be able to drop sframe2 within sframe1 on the location I let go I need to be able to correlate the location it was dropped on to the scaled image as I want to merge sphoto2 with sphoto1.
sframe1 and sframe2 have coordinates on mframe. sphoto1 and sphoto2 only have private coordinates (such as I can merge the images to and x and y position on them.).
The problem is that because the photos inside are scaled differently to these frames I have figure out the scaling factors to be able to correctly merge sphoto2 with sphoto1 with photo2 at the correct size and position on sphoto1.
so the question is... How can I do that?
Below is a diagram to assist in visually representing the problem.
Here is also a video to show you what it should not do. The image inside the frame needs to scale and merge on the other image correctly.
http://www.youtube.com/watch?v=N17Rrs1dSz0&feature=youtu.be
My mind is fried. Can you figure out what needs to scale what?
You can set layout params for that image and multiply with screen ratio, example:
imageview.setLayoutParams(new LinearLayout.LayoutParams(
(int) (250 * config.ratio), (int) (280 * config.ratio)));
But if you use this solution, you must calculate sceen ratio before scaling your image
In Android using ImageView and Matrix, I want to scale an image to fit in the display area.
Then using pinch, a user can zoom the image.
Using setScaleType(ImageView.ScaleType.FIT_CENTER) I can fit my image in display area. But when I use setScaleType(ImageView.ScaleType.MATRIX), to start zooming the image, the image gets reset to default size.
So my question is how to find a scale matrix which will give same effect as
setScaleType(ImageView.ScaleType.FIT_CENTER).
You need to use mMatrix.setRectToRect and Matrix.ScaleToFit.CENTER with your image coordinates and your view coordinates as the source and destination rectangles.
mMatrix.setRectToRect(new RectF(0,0,mImageView.getMeasuredWidth(),mImageView.getMeasuredHeight()),new RectF(0,0,mImageView.getParent().getMeasuredWidth(),mImageView.getParent().getMeasuredHeight()),Matrix.ScaleToFit.CENTER);
I have a Canvas that i draw text on.
(this is quite advanced i think hope you can follow me)
See attached image below.
The functionality is that I can tap on the screen to change the textsize.
Tap left screen = smaller Font.
Tap right Screen = bigger Font.
I can then also move the text on the screen.
When textsize is ok and i have moved the text where i want it,
Then I want to save it back to the original Bitmap.
I use options.inSampleSize = 4; to initially load the Bitmap
The ImageView that have the Bitmap is of course smaller then the original Image.
Some kind of calculation is needed.
This tends to be quite difficult to do.
I have the options.inSampleSize = 4 Bitmaps Ratio.
It's 0.59, 0.69 something depending on Landscape or portrait.
Im playing around with that to somehow change the new BitmapsetTextSize
to look the same as the ImageView smaller Bitmap.
What could i do here?
I have a feeling that since one never know what size an image have.
I have to somehow scale/constrain the Loaded Bitmap Ratio to a fixed Ratio.
Then i need to using percentage or something to transfer the text location
to the bigger image.
I can kind of do that when it comes to initial
(red small ball on picture) location. Hence, the starting point of the text.
But i dont know how long the text is so im stuck so so speak and asking for advice
One way i tried was to divide paint.getTextSize() with the Ratio something like 0.59. That looked like a solution at first. But the image ratio is not fixed and the Font size is not fixed something else is needed.
Here are two pictures showing the problem.
On phone Bitmap:
The saved new Bitmap:
I'm not 100% clear that I understand what you mean, but here's a go. It sounds like you were close to the right approach. Instead of using a fixed ratio, you need to calculate the ratio that the image is scaled by to fit in the view on the phone, then you can scale the text by the inverse ratio. So in steps:
Measure the width of the original image (height would do just as well, but we just need one dimension)
Measure the width of the scaled image
Calculate ratio (ratio = original / scaled)
Let the user type their text
You can then get the text size using something like: float paintSize = paint.getTextSize();
For rendering on the final image, use paint.setTextSize(paintSize / ratio);.
I have an activity which consists of just an ImageView. The drawable contained in the ImageView is a Bitmap that was captured from the camera. The image is automatically scaled (maintaining the Bitmap's aspect ratio) so that the scaled width matches the width of the ImageView, and the image is centered on the screen vertically.
I need to figure out the coordinates of the top-left pixel of the actual drawable (not of the ImageView itself), but the ImageView class doesn't seem to give me a way of doing that.
I feel like I could potentially calculate it based on the dimensions of the original bitmap and the dimensions of the ImageView, but that would involve a lot of math that should be unnecessary, and would be prone to floating point errors.
Does anyone know of a way to find the coordinates of the top-left pixel of the Drawable relative to the ImageView?
Thanks.
Looking at the ImageView source code, it translates a Matrix halfway the vertical size (i.e., center it vertically), so you could retrieve it with the ImageView getImageMatrix() method and check the vertical translation by Matrix#getValues(float[]). If I read it right, it will be the sixth value.
I can't test it right now, but it would be something like:
Matrix matrix = iv.getImageMatrix ();
float[] values = new float[];
matrix.getValues(values);
float startY = values[5];