Skip to content

Commit 3e373b5

Browse files
committed
Merge branch 'release/2.1.0'
2 parents 0732a22 + 12af9b9 commit 3e373b5

File tree

7 files changed

+238
-31
lines changed

7 files changed

+238
-31
lines changed

ExampleProject/ExampleProject/ModalScannerView.swift

+12-1
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,19 @@ struct ModalScannerView: View {
5757
cameraPosition: $cameraPosition
5858
){
5959
print($0)
60+
}
61+
onDraw: {
62+
print("Preview View Size = \($0.cameraPreviewView.bounds)")
63+
print("Barcode Corners = \($0.corners)")
64+
65+
let lineColor = UIColor.green
66+
let fillColor = UIColor(red: 0, green: 1, blue: 0.2, alpha: 0.4)
67+
68+
//Draw Barcode corner
69+
$0.draw(lineWidth: 1, lineColor: lineColor, fillColor: fillColor)
70+
6071
}.frame(minWidth: 0, maxWidth: .infinity, minHeight: 400, maxHeight: 400, alignment: .topLeading)
61-
72+
6273
Spacer()
6374

6475
Text(barcodeValue)

README.md

+48-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# CarBode
66
## Free and Opensource Barcode scanner & Barcode generator for swiftUI
77

8+
![CarBodeDemo](https://raw.githubusercontent.com/heart/CarBode-Barcode-Scanner-For-SwiftUI/master/logo/demo.gif)
9+
810
# Why you must use CarBode
911
1. CarBode have both Barcode Scanner and Barcode Generator
1012
1. CarBode is a lightweight components
@@ -18,10 +20,12 @@
1820
- [Example project](#example-project)
1921
- [How to use scanner view](#how-to-use-scanner-view)
2022
- [Add camera usage description to `info.plist`](#add-camera-usage-description-to-your-infoplist)
23+
- [Simple Sscanner](#simple-scanner)
24+
- [Draw box around the barcode](#draw-box-around-the-barcode)
2125
- [torch light on/off](#how-to-turn-torch-light-onoff)
22-
- [Test on iOS simulator](#test-on-ios-simulator)
23-
- [Barcode types support](#barcode-types-support)
2426
- [Switch front/back camera](#switch-to-front-camera)
27+
- [Barcode types support](#barcode-types-support)
28+
- [Test on iOS simulator](#test-on-ios-simulator)
2529
- [How to use barcode generator view](#how-to-use-barcode-generator-view)
2630
- [Barcode type you can generate](#barcode-type-you-can-generate)
2731
- [Rotate your barcode](#rotate-your-barcode)
@@ -35,7 +39,7 @@ The preferred way of installing SwiftUIX is via the [Swift Package Manager](http
3539
3640
1. In Xcode, open your project and navigate to **File****Swift Packages****Add Package Dependency...**
3741
2. Paste the repository URL (`https://github.com/heart/CarBode-Barcode-Scanner-For-SwiftUI`) and click **Next**.
38-
3. For **Rules**, select **Branch** (with branch set to `2.0.1` ).
42+
3. For **Rules**, select **Branch** (with branch set to `2.1.0` ).
3943
4. Click **Finish**.
4044

4145
# Example project
@@ -53,6 +57,7 @@ CarBode-Barcode-Scanner-For-SwiftUI/ExampleProject/ExampleProject.xcodeproj
5357
<string>This app needs access to the camera, to be able to read barcodes.</string>
5458
```
5559

60+
# Simple Scanner
5661
```Swift
5762
import SwiftUI
5863
import CarBode
@@ -76,6 +81,44 @@ struct ContentView: View {
7681
}
7782
```
7883

84+
# Draw box around the barcode
85+
```Swift
86+
import SwiftUI
87+
import CarBode
88+
import AVFoundation //import to access barcode types you want to scan
89+
90+
struct ContentView: View {
91+
var body: some View {
92+
VStack{
93+
CBScanner(
94+
supportBarcode: .constant([.qr, .code128]), //Set type of barcode you want to scan
95+
scanInterval: .constant(5.0) //Event will trigger every 5 seconds
96+
){
97+
//When the scanner found a barcode
98+
print($0.value)
99+
print("Barcode Type is", $0.type.rawValue)
100+
}
101+
onDraw: {
102+
print("Preview View Size = \($0.cameraPreviewView.bounds)")
103+
print("Barcode Corners = \($0.corners)")
104+
105+
//line width
106+
let lineWidth = 2
107+
108+
//line color
109+
let lineColor = UIColor.red
110+
111+
//Fill color with opacity
112+
//You also can use UIColor.clear if you don't want to draw fill color
113+
let fillColor = UIColor(red: 0, green: 1, blue: 0.2, alpha: 0.4)
114+
115+
//Draw box
116+
$0.draw(lineWidth: lineWidth, lineColor: lineColor, fillColor: fillColor)
117+
}
118+
}
119+
}
120+
```
121+
79122
# How to turn torch light on/off
80123
```Swift
81124
import SwiftUI
@@ -242,6 +285,8 @@ CBBarcodeView(data: ..... ,
242285
CarBode welcomes contributions in the form of GitHub issues and pull-requests.
243286

244287
## Changelog
288+
- 2.1.0 You can draw a box around the barcode
289+
- 2.0.1 Fixed bugs
245290
- 2.0.0 I learned many more things about SwiftUI then I decide to restructure the scanner I hope you will like it. And this version you can switch front and back camera.
246291
- 1.5.0 Fixed bugs and you can read the barcode type when scanner found it
247292
- 1.4.0 Rename component and add new barcode generator view component

Sources/CarBode/BarcodeData.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import Foundation
1+
22
import AVFoundation
3+
import UIKit
34

45
public struct BarcodeData {
56
public let value: String
67
public let type: AVMetadataObject.ObjectType
7-
8-
public init(value: String, type: AVMetadataObject.ObjectType){
8+
9+
public init(value: String, type: AVMetadataObject.ObjectType) {
910
self.value = value
1011
self.type = type
1112
}
13+
1214
}

Sources/CarBode/BarcodeFrame.swift

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// BarcodeFrame.swift
3+
//
4+
//
5+
// Created by weera Kaew-uan on 12/10/2563 BE.
6+
//
7+
8+
import UIKit
9+
10+
public struct BarcodeFrame {
11+
12+
public let corners:[CGPoint]
13+
public let cameraPreviewView: UIView
14+
15+
public func draw(lineWidth: CGFloat = 1, lineColor: UIColor = UIColor.red, fillColor: UIColor = UIColor.clear) {
16+
17+
let view = cameraPreviewView as! CameraPreview
18+
19+
view.drawFrame(corners: corners,
20+
lineWidth: lineWidth,
21+
lineColor: lineColor,
22+
fillColor: fillColor)
23+
}
24+
25+
26+
27+
}

Sources/CarBode/CBScanner.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import AVFoundation
1414
public struct CBScanner: UIViewRepresentable {
1515

1616
public typealias OnFound = (BarcodeData)->Void
17+
public typealias OnDraw = (BarcodeFrame)->Void
18+
1719
public typealias UIViewType = CameraPreview
1820

1921
@Binding
@@ -32,20 +34,23 @@ public struct CBScanner: UIViewRepresentable {
3234
public var mockBarCode: BarcodeData
3335

3436
public var onFound: OnFound?
37+
public var onDraw: OnDraw?
3538

3639
public init(supportBarcode:Binding<[AVMetadataObject.ObjectType]> ,
3740
torchLightIsOn: Binding<Bool> = .constant(false),
3841
scanInterval: Binding<Double> = .constant(3.0),
3942
cameraPosition: Binding<AVCaptureDevice.Position> = .constant(.back),
4043
mockBarCode: Binding<BarcodeData> = .constant(BarcodeData(value: "barcode value", type: .qr)),
41-
onFound: @escaping OnFound
44+
onFound: @escaping OnFound,
45+
onDraw: OnDraw? = nil
4246
) {
4347
_torchLightIsOn = torchLightIsOn
4448
_supportBarcode = supportBarcode
4549
_scanInterval = scanInterval
4650
_cameraPosition = cameraPosition
4751
_mockBarCode = mockBarCode
4852
self.onFound = onFound
53+
self.onDraw = onDraw
4954
}
5055

5156
public func makeUIView(context: UIViewRepresentableContext<CBScanner>) -> CBScanner.UIViewType {
@@ -54,6 +59,7 @@ public struct CBScanner: UIViewRepresentable {
5459
view.supportBarcode = supportBarcode
5560
view.setupScanner()
5661
view.onFound = onFound
62+
view.onDraw = onDraw
5763
return view
5864
}
5965

0 commit comments

Comments
 (0)