Skip to content

Commit 6f0a44e

Browse files
committed
feat: add PDF representations
1 parent 965cc3c commit 6f0a44e

File tree

6 files changed

+78
-17
lines changed

6 files changed

+78
-17
lines changed

Classes/TiScannerModule.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,14 @@
1414
VNDocumentCameraScan *_currentScan;
1515
}
1616

17+
- (NSNumber *)isSupported:(id)unused;
18+
19+
- (void)showScanner:(id)value;
20+
21+
- (TiBlob *)imageOfPageAtIndex:(id)index;
22+
23+
- (TiBlob *)pdfOfPageAtIndex:(id)index;
24+
25+
- (TiBlob *)pdfOfAllPages:(id)unused;
26+
1727
@end

Classes/TiScannerModule.m

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Copyright (c) 2019 Your Company. All rights reserved.
66
*/
77

8+
#import <PDFKit/PDFKit.h>
89
#import <TitaniumKit/TitaniumKit.h>
910
#import "TiScannerModule.h"
1011
#import "TiBase.h"
@@ -51,6 +52,40 @@ - (TiBlob *)imageOfPageAtIndex:(id)index
5152
return blob;
5253
}
5354

55+
- (TiBlob *)pdfOfPageAtIndex:(id)index
56+
{
57+
ENSURE_SINGLE_ARG(index, NSNumber);
58+
59+
if (_currentScan == nil) {
60+
return nil;
61+
}
62+
63+
UIImage *image = [_currentScan imageOfPageAtIndex:[(NSNumber *)index integerValue]];
64+
65+
PDFDocument *pdfDocument = [PDFDocument new];
66+
PDFPage *pdfPage = [[PDFPage alloc] initWithImage:image];
67+
[pdfDocument insertPage:pdfPage atIndex:0];
68+
69+
return [[TiBlob alloc] initWithData:[pdfDocument dataRepresentation] mimetype:@"application/pdf"];
70+
}
71+
72+
- (TiBlob *)pdfOfAllPages:(id)unused
73+
{
74+
if (_currentScan == nil) {
75+
return nil;
76+
}
77+
78+
PDFDocument *pdfDocument = [PDFDocument new];
79+
80+
for (NSUInteger index = 0; index < _currentScan.pageCount; index++) {
81+
UIImage *image = [_currentScan imageOfPageAtIndex:index];
82+
PDFPage *pdfPage = [[PDFPage alloc] initWithImage:image];
83+
[pdfDocument insertPage:pdfPage atIndex:index];
84+
}
85+
86+
return [[TiBlob alloc] initWithData:[pdfDocument dataRepresentation] mimetype:@"application/pdf"];
87+
}
88+
5489
#pragma mark Utils
5590

5691
- (VNDocumentCameraViewController *)scannerViewController

README.md

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,39 @@ document.
88

99
## Requirements
1010

11-
- [x] Titanium SDK 8.2.0+
1211
- [x] iOS 13+
12+
- [x] Titanium SDK 8.2.0+
13+
- [x] Granted camera permissions
14+
15+
## APIs
16+
17+
### Methods
18+
19+
- [x] `showScanner`
20+
- [x] `imageOfPageAtIndex(index)` (after the `success` event)
21+
- [x] `pdfOfPageAtIndex(index)` (after the `success` event)
22+
- [x] `pdfOfAllPages()` (after the `success` event)
23+
24+
### Events
25+
26+
- [x] `success`
27+
- [x] `error`
28+
- [x] `cancel`
1329

1430
## Example
1531

1632
```js
17-
var Scanner = require('ti.scanner');
33+
import Scanner from 'ti.scanner';
1834

19-
var win = Ti.UI.createWindow({
35+
const win = Ti.UI.createWindow({
2036
backgroundColor: '#fff'
2137
});
2238

23-
var btn = Ti.UI.createButton({
39+
const btn = Ti.UI.createButton({
2440
title: 'Scan Document'
2541
});
2642

27-
btn.addEventListener('click', function () {
43+
btn.addEventListener('click', () => {
2844
Ti.Media.requestCameraPermissions(event => {
2945
if (!event.success) {
3046
alert('No camera permissions');
@@ -34,26 +50,26 @@ btn.addEventListener('click', function () {
3450
});
3551
});
3652

37-
Scanner.addEventListener('cancel', function () {
53+
Scanner.addEventListener('cancel', () => {
3854
Ti.API.warn('Cancelled …');
3955
});
4056

41-
Scanner.addEventListener('error', function (event) {
57+
Scanner.addEventListener('error', event => {
4258
Ti.API.error('Errored …');
4359
Ti.API.error(event.error);
4460
});
4561

46-
Scanner.addEventListener('success', function (event) {
62+
Scanner.addEventListener('success', event => {
4763
Ti.API.warn('Succeeded …');
4864
Ti.API.warn(event);
4965

50-
var win2 = Ti.UI.createWindow({
51-
backgroundColor: '#333'
66+
const win2 = Ti.UI.createWindow({
67+
backgroundColor: '#333'
5268
});
5369

54-
var image = Ti.UI.createImageView({
55-
height: '70%',
56-
image: Scanner.imageOfPageAtIndex(0) /* Or many images via "event.count" */
70+
const image = Ti.UI.createImageView({
71+
height: '70%',
72+
image: Scanner.imageOfPageAtIndex(0) /* Or pdfOfPageAtIndex(0) if you need the PDF of it, or many images via "event.count" */
5773
});
5874

5975
win2.add(image);

manifest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
# this is your module manifest and used by Titanium
33
# during compilation, packaging, distribution, etc.
44
#
5-
version: 2.0.0
5+
version: 2.1.0
66
apiversion: 2
77
architectures: arm64 x86_64
88
description: titanium-scanner
99
author: Hans Knöchel
1010
license: MIT
11-
copyright: Copyright (c) 2019 by Hans Knöchel
11+
copyright: Copyright (c) 2019-present by Hans Knöchel
1212

1313
# these should not be edited
1414
name: titanium-scanner

module.xcconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
// Note: Titanium SDK 6.2.2+ detects and links frameworks automatically
1313
//
1414

15-
OTHER_LDFLAGS=$(inherited) -weak_framework Vision -weak_framework VisionKit
15+
OTHER_LDFLAGS=$(inherited) -weak_framework Vision -weak_framework VisionKit -weak_framework PDFKit

titanium-scanner.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
0867D690FE84028FC02AAC07 /* Project object */ = {
150150
isa = PBXProject;
151151
attributes = {
152-
LastUpgradeCheck = 1240;
152+
LastUpgradeCheck = 1250;
153153
};
154154
buildConfigurationList = 1DEB922208733DC00010E9CD /* Build configuration list for PBXProject "titanium-scanner" */;
155155
compatibilityVersion = "Xcode 9.3";

0 commit comments

Comments
 (0)