Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: There's a bug where the output screen turns 90 degrees, Screen has different left and right spacing #20

4 changes: 2 additions & 2 deletions Sudoku.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UIMainStoryboardFile = Main;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -406,7 +406,7 @@
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UIMainStoryboardFile = Main;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
36 changes: 12 additions & 24 deletions Sudoku/photoSudoku.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,31 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="cLG-Qq-7gK">
<rect key="frame" x="32" y="44" width="350" height="350"/>
<constraints>
<constraint firstAttribute="height" constant="350" id="86n-fo-x4u"/>
<constraint firstAttribute="width" constant="350" id="WfO-MX-1mn"/>
</constraints>
</imageView>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="aep-Ba-2AR">
<rect key="frame" x="32" y="414" width="350" height="350"/>
<constraints>
<constraint firstAttribute="height" constant="350" id="RV5-WP-Xq9"/>
<constraint firstAttribute="width" constant="350" id="sfj-Im-cfp"/>
</constraints>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="cLG-Qq-7gK">
<rect key="frame" x="14" y="44" width="380" height="380"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="M9s-cg-Kfi">
<rect key="frame" x="174" y="803" width="67" height="31"/>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fgX-J8-1nV">
<rect key="frame" x="174" y="380" width="67" height="31"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="filled" title="Button"/>
<buttonConfiguration key="configuration" style="plain" title="Button"/>
<connections>
<action selector="shootingAction:" destination="Y6W-OH-hqX" eventType="touchUpInside" id="quG-Za-zbU"/>
<action selector="shootingAction:" destination="Y6W-OH-hqX" eventType="touchUpInside" id="Tgp-o7-5va"/>
</connections>
</button>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="aep-Ba-2AR">
<rect key="frame" x="14" y="432" width="380" height="380"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
</subviews>
<viewLayoutGuide key="safeArea" id="vDu-zF-Fre"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="cLG-Qq-7gK" firstAttribute="centerX" secondItem="5EZ-qb-Rvc" secondAttribute="centerX" id="bIQ-sF-dxh"/>
<constraint firstItem="cLG-Qq-7gK" firstAttribute="top" secondItem="vDu-zF-Fre" secondAttribute="top" id="ihF-c4-y4I"/>
<constraint firstItem="aep-Ba-2AR" firstAttribute="centerX" secondItem="5EZ-qb-Rvc" secondAttribute="centerX" id="mbK-xa-zGA"/>
<constraint firstItem="aep-Ba-2AR" firstAttribute="top" secondItem="cLG-Qq-7gK" secondAttribute="bottom" constant="20" id="uav-pV-ucu"/>
</constraints>
</view>
<connections>
<outlet property="cameraView" destination="cLG-Qq-7gK" id="3EJ-eq-Spp"/>
<outlet property="refinedView" destination="aep-Ba-2AR" id="Vx1-kn-Ca8"/>
<outlet property="shooting" destination="M9s-cg-Kfi" id="Y63-fI-FWy"/>
<outlet property="shooting" destination="fgX-J8-1nV" id="HX8-Xv-CvC"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Ief-a0-LHa" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
Expand Down
10 changes: 5 additions & 5 deletions Sudoku/photoSudokuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ final class photoSudokuViewController: UIViewController, AVCaptureVideoDataOutpu
session?.startRunning()

}

@IBAction func shootingAction(_ sender: Any) {
if check {
start()
check = false
}
else {
sudokuSolvingWorkItem = DispatchWorkItem(block: self.sudokuSolvingQueue)
sudokuSolvingWorkItem = DispatchWorkItem(block: sudokuSolvingQueue)
DispatchQueue.main.async(execute: sudokuSolvingWorkItem!)
stop()
check = true
}
Expand Down Expand Up @@ -77,7 +78,7 @@ final class photoSudokuViewController: UIViewController, AVCaptureVideoDataOutpu

previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
previewLayer?.frame = cameraView.frame
previewLayer?.frame = cameraView.bounds
cameraView.layer.addSublayer(previewLayer!)
} catch {

Expand All @@ -93,7 +94,7 @@ final class photoSudokuViewController: UIViewController, AVCaptureVideoDataOutpu
*/
func captureOutput(_ output: AVCaptureOutput, didOutput buffer: CMSampleBuffer, from connection: AVCaptureConnection) {
//기기의 현재 방향에 따라 화면의 방향도 돌려준다.
connection.videoOrientation = AVCaptureVideoOrientation(rawValue: UIDevice.current.orientation.rawValue) ?? AVCaptureVideoOrientation.portrait
connection.videoOrientation = AVCaptureVideoOrientation.portrait


/*
Expand Down Expand Up @@ -139,7 +140,6 @@ final class photoSudokuViewController: UIViewController, AVCaptureVideoDataOutpu
let r = CGRect(x: 0, y: y, width: w, height: w)
let imgCrop = img.cgImage?.cropping(to: r)
let refinedImage = UIImage(cgImage: imgCrop!)

self.toRefinedView(refinedImage)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sudoku/pickerSudoku.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<action selector="shootSolSudoku:" destination="Y6W-OH-hqX" eventType="touchUpInside" id="cvh-q7-IA8"/>
</connections>
</button>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="UYs-1J-XfZ">
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="UYs-1J-XfZ">
<rect key="frame" x="43" y="116" width="328" height="329"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
Expand Down
84 changes: 79 additions & 5 deletions Sudoku/pickerSudokuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by 이주화 on 2022/09/14.
//
import UIKit
import AVFoundation
import CoreML
import Vision

Expand All @@ -15,24 +16,31 @@ class pickerSudokuViewController: UIViewController {

@IBOutlet weak var pickerImage: UIImageView!

let picker = UIImagePickerController()

var sudokuSolvingWorkItem: DispatchWorkItem?
var count:Int = 0
let picker = UIImagePickerController()

override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
// Do any additional setup after loading the view.
}



@IBAction func shootPhotoPicker(_ sender: UIButton) {
let alert = UIAlertController(title: "Select", message: nil, preferredStyle: .actionSheet)
let library = UIAlertAction(title: "Album", style: .default) { (action) in
self.openLibrary()
}
let camera = UIAlertAction(title: "Camera", style: .default) { (action) in
self.openCamera()
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

alert.addAction(library)
alert.addAction(camera)
alert.addAction(cancel)

present(alert, animated: true, completion: nil)
Expand All @@ -41,7 +49,7 @@ class pickerSudokuViewController: UIViewController {
@IBAction func shootSolSudoku(_ sender: Any) {
if pickerImage.image != nil {
sudokuSolvingWorkItem = DispatchWorkItem(block: self.sudokuSolvingQueue)
self.recognizeNum(image: pickerImage.image!)
DispatchQueue.main.async(execute: sudokuSolvingWorkItem!)
} else {
let alret = UIAlertController(title: "사진이 업로드 되지 않았습니다.", message: "사진을 업로드 하시겠습니까?", preferredStyle: .alert)
let yes = UIAlertAction(title: "네", style: .default) { (action) in
Expand Down Expand Up @@ -166,19 +174,85 @@ class pickerSudokuViewController: UIViewController {
extension pickerSudokuViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func openLibrary() {
picker.sourceType = .photoLibrary
present(picker, animated: false, completion: nil)

self.present(picker, animated: false, completion: nil)
}

func openCamera() {
picker.sourceType = .camera
self.present(picker, animated: false, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
picker.dismiss(animated: true)
return
}
if let detectRectangle = wrapper.detectRectangle(image){
let fixOrientationImage = image.fixOrientation()

if let detectRectangle = wrapper.detectRectangle(fixOrientationImage){
pickerImage.image = detectRectangle[1] as? UIImage
}
// self.pickerImage.image = image
picker.dismiss(animated: true)
}

}

extension UIImage {

func fixOrientation() -> UIImage {

// 이미지의 방향이 올바를 경우 수정하지 않는다.
if ( self.imageOrientation == UIImage.Orientation.up ) {
return self;
}

// 이미지를 변환시키기 위한 함수 선언
var transform: CGAffineTransform = CGAffineTransform.identity

// 이미지의 상태에 맞게 이미지를 돌린다.
if ( self.imageOrientation == UIImage.Orientation.down || self.imageOrientation == UIImage.Orientation.downMirrored ) {
transform = transform.translatedBy(x: self.size.width, y: self.size.height)
transform = transform.rotated(by: CGFloat(Double.pi))
}

if ( self.imageOrientation == UIImage.Orientation.left || self.imageOrientation == UIImage.Orientation.leftMirrored ) {
transform = transform.translatedBy(x: self.size.width, y: 0)
transform = transform.rotated(by: CGFloat(Double.pi / 2.0))
}

if ( self.imageOrientation == UIImage.Orientation.right || self.imageOrientation == UIImage.Orientation.rightMirrored ) {
transform = transform.translatedBy(x: 0, y: self.size.height);
transform = transform.rotated(by: CGFloat(-Double.pi / 2.0));
}

if ( self.imageOrientation == UIImage.Orientation.upMirrored || self.imageOrientation == UIImage.Orientation.downMirrored ) {
transform = transform.translatedBy(x: self.size.width, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
}

if ( self.imageOrientation == UIImage.Orientation.leftMirrored || self.imageOrientation == UIImage.Orientation.rightMirrored ) {
transform = transform.translatedBy(x: self.size.height, y: 0);
transform = transform.scaledBy(x: -1, y: 1);
}

// 이미지 변환용 값 선언
let cgValue: CGContext = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height),
bitsPerComponent: self.cgImage!.bitsPerComponent, bytesPerRow: 0,
space: self.cgImage!.colorSpace!,
bitmapInfo: self.cgImage!.bitmapInfo.rawValue)!;

cgValue.concatenate(transform)

if ( self.imageOrientation == UIImage.Orientation.left ||
self.imageOrientation == UIImage.Orientation.leftMirrored ||
self.imageOrientation == UIImage.Orientation.right ||
self.imageOrientation == UIImage.Orientation.rightMirrored ) {
cgValue.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.height,height: self.size.width))
} else {
cgValue.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.width,height: self.size.height))
}

return UIImage(cgImage: cgValue.makeImage()!)
}
}