Vassa007

rekam wajah

Oct 1st, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.19 KB | None | 0 0
  1. //
  2. //  RekamWajahViewController.swift
  3. //  SFS-ios
  4. //
  5. //  Created by UPATIK Macbook on 01/10/24.
  6. //  Copyright © 2024 Universitas Jember. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import AVFoundation
  11.  
  12. class RekamWajahViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  13.     @IBOutlet weak var imageView: UIImageView!
  14.    
  15.     override func viewDidLoad() {
  16.         super.viewDidLoad()
  17.     }
  18.    
  19.     func showCameraAccessDeniedAlert() {
  20.             let alert = UIAlertController(
  21.                 title: "Akses Kamera Ditolak",
  22.                 message: "Aplikasi memerlukan akses kamera. Silakan berikan izin di Pengaturan.",
  23.                 preferredStyle: .alert
  24.             )
  25.             alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
  26.             present(alert, animated: true, completion: nil)
  27.         }
  28.  
  29.         func checkCameraPermission(completion: @escaping (Bool) -> Void) {
  30.             let status = AVCaptureDevice.authorizationStatus(for: .video)
  31.             switch status {
  32.             case .authorized:
  33.                 completion(true)
  34.             case .notDetermined:
  35.                 AVCaptureDevice.requestAccess(for: .video) { granted in
  36.                     completion(granted)
  37.                 }
  38.             case .denied, .restricted:
  39.                 completion(false)
  40.             @unknown default:
  41.                 completion(false)
  42.             }
  43.         }
  44.    
  45.     @IBAction func takePhoto(_ sender: Any) {
  46.         checkCameraPermission { granted in
  47.             if granted {
  48.                 DispatchQueue.main.async {
  49.                     let cameraVC = CameraViewController()
  50.                     cameraVC.modalPresentationStyle = .fullScreen
  51.                     self.present(cameraVC, animated: true, completion: nil)
  52.                 }
  53.             } else {
  54.                 self.showCameraAccessDeniedAlert()
  55.             }
  56.         }
  57.     }
  58.    
  59.     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  60.         if let originalImage = info[.originalImage] as? UIImage {
  61.             // Crop image to 1:1 ratio and resize to 512x512
  62.             let fixedOrientationImage = fixImageOrientation(image: originalImage)
  63.             let croppedImage = cropToSquare(image: fixedOrientationImage)
  64.             let resizedImage = resizeImage(image: croppedImage, targetSize: CGSize(width: 512, height: 512))
  65.            
  66.             imageView.image = resizedImage
  67.         }
  68.         dismiss(animated: true, completion: nil)
  69.        
  70.     }
  71.    
  72.     func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  73.         dismiss(animated: true, completion: nil)
  74.     }
  75.  
  76.     func cropToSquare(image: UIImage) -> UIImage {
  77.         let originalWidth = image.size.width
  78.         let originalHeight = image.size.height
  79.  
  80.         let cropSize = min(originalWidth, originalHeight)
  81.         let xOffset = (originalWidth - cropSize) / 2
  82.         let yOffset = (originalHeight - cropSize) / 2
  83.  
  84.         let cropRect = CGRect(x: xOffset, y: yOffset, width: cropSize, height: cropSize)
  85.         let imageRef = image.cgImage?.cropping(to: cropRect)
  86.  
  87.         return UIImage(cgImage: imageRef!)
  88.     }
  89.  
  90.     func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
  91.         let size = image.size
  92.  
  93.         let widthRatio  = targetSize.width  / size.width
  94.         let heightRatio = targetSize.height / size.height
  95.  
  96.         let scaleFactor = min(widthRatio, heightRatio)
  97.  
  98.         let newSize = CGSize(width: size.width * scaleFactor, height: size.height * scaleFactor)
  99.         print("ukuran gambar : \(newSize)")
  100.  
  101.         UIGraphicsBeginImageContextWithOptions(targetSize, false, 0.0)
  102.         image.draw(in: CGRect(origin: .zero, size: targetSize))
  103.  
  104.         let newImage = UIGraphicsGetImageFromCurrentImageContext()
  105.         UIGraphicsEndImageContext()
  106.  
  107.         return newImage!
  108.     }
  109.    
  110.     func fixImageOrientation(image: UIImage) -> UIImage {
  111.         if image.imageOrientation == .up {
  112.             return image
  113.         }
  114.        
  115.         var transform = CGAffineTransform.identity
  116.        
  117.         switch image.imageOrientation {
  118.         case .down, .downMirrored:
  119.             transform = transform.translatedBy(x: image.size.width, y: image.size.height)
  120.             transform = transform.rotated(by: .pi)
  121.         case .left, .leftMirrored:
  122.             transform = transform.translatedBy(x: image.size.width, y: 0)
  123.             transform = transform.rotated(by: .pi / 2)
  124.         case .right, .rightMirrored:
  125.             transform = transform.translatedBy(x: 0, y: image.size.height)
  126.             transform = transform.rotated(by: -.pi / 2)
  127.         default:
  128.             break
  129.         }
  130.        
  131.         switch image.imageOrientation {
  132.         case .upMirrored, .downMirrored:
  133.             transform = transform.translatedBy(x: image.size.width, y: 0)
  134.             transform = transform.scaledBy(x: -1, y: 1)
  135.         case .leftMirrored, .rightMirrored:
  136.             transform = transform.translatedBy(x: image.size.height, y: 0)
  137.             transform = transform.scaledBy(x: -1, y: 1)
  138.         default:
  139.             break
  140.         }
  141.        
  142.         guard let cgImage = image.cgImage else { return image }
  143.        
  144.         guard let context = CGContext(
  145.             data: nil,
  146.             width: Int(image.size.width),
  147.             height: Int(image.size.height),
  148.             bitsPerComponent: cgImage.bitsPerComponent,
  149.             bytesPerRow: 0,
  150.             space: cgImage.colorSpace!,
  151.             bitmapInfo: cgImage.bitmapInfo.rawValue
  152.         ) else { return image }
  153.        
  154.         context.concatenate(transform)
  155.        
  156.         switch image.imageOrientation {
  157.         case .left, .leftMirrored, .right, .rightMirrored:
  158.             context.draw(cgImage, in: CGRect(x: 0, y: 0, width: image.size.height, height: image.size.width))
  159.         default:
  160.             context.draw(cgImage, in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
  161.         }
  162.        
  163.         guard let newCgImage = context.makeImage() else { return image }
  164.        
  165.         return UIImage(cgImage: newCgImage)
  166.        
  167.     }
  168. }
  169.  
Add Comment
Please, Sign In to add comment