Skip to content

Commit

Permalink
testing QR code scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
s4rv4d committed Mar 7, 2021
1 parent 2584713 commit 9926789
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docWind.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 2.1;
MARKETING_VERSION = 2.2;
PRODUCT_BUNDLE_IDENTIFIER = com.sarvadShetty.docWind;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand All @@ -1142,7 +1142,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 2.1;
MARKETING_VERSION = 2.2;
PRODUCT_BUNDLE_IDENTIFIER = com.sarvadShetty.docWind;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand Down
Binary file not shown.
45 changes: 43 additions & 2 deletions docWind/Views/Main module/Pdf module/AddPdfMainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ struct AddPdfMainView: View {
EditImageview(mainImages: self.$pages, mICopy: self.$pagesCopy, mainImagesCopy: self.pagesCopy, currentImage: self.pagesCopy.first!, currentImageCopy: self.pagesCopy.first!, imageCount: self.pagesCopy.count)
case .scanQR:
// EmptyView()
// CodeScanner(codeTypes: [.qr], simulatedData: "", completion: self.handleScan)
CodeScannerView(codeTypes: [.qr], scanMode: .oncePerCode, simulatedData: "", completion: self.handleScan)
CodeScannerView(codeTypes: [.qr], scanMode: .once, simulatedData: "", completion: self.handleScan)
}
}

.actionSheet(isPresented: $showingActionSheet) {
ActionSheet(title: Text("Options"), message: Text("Choose an option"), buttons: [
.default(Text("Scan a document"), action: scanTapped),
.default(Text("Scan QR/Barcode code"), action: scanQRTapped),
.default(Text("Choose an image"), action: addImagesTapped),
.cancel()
])
Expand All @@ -184,7 +184,44 @@ struct AddPdfMainView: View {

// MARK: - Functions
private func handleScan(result: Result<String, CodeScannerView.ScanError>) {
print(result)

switch result {
case .success(let urlString):
scannedData(string: urlString)
case .failure(let errorEnum) :
print(errorEnum.localizedDescription)

default:
self.activeAlertSheet = .notice
self.alertMessage = "Scanned QR Code didnt contain a valid URL to download PDF from"
self.showAlert.toggle()
}
}

private func scannedData(string: String) {
guard let downloadURL = URL(string: "https://www.tutorialspoint.com/swift/swift_tutorial.pdf") else { fatalError("failed to initialize URL from string.") }
let cgpdfURL = downloadURL as CFURL
guard let pdfDocument = CGPDFDocument(cgpdfURL) else { fatalError("failed to initialize PDFDocument") }

let pageCount = pdfDocument.numberOfPages

for i in 0 ... pageCount {
autoreleasepool {
guard let page = pdfDocument.page(at: i) else { return }
let pageRect = page.getBoxRect(.mediaBox)
let renderer = UIGraphicsImageRenderer(size: pageRect.size)
let image = renderer.image { ctx in
UIColor.white.set()
ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
ctx.fill(pageRect)
ctx.cgContext.drawPDFPage(page)
}

pages.append(image.downSampleImage())
}
}
}

private func approximateFileSize() -> String {
Expand Down Expand Up @@ -270,6 +307,10 @@ struct AddPdfMainView: View {
self.showingActionSheet.toggle()
}

private func scanQRTapped() {
self.activeSheet = .scanQR
}

private func showSubView() {
self.removeWatermark = false
self.activeSheet = .subView
Expand Down

0 comments on commit 9926789

Please sign in to comment.