简体   繁体   中英

How to get co-ordinates of image cropper in android?

I am trying to get these x1, x2, y1, y2 co-ordinates of the image cropper according this image.

图片

Any help regarding this?

following link is the library i am using for image cropping. Android-Image-Cropper

Although Using thirdparty libraries are advised for beginners, sometimes is not the best choice, especially if there is an implementation of whatever you want to do in the android SDK, take a look at this tutorial for example:

https://code.tutsplus.com/tutorials/capture-and-crop-an-image-with-the-device-camera--mobile-11458

Now for your problem, you can approach this in many ways:

Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yourImage);

croppedImage = Bitmap.createBitmap(mBitmap, 0 , 0 , desiredwidth, desiredheight);
  1. Convert your image into a bitmap and crop the bitmap, so get your image from drawable or from Uri on the device then crop it with 2 lines of code, i am choosing a drawable in this case.

Using the answer above does not work if you want to crop out of bounds areas

Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yourImage);


Rect mRect= new Rect(x1, y1, x2, y2); 

assert(mRect.left < mRect.right && mRect.top < mRect.bottom);

Bitmap croppedImage = Bitmap.createBitmap(rect.right-rect.left, rect.bottom-rect.top, Bitmap.Config.ARGB_8888);

new Canvas(croppedImage).drawBitmap(mBitmap, -rect.left, -rect.top, null);
  1. This will define a rectangle based on xy coordinates and then it will crop the image as a bitmap based on these coordinates and then just call new Canvas to draw it wherever you want.

PS: Please check the android sdk before you use any libraries there are alot of good examples and it will decrease the use of unnecessary libraries, and it will make you understand the underlying code better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM