I am trying to crop image inside bounding box from camera preview and display in a activity. I have a surfaceview and a square imageview on top of it. I have to capture image that appears only inside imageview. Following is my cropping code, No idea as to what is wrong here,Please help
int width = rotatedBitmap.getWidth();
int height = rotatedBitmap.getHeight();
int newWidth = (height > width) ? width : height;
int newHeight = (height > width)? height - ( height - width) : height;
int x = (width - height) / 2;
x = (x < 0)? 0: x;
int y = (height - width) / 2;
y = (y < 0)? 0: y;
Bitmap cropImg = Bitmap.createBitmap(rotatedBitmap, x, y,newWidth, newHeight);
what's wrong with my code?
Related
I want to draw image on Top in each Arc of Canvas
private void drawImage(Canvas canvas, float tempAngle, Bitmap bitmap,String mValue) {
//get every arc img width and angle
int imgWidth = (radius / mWheelItems.size());
//int imgWidth = (radius / 3);
float angle = (float) ((tempAngle + 360 / mWheelItems.size() /2) * Math.PI / 180);
//calculate x and y
int x = (int) (center + radius / 2 / 2 * Math.cos(angle));
int y = (int) (center + radius / 2 / 2 * Math.sin(angle));
int top=y - imgWidth/2;
int bottom=y +imgWidth/2;
int left=x - imgWidth /2;
int right=x + imgWidth / 2;
Rect rect = new Rect(left, top, right, bottom);
final Rect rect1 = new Rect(x - imgWidth /2 , y - imgWidth / 2 , bitmap.getWidth() , bitmap.getHeight());
canvas.drawBitmap(bitmap, null, rect, null);
}
the Arc is made according to the size of items
The Result is shown like that
But I want the image bitmap shown on top like that of rect. Red also want to large size of images or bitmap
Solved by Specific Tab and Big Screen Tablets
Change the X and Y coordination According to Angle
if(tempAngle==0) {
x = x + 50;
}
if(tempAngle==60) {
y = y + 50;
}
if(tempAngle==120) {
imgWidth = imgWidth-12;
y = y + 20;
x = x - 40;
}
if(tempAngle==180) {
imgWidth = imgWidth+12;
x = x - 50;
}
if(tempAngle==240) {
y = y - 50;
}
if(tempAngle==300) {
y = y - 20;
x = x + 40;
}
So I have a field where my image displays and my max height can be 375 and max width is 775. I want it to be as close to as one of these values as possible to get the maximum size while maintaining the aspect ratio This is what I came up with and it actually seems to work pretty well but I am thinking there is a better way I am not thinking of.
InputStream in = new ByteArrayInputStream(fileData);
BufferedImage buf = ImageIO.read(in);
int maxWidth = 775;
int maxHeight = 375;
int newWidth;
int newHeight;
float height = buf.getHeight();
float width = buf.getWidth();
float ratio = (float) 0.0;
if(height > maxHeight || width > maxWidth)
{
if (height > width)
{
ratio = (height/width);
}
else if(width > height)
ratio = (width/height);
while(height > maxHeight || width > maxWidth)
{
if (height > width)
{
height -= ratio;
width -= 1;
}
else if(width > height)
{
width -= ratio;
height -= 1;
}
}
}
newWidth = (int) width;
newHeight = (int) height;
// Call method to scale image to appropriate size
byte[] newByte = scale(fileData, newWidth, newHeight);
You know that you will use one of the two maxes (if you weren't the image could still be scaled up).
So it's just a problem of determining which. If this aspect ratio of the image is greater than your max area ratio, then width is your limiting factor, so you set width to max, and determine height from your ratio.
Same process can be applied for a smaller ratio
Code looks something like the following
float maxRatio = maxWidth/maxHeight;
if(maxRatio > ratio) {
width = maxWidth;
height = width / ratio;
} else {
height = maxHeight;
width = height * ratio;
}
I have an image where
Height = 1300
Width = 1300
I scaled the image to:
ScaledHeight = 700
ScaledWidth = 700
I am using the following to get the original Coordinates:
public Coordinate GetScaledXYCoordinate(int oldX, int oldY, int width, int height, int scaledWidth, int scaledHeight)
{
int newX = (int)(oldX * width)/scaledWidth;
int newY = (int)(oldY * height)/scaledHeight;
Coordinate retXY = new Coordinate(newX, newY);
return retXY;
}
EDIT:
I have updated to include answer
With
int width = 1300;
int scaledWidth = 700;
the value of (width/scaledWidth) is 1 - you are doing integer arithmetic not floating point.
Use
int newX = (oldX * width) /scaledWidth;
int newY = (oldY * height) /scaledHeight;
to avoid this issue.
My Question is how can I move a bitmap on a canvas?
Recently I'm making a game and There's a button.
When I'm clicking on this button I want to move the bitmap (Only X)
but when i'm doing this the bitmap is gone.
Here's the code :
"main_right" is the button that I'm clicking on.
I also tried to put "invalidate()" there but it also didn't work.
#Override
public boolean onTouchEvent(MotionEvent event) {
final float x = event.getX();
final float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (x > width - main_right.getWidth()
&& x < width - main_right.getWidth()
+ main_right.getWidth()
&& y > height
- (main_right.getHeight() + main_right.getHeight() / 2)
&& y < height
- (main_right.getHeight() + main_right.getHeight() / 2)
+ main_right.getHeight()) {
x += 1;
canvas_main.drawBitmap(image, (width / 2) + x2,
height - (image.getHeight() + image.getHeight() / 2),
paint);
} else {
x = 0;
}
return true;
}
return false;
}
Store the x position on your onTouchEvent and draw at your canvas inside the onDraw(Canvas), that is the surface you need to draw.
canvas_main.drawBitmap(image, (width / 2) + x,
height - (image.getHeight() + image.getHeight() / 2),
paint);
The height might be the problem if your bitmap is > (device height/3)
height - (image.getHeight() + image.getHeight() / 2)
In essence means height - 1.5*image height. Since the top left of the bitmap is placed at that coordinate, the image will disappear out of the screen.
It should be
height - (image.getHeight() - image.getHeight() / 2)
or
height - image.getHeight() / 2
I am trying to resize the JFrame manually by dragging a JPanel. Here is what i have done. At the moment it works, but the screen still flickers, and the resizing is not correct
What i want it to happen is, when i click on the Jpanel and drag it, the JFrame should resize automatically to where the X Coordinates and Y Coordinates are.
public void mouseDragged(MouseEvent e) {
// Sets width, height and moved X and Y coordinates
int currentwidth = this.getWidth();
int currentheight = this.getHeight();
int newwidth = 0;
int newheight = 0 ;
int thisX = this.getX();
int thisY = this.getY();
// Calculates the moving distance
int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);
// Checks which component the mouse is clicked on
if(e.getComponent() == reszingbit){
// Calculates the new height and width
newwidth = 200 + xMoved;
newheight = 200 + yMoved;
// Making sure that the new height and width is not less than actual size
if(newwidth >= 200 && newheight >= 200){
this.setSize(newwidth,newheight);
}
}
200 is the actual height and width.
It is to little informations, but maybe you need, to change the size calculation to:
newwidth = this.getWidth() + xMoved;
newheight = this.getHeight() + yMoved;
also the move size calculation should look like this:
int xMoved = e.getX() - initialClick.x;
int yMoved = e.getY() - initialClick.y;