我有以下代码将RAW图像保存为JPEG格式到相机胶卷,但它有三个问题:1)它是镜像图像,2)它旋转了90度,3)它的分辨率很低。
我知道如何直接捕获JPEG,但我正在尝试先对RAW数据应用一些自定义滤镜。 请帮忙如果你能的话,提前感谢!
// In my PhotoCaptureDelegate
func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingRawPhotoSampleBuffer rawSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
if ( rawSampleBuffer != nil) {
let temporaryDNGFileURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(resolvedSettings.uniqueID)lld.dng")!
let temporaryJPGFileURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(resolvedSettings.uniqueID)lld.jpg")!
let imageData = AVCapturePhotoOutput.dngPhotoDataRepresentation(forRawSampleBuffer: rawSampleBuffer!, previewPhotoSampleBuffer: previewPhotoSampleBuffer)
try! imageData?.write(to: temporaryDNGFileURL)
PHPhotoLibrary.requestAuthorization({ (status) in
if status == .authorized {
PHPhotoLibrary.shared().performChanges({
let options = PHAssetResourceCreationOptions()
options.shouldMoveFile = true
//Write Raw:
PHAssetCreationRequest.forAsset().addResource(with: .photo, fileURL: temporaryDNGFileURL, options: options)
// Process Raw to JPG
if let ciRawFilter = CIFilter(imageData: imageData! as Data, options: nil) {
let cs = CGColorSpace(name: CGColorSpace.displayP3)!
do {
try self.contextForSaving.writeJPEGRepresentation(of: ciRawFilter.outputImage!, to: temporaryJPGFileURL, colorSpace: cs, options: [ kCGImageDestinationLossyCompressionQuality as String: 1.0])
PHAssetCreationRequest.forAsset().addResource(with: .photo, fileURL: temporaryJPGFileURL, options: options)
} catch _ {
print("error with jpeg conversion")
}
}
}, completionHandler: { [unowned self] success, error in
if let error = error {
print("Error occurered while saving photo to photo library: \(error)")
} else {
print("Raw photo written to photo library.")
}
if FileManager.default.fileExists(atPath: temporaryDNGFileURL.path) {
do {
(try FileManager.default.removeItem(at: temporaryDNGFileURL))
}
catch _ {
print("could not remove temporary file")
}
}
self.didFinish()
}
)
}
else {
self.didFinish()
}
})
} else {
print("Error capturing photo: \(error)")
}
}
我知道如何直接捕获JPEG,但我正在尝试先对RAW数据应用一些自定义滤镜。 请帮忙如果你能的话,提前感谢!
let imageData = AVCapturePhotoOutput.dngPhotoDataRepresentation(forRawSampleBuffer: rawSampleBuffer!, previewPhotoSampleBuffer: previewPhotoSampleBuffer) let ciRawFilter = CIFilter(imageData: imageData! as Data, options: nil)
- Dan Hixon