TextureView.getBitmap can only be used with width and height parameters, does anyone know if it is possible to get a Bitmap that is taken from the TextureView with a certain X and Y offset?
Related
I have an Image View which displays an image (e.g 2000x1000 pixels) and I have a coordinate (X,Y) on that image (not the image view). The canvas of my Image View is 600x800 for example. How can I convert the point (X,Y) to screen coordinate so that I can draw a path with them on the OnDraw(...) method of Image View. Any help is appreciated! Thank you.
Update: If I use matrix to draw the path between coordinates, it works but the path and objects i draw become really small. Here is the code i used.
final Matrix matrix = canvas.getMatrix();
matrix.preConcat( _view.getImageMatrix() );
matrix.preScale( 1.0f /_inSampleSize, 1.0f / _inSampleSize);
canvas.setMatrix( matrix );
//I draw the path here
Update: I add a picture to show the effect when using matrix to draw the path. I would like to have the 4 line and the 4 corner balls to be in normal size. The red color is the boundary of the Image View which holds the picture.
I think that might depend on how exactly you are displaying your image. Your ImageView (600x800) is not the same aspect ratio as your bitmap (2000x1000).
You are keeping the bitmap's aspect ratio stable as you scale it down? If so, which part (height or width) takes up the full screen and which has black (or whatever else) as padding? This will help you determine your scale factor.
scale_factor = goal_height/height1; //if height is what you are scaling by
scale_factor = goal_width/width1; //if width is what you are scaling by.
I would try:
x_goal = x1 * scale_factor;
y_goal = y1 * scale_factor;
That is, if you have a point (1333, 900) in your image, and your image takes up the full width, you would multiply both x and y by 600/2000 to get (399.9, 270). (you might want to round that decimal).
If you are NOT keeping the bitmaps aspect ratio stable (that is, you're squeezing it to fit), then you'd have a height_scale_factor and a width_scale factor. So you'd take (1333,900) and multiply x by 600/2000 and y by 800/1000 to get (399.9,720).
I have floor plan image and draw it into UI with bitmap in scaled example 500 x 500 pixel. The actual image is 700x700 pixel.
With normal size (700px x 700px) floorplan i could draw circle in direct position example (50px , 75px) and it give the corect result in normal size of floorplan.
This circle is for show current location of user.
The question is how to position that circle if the image is scaled into 500x500 pixel with correct position same as (50px ,75px) in (700px x 700px) floorplan ?
Dosn't this come down to a simple math problem?
The coordinates 50,75 in a 700*700 image would correspond to the coordinates 35(,71..), 53(,57..) in a 500*500 image. Fraction can be omitted, since, well, they're pixels.
The scaling factor would be 500/700 which is roughly 0,714. IE
ScalingFactor = NewDimension / OriginalDimension
ScaledPosition = OriginalPosition * ScalingFactor
This is obviously simplified because it is a square image, and scaling is the same among both sides.
I want to calculate rotated image size, Image is inside rectangle. I have rectangle width, height and angle of rotated image. Any one tell me
how to calculate rotated image size?
So you have width, height and angle means you already got RotatedRect.
Now using the method
Rect RotatedRect::boundingRect();
you can easly calculate the bounding box for rotated rect.
for more info see RotatedRect.
Edit:
As per your comment below is the way how to find the width and height of rotated rect.
So you know the four corners of rectangle, lets say (x1,y1),(x2,y2),(x3,y3),(x4,y4), now you need to find the transformed point after rotation by the given angle, let it be (xT1,yT1),(xT2,yT2),etc...
where
xT = x0+(x-x0)*cos(theta)+(y-y0)*sin(theta)
yT = y0-(x-x0)*sin(theta)+(y-y0)*cos(theta)
here (x0,y0) is the center around which you are rotating. and theta = angle * CV_PI / 180.0
Using above calculate four transformed points, finally calculate the height and width by finding the distance between transformed points.
The issue is when i up or downscale the sprite at its centre, sprite X and Y are not updated, although its drawn correctly on the screen . You can test this issue with the following code:
Rectangle rect=new Rectangle(CAMERA_WIDTH/2,CAMERA_HEIGHT/2,100,100,this.getVertexBufferObjectManager());
rect.setScaleCenter(rect.getWidth()/2, rect.getHeight()/2);
rect.setScale(1.5f);
The new X and Y should match the scene coordinates, but this doesnot happen
The X and Y postion of a sprite should NEVER update in response to a scale change. The x and y are measured from the transform center of the sprite, essentially its local 0,0. So no scale multiplier will ever change that transform root to anything other than 0,0.
You may see the object appear to move left or right in response to a scale up or scale down, but that is because the bounding box of the sprite is enlarging or shrinking. But the point from which that transform is changing does not change, so X and Y stay the same.
I want to know if there is any way to modify text height and width separately when drawing with canvas. To set text size you can simply do paint.setTextSize(x);but it change text size in X and Y and I need to change text size in X and Y separately as you do with paint.setTextScaleX(x);but there isn't anything like paint.setTextScaleY(y);.
Is any way to implement this or does it already exists in Android ?
Thank you
There is no setTextScaleY method, because there is no need for one. setTextSize multiplies both X and Y scale factors, and setTextScaleX multiplies the X scale factor only. So you could reach any desired scaling scaleX, scaleY this way:
setTextSize(scaleY);
setTextScaleX(scaleX/scaleY); //setTextScaleX scales according to the CURRENT size (setTextScaleX(1) does nothing).