I have custom camera view and on top of that I have guides I want to crop the taken photo to that guide, when I get the cropped image I get of different section, I want to get the area inside the guides.
我有自定义相机视图,最重要的是我有指南我想将拍摄的照片裁剪到该指南,当我得到裁剪后的图像我得到不同的部分,我想得到指南内的区域。
func imageByCropToRect(rect:CGRect, scale:Bool) -> UIImage {
var rect = rect
var scaleFactor: CGFloat = 1.0
if scale {
scaleFactor = self.scale
rect.origin.x *= scaleFactor
rect.origin.y *= scaleFactor
rect.size.width *= scaleFactor
rect.size.height *= scaleFactor
}
var image: UIImage? = nil;
if rect.size.width > 0 && rect.size.height > 0 {
let imageRef = self.cgImage!.cropping(to: rect)
image = UIImage(cgImage: imageRef!, scale: scaleFactor, orientation: self.imageOrientation)
}
return image!
}
if let image = self.getImageFromSampleBuffer(buffer: sampleBuffer, orientation: orientation) {
let newImage = image.imageByCropToRect(rect: self.guideImageView.frame, scale: true)
selectedPropertyImage = newImage
stopCaptureSession()}
1 个解决方案
#1
0
You have to map coordinate of current view layer to camera preview layer using captureDevicePointConverted. For example:
您必须使用captureDevicePointConverted将当前视图图层的坐标映射到摄像机预览图层。例如:
let previewImageLayerBounds = previewLayer.bounds
let originalWidth = original.size.width
let originalHeight = original.size.height
let A = previewImageLayerBounds.origin
let B = CGPoint(x: previewImageLayerBounds.size.width, y: previewImageLayerBounds.origin.y)
let D = CGPoint(x: previewImageLayerBounds.size.width, y: previewImageLayerBounds.size.height)
let a = previewLayer.captureDevicePointConverted(fromLayerPoint: A)
let b = previewLayer.captureDevicePointConverted(fromLayerPoint: B)
let d = previewLayer.captureDevicePointConverted(fromLayerPoint: D)
let posX = floor(b.x * originalHeight)
let posY = floor(b.y * originalWidth)
let width: CGFloat = d.x * originalHeight - b.x * originalHeight
let height: CGFloat = a.y * originalWidth - b.y * originalWidth
let cropRect = CGRect(x: posX, y: posY, width: width, height: height)
#1
0
You have to map coordinate of current view layer to camera preview layer using captureDevicePointConverted. For example:
您必须使用captureDevicePointConverted将当前视图图层的坐标映射到摄像机预览图层。例如:
let previewImageLayerBounds = previewLayer.bounds
let originalWidth = original.size.width
let originalHeight = original.size.height
let A = previewImageLayerBounds.origin
let B = CGPoint(x: previewImageLayerBounds.size.width, y: previewImageLayerBounds.origin.y)
let D = CGPoint(x: previewImageLayerBounds.size.width, y: previewImageLayerBounds.size.height)
let a = previewLayer.captureDevicePointConverted(fromLayerPoint: A)
let b = previewLayer.captureDevicePointConverted(fromLayerPoint: B)
let d = previewLayer.captureDevicePointConverted(fromLayerPoint: D)
let posX = floor(b.x * originalHeight)
let posY = floor(b.y * originalWidth)
let width: CGFloat = d.x * originalHeight - b.x * originalHeight
let height: CGFloat = a.y * originalWidth - b.y * originalWidth
let cropRect = CGRect(x: posX, y: posY, width: width, height: height)