Skip to content

Commit a30e119

Browse files
committed
add Add parameters to adjust the scan area position
1 parent 0f6bd26 commit a30e119

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

android/src/main/java/com/chavesgu/scan/ScanDrawView.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
import android.util.Log;
1919
import android.view.SurfaceHolder;
2020
import android.view.SurfaceView;
21+
import android.view.ViewGroup;
2122
import android.view.animation.LinearInterpolator;
23+
import android.widget.RelativeLayout;
2224

25+
import java.math.BigDecimal;
2326
import java.util.Map;
2427

2528
import androidx.annotation.Nullable;
@@ -39,6 +42,7 @@ public class ScanDrawView extends SurfaceView implements SurfaceHolder.Callback
3942
private int scanLineColor;
4043
private boolean transparentScanLine = false;
4144
private double scale = .7;
45+
private float up = 80.0f;
4246
private float dpi;
4347
private boolean running;
4448

@@ -54,6 +58,8 @@ public ScanDrawView(Context context, Activity activity, @Nullable Map<String, Ob
5458
final int g = (int) args.get("g");
5559
final int b = (int) args.get("b");
5660
final double alpha = (double) args.get("a");
61+
up = BigDecimal.valueOf((double) args.get("up")).floatValue();
62+
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
5763
final int a = max(0, min(255, (int)floor(alpha * 256.0)));
5864
if (a==0) transparentScanLine = true;
5965
scanLineColor = Color.argb(a, r, g, b);
@@ -75,8 +81,8 @@ public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int width, int he
7581
vw = width;
7682
vh = height;
7783
areaWidth = min(vw, vh) * scale;
78-
areaX = (vw - areaWidth) / 2;
79-
areaY = (vh - areaWidth) / 2;
84+
areaX = (vw - areaWidth) / 2 ;
85+
areaY = (vh - areaWidth) / 2 ;
8086

8187
DisplayMetrics dm = new DisplayMetrics();
8288
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
@@ -128,12 +134,12 @@ private void tryDraw(final SurfaceHolder holder) {
128134

129135
private void drawing(Canvas canvas) {
130136
final float x = (float) areaX;
131-
final float y = (float) areaY;
137+
final float y = (float) areaY - up;
132138
final float width = (float) areaWidth;
133139
final float shortWidth = (float) (areaWidth * 0.1);
134140
final float scanLineWidth = (float) (areaWidth * 0.8);
135141
final float scanLineX = (float) (vw - scanLineWidth)/2;
136-
final float scanLineY = (float) (vh - scanLineWidth)/2;
142+
final float scanLineY = (float) (vh - scanLineWidth)/2 - up;
137143

138144
if (scale < 1) {
139145
Paint paint = new Paint();

android/src/main/java/com/chavesgu/scan/ScanPlatformView.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.util.Log;
88
import android.view.View;
99

10+
import java.math.BigDecimal;
1011
import java.util.Map;
1112

1213
import androidx.annotation.NonNull;
@@ -29,6 +30,7 @@ public class ScanPlatformView implements PlatformView, MethodChannel.MethodCallH
2930
private ScanViewNew scanViewNew;
3031
private ScanDrawView scanDrawView;
3132
private boolean flashlight;
33+
private int up = 80;
3234

3335
ScanPlatformView(@NonNull BinaryMessenger messenger, @NonNull Context context, @NonNull Activity activity, ActivityPluginBinding activityPluginBinding, int viewId, @Nullable Map<String, Object> args) {
3436
channel = new MethodChannel(messenger, "chavesgu/scan/method_"+viewId);
@@ -44,7 +46,7 @@ private void initForBinding(Map<String, Object> args) {
4446
this.scanViewNew.setCaptureListener(this);
4547

4648
this.scanDrawView = new ScanDrawView(context, activity, args);
47-
49+
this.up = BigDecimal.valueOf((double) args.get("up")).intValue();
4850
this.parentView = new ParentView(context);
4951
this.parentView.addView(this.scanViewNew);
5052
this.parentView.addView(this.scanDrawView);

ios/Classes/ScanView.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class ScanView: UIView,AVCaptureMetadataOutputObjectsDelegate,FlutterPlug
3232
var vw:CGFloat = 0;
3333
var vh:CGFloat = 0;
3434
var scale:CGFloat = 0.7;
35+
36+
var up = 80.0 ;
3537

3638
init(_ frame:CGRect, viewId:Int64, args: Any?,registrar: FlutterPluginRegistrar) {
3739
super.init(frame: frame);
@@ -43,6 +45,7 @@ public class ScanView: UIView,AVCaptureMetadataOutputObjectsDelegate,FlutterPlug
4345

4446
let params = args as! NSDictionary;
4547
self.scale = params["scale"] as! CGFloat;
48+
self.up = params["up"] as! CGFloat;
4649
let r = params["r"] as! CGFloat;
4750
let g = params["g"] as! CGFloat;
4851
let b = params["b"] as! CGFloat;
@@ -155,7 +158,7 @@ public class ScanView: UIView,AVCaptureMetadataOutputObjectsDelegate,FlutterPlug
155158
let scale:CGFloat = self.scale;
156159
let areaWidth = min(self.vw, self.vh) * scale;
157160
let x = (self.vw - areaWidth) / 2;
158-
let y = (self.vh - areaWidth) / 2;
161+
let y = (self.vh - areaWidth) / 2 - up;
159162
let shortWidth:CGFloat = areaWidth * 0.1;
160163
let joinWidth:CGFloat = 0.5;
161164

@@ -221,7 +224,7 @@ public class ScanView: UIView,AVCaptureMetadataOutputObjectsDelegate,FlutterPlug
221224
let scanPath = UIBezierPath();
222225
let scanLineWidth = areaWidth * 0.8;
223226
let scanLineX = (self.vw - scanLineWidth) / 2;
224-
let scanLineY = (self.vh - scanLineWidth) / 2;
227+
let scanLineY = (self.vh - scanLineWidth) / 2 - up;
225228
scanPath.move(to: CGPoint(x: scanLineX, y: scanLineY));
226229
scanPath.addLine(to: CGPoint(x: scanLineX + scanLineWidth, y: scanLineY));
227230
let scanShapeLayer = CAShapeLayer();

lib/scan.dart

+4
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ class ScanView extends StatefulWidget {
2323
this.onCapture,
2424
this.scanLineColor = Colors.green,
2525
this.scanAreaScale = 0.7,
26+
this.horizontalHeight = 80.0,
2627
}) : assert(scanAreaScale <= 1.0, 'scanAreaScale must <= 1.0'),
2728
assert(scanAreaScale > 0.0, 'scanAreaScale must > 0.0');
2829

2930
final ScanController? controller;
3031
final CaptureCallback? onCapture;
3132
final Color scanLineColor;
3233
final double scanAreaScale;
34+
final double horizontalHeight;
3335

3436
@override
3537
State<StatefulWidget> createState() => _ScanViewState();
@@ -50,6 +52,7 @@ class _ScanViewState extends State<ScanView> {
5052
"b": widget.scanLineColor.blue,
5153
"a": widget.scanLineColor.opacity,
5254
"scale": widget.scanAreaScale,
55+
"up": widget.horizontalHeight,
5356
},
5457
onPlatformViewCreated: (id) {
5558
_onPlatformViewCreated(id);
@@ -65,6 +68,7 @@ class _ScanViewState extends State<ScanView> {
6568
"b": widget.scanLineColor.blue,
6669
"a": widget.scanLineColor.opacity,
6770
"scale": widget.scanAreaScale,
71+
"up": widget.horizontalHeight,
6872
},
6973
onPlatformViewCreated: (id) {
7074
_onPlatformViewCreated(id);

0 commit comments

Comments
 (0)