Cropping UIImage like camscanner

2019-02-01 01:54发布

问题:

I am stuck in my application feature. I want cropping feature similar to Cam Scanner Cropping. The screens of CAM-SCANNER are: I have created similar crop view.

I have obtained CGPoint of four corners. But How can I obtained cropped image in slant.

Please provide me some suggestions if possible.

回答1:

This is a perspective transform problem. In this case they are plotting a 3D projection in a 2D plane.

As, the first image has selection corners in quadrilateral shape and when you transform it in a rectangular shape, then you will either need to add more pixel information(interpolation) or remove some pixels.

So now actual problem is to add additional pixel information to cropped image and project it to generate second image. It can be implemented in various ways:

<> you can implement it by your own by applying perspective tranformation matrix with interpolation.

<> you can use OpenGL .

<> you can use OpenCV. .. and there are many more ways to implement it.

I had solved this problem using OpenCV. Following functions in OpenCV will help you to achieve this.

  1. cvPerspectiveTransform

  2. cvWarpPerspective

First function will calculate transformation matrix using source and destination projection coordinates. In your case src array will have values from CGPoint for all the corners. And dest will have rectangular projection points for example {(0,0)(200,0)(200,150)(0,150)}.

Once you get transformation matrix you will need to pass it to second function. you can visit this thread.

There may be few other alternatives to OpenCV library, but it has good collection of image processing algorithms.

iOS application with opencv library is available at eosgarden.



回答2:

I see 2 possibilities. The first is to calculate a transformation matrix that slants the image, and installing it in the CATransform3D property of your view's layer.

That would be simple, assuming you knew how to form the transformation matrix that did the stretching. I've never learned how to construct transformation matrixes that stretch or skew images, so I can't be of any help. I'd suggest googling transformation matrixes and stretching/skewing.

The other way would be to turn the part of the image you are cropping into an OpenGL texture and map the texture onto your output. The actual texture drawing part of that would be easy, but there are about 1000 kilos of OpenGL setup to do, and a whole lot to learning in order to get anything done at all. If you want to pursue that route, I'd suggest searching for simple 2D texture examples using the new iOS 5 GLKit.



回答3:

Using the code given in Link : http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/

Instead of using CGRect and CGContextClipToRect Try using CGContextEOClip OR CGContextClosePath

Though i havnt tried this... But i have tried drawing closed path using CGContextClosePath on TouchesBegan and TouchesMoved and TouchesEnd events.

Hope this can give more insight to your problem...