From fe4c9b981d2f47bc7576c0a69487bd65c381e097 Mon Sep 17 00:00:00 2001 From: Pasha Shkaran Date: Mon, 27 Nov 2017 17:49:27 +0200 Subject: [PATCH 1/2] Task 282 --- .../utils/BarcodeScannerView.java | 19 +++++ .../communicator/utils/ViewFinderView.java | 77 ++++++++++--------- 2 files changed, 60 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/nynja/mobile/communicator/utils/BarcodeScannerView.java b/app/src/main/java/com/nynja/mobile/communicator/utils/BarcodeScannerView.java index 2278ca7f8a..5e30b29426 100644 --- a/app/src/main/java/com/nynja/mobile/communicator/utils/BarcodeScannerView.java +++ b/app/src/main/java/com/nynja/mobile/communicator/utils/BarcodeScannerView.java @@ -8,6 +8,7 @@ import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.hardware.Camera.PreviewCallback; import android.hardware.Camera.Size; +import android.support.annotation.ColorInt; import android.util.AttributeSet; import android.view.View; import android.widget.FrameLayout; @@ -36,6 +37,8 @@ public abstract class BarcodeScannerView extends FrameLayout implements PreviewC private int mViewFinderOffset; private float mAspectTolerance; private static final int FOCUS_AREA_SIZE = 300; + private boolean mIsLaserEnabled = true; + @ColorInt private int mLaserColor = getResources().getColor(R.color.viewfinder_laser); public BarcodeScannerView(Context context) { super(context); @@ -65,6 +68,8 @@ public abstract class BarcodeScannerView extends FrameLayout implements PreviewC this.mCornerRadius = a.getDimensionPixelSize(styleable.BarcodeScannerView_cornerRadius, this.mCornerRadius); this.mBorderAlpha = a.getFloat(styleable.BarcodeScannerView_borderAlpha, this.mBorderAlpha); this.mViewFinderOffset = a.getDimensionPixelSize(styleable.BarcodeScannerView_finderOffset, this.mViewFinderOffset); + mIsLaserEnabled = a.getBoolean(R.styleable.BarcodeScannerView_laserEnabled, mIsLaserEnabled); + mLaserColor = a.getColor(R.styleable.BarcodeScannerView_laserColor, mLaserColor); } finally { a.recycle(); } @@ -109,9 +114,23 @@ public abstract class BarcodeScannerView extends FrameLayout implements PreviewC viewFinderView.setBorderCornerRadius(0); viewFinderView.setSquareViewFinder(true); viewFinderView.setViewFinderOffset(0); + viewFinderView.setLaserColor(mLaserColor); + viewFinderView.setLaserEnabled(mIsLaserEnabled); return viewFinderView; } + public void setLaserColor(int laserColor) { + mLaserColor = laserColor; + mViewFinderView.setLaserColor(mLaserColor); + mViewFinderView.setupViewFinder(); + } + + public void setLaserEnabled(boolean isLaserEnabled) { + mIsLaserEnabled = isLaserEnabled; + mViewFinderView.setLaserEnabled(mIsLaserEnabled); + mViewFinderView.setupViewFinder(); + } + public void startCamera(int cameraId) { if (this.mCameraHandlerThread == null) { this.mCameraHandlerThread = new CameraHandlerThread(this); diff --git a/app/src/main/java/com/nynja/mobile/communicator/utils/ViewFinderView.java b/app/src/main/java/com/nynja/mobile/communicator/utils/ViewFinderView.java index 9575cbd31a..c9b60a7cf9 100644 --- a/app/src/main/java/com/nynja/mobile/communicator/utils/ViewFinderView.java +++ b/app/src/main/java/com/nynja/mobile/communicator/utils/ViewFinderView.java @@ -19,7 +19,8 @@ import me.dm7.barcodescanner.core.R.integer; public class ViewFinderView extends View implements IViewFinder { private Rect mFramingRect; - private static final int[] SCANNER_ALPHA = new int[]{0, 64, 128, 192, 255, 192, 128, 64}; + private static final int[] SCANNER_ALPHA = new int[]{0, 42, 63, 74, 84, 96, 106, 128, 136, 147, 152, 168, 189, + 210, 231, 255, 242, 231, 222, 210, 200, 189, 177, 168, 147, 128, 106, 84, 63, 42, 21, 0, 21, 42, 63, 84, 106}; private int scannerAlpha; private final int mDefaultLaserColor; private final int mDefaultMaskColor; @@ -33,6 +34,7 @@ public class ViewFinderView extends View implements IViewFinder { protected boolean mSquareViewFinder; private boolean mIsLaserEnabled; private int mViewFinderOffset; + private int currentHeight = 10; public ViewFinderView(Context context) { super(context); @@ -65,7 +67,7 @@ public class ViewFinderView extends View implements IViewFinder { this.mBorderPaint = new Paint(); this.mBorderPaint.setColor(this.mDefaultBorderColor); this.mBorderPaint.setStyle(Style.STROKE); - this.mBorderPaint.setStrokeWidth((float)this.mDefaultBorderStrokeWidth); + this.mBorderPaint.setStrokeWidth((float) this.mDefaultBorderStrokeWidth); this.mBorderPaint.setAntiAlias(true); this.mBorderLineLength = this.mDefaultBorderLineLength; } @@ -83,7 +85,7 @@ public class ViewFinderView extends View implements IViewFinder { } public void setBorderStrokeWidth(int borderStrokeWidth) { - this.mBorderPaint.setStrokeWidth((float)borderStrokeWidth); + this.mBorderPaint.setStrokeWidth((float) borderStrokeWidth); } public void setBorderLineLength(int borderLineLength) { @@ -95,7 +97,7 @@ public class ViewFinderView extends View implements IViewFinder { } public void setBorderCornerRounded(boolean isBorderCornersRounded) { - if(isBorderCornersRounded) { + if (isBorderCornersRounded) { this.mBorderPaint.setStrokeJoin(Join.ROUND); } else { this.mBorderPaint.setStrokeJoin(Join.BEVEL); @@ -104,12 +106,12 @@ public class ViewFinderView extends View implements IViewFinder { } public void setBorderAlpha(float alpha) { - int colorAlpha = (int)(255.0F * alpha); + int colorAlpha = (int) (255.0F * alpha); this.mBorderPaint.setAlpha(colorAlpha); } public void setBorderCornerRadius(int borderCornersRadius) { - this.mBorderPaint.setPathEffect(new CornerPathEffect((float)borderCornersRadius)); + this.mBorderPaint.setPathEffect(new CornerPathEffect((float) borderCornersRadius)); } public void setViewFinderOffset(int offset) { @@ -130,9 +132,9 @@ public class ViewFinderView extends View implements IViewFinder { } public void onDraw(Canvas canvas) { - if(this.getFramingRect() != null) { + if (this.getFramingRect() != null) { this.drawViewFinderBorder(canvas); - if(this.mIsLaserEnabled) { + if (this.mIsLaserEnabled) { this.drawLaser(canvas); } @@ -142,31 +144,34 @@ public class ViewFinderView extends View implements IViewFinder { public void drawViewFinderBorder(Canvas canvas) { Rect framingRect = this.getFramingRect(); Path path = new Path(); - path.moveTo((float)framingRect.left, (float)(framingRect.top + this.mBorderLineLength)); - path.lineTo((float)framingRect.left, (float)framingRect.top); - path.lineTo((float)(framingRect.left + this.mBorderLineLength), (float)framingRect.top); + path.moveTo((float) framingRect.left, (float) (framingRect.top + this.mBorderLineLength)); + path.lineTo((float) framingRect.left, (float) framingRect.top); + path.lineTo((float) (framingRect.left + this.mBorderLineLength), (float) framingRect.top); canvas.drawPath(path, this.mBorderPaint); - path.moveTo((float)framingRect.right, (float)(framingRect.top + this.mBorderLineLength)); - path.lineTo((float)framingRect.right, (float)framingRect.top); - path.lineTo((float)(framingRect.right - this.mBorderLineLength), (float)framingRect.top); + path.moveTo((float) framingRect.right, (float) (framingRect.top + this.mBorderLineLength)); + path.lineTo((float) framingRect.right, (float) framingRect.top); + path.lineTo((float) (framingRect.right - this.mBorderLineLength), (float) framingRect.top); canvas.drawPath(path, this.mBorderPaint); - path.moveTo((float)framingRect.right, (float)(framingRect.bottom - this.mBorderLineLength)); - path.lineTo((float)framingRect.right, (float)framingRect.bottom); - path.lineTo((float)(framingRect.right - this.mBorderLineLength), (float)framingRect.bottom); + path.moveTo((float) framingRect.right, (float) (framingRect.bottom - this.mBorderLineLength)); + path.lineTo((float) framingRect.right, (float) framingRect.bottom); + path.lineTo((float) (framingRect.right - this.mBorderLineLength), (float) framingRect.bottom); canvas.drawPath(path, this.mBorderPaint); - path.moveTo((float)framingRect.left, (float)(framingRect.bottom - this.mBorderLineLength)); - path.lineTo((float)framingRect.left, (float)framingRect.bottom); - path.lineTo((float)(framingRect.left + this.mBorderLineLength), (float)framingRect.bottom); + path.moveTo((float) framingRect.left, (float) (framingRect.bottom - this.mBorderLineLength)); + path.lineTo((float) framingRect.left, (float) framingRect.bottom); + path.lineTo((float) (framingRect.left + this.mBorderLineLength), (float) framingRect.bottom); canvas.drawPath(path, this.mBorderPaint); } public void drawLaser(Canvas canvas) { Rect framingRect = this.getFramingRect(); - this.mLaserPaint.setAlpha(SCANNER_ALPHA[this.scannerAlpha]); - this.scannerAlpha = (this.scannerAlpha + 1) % SCANNER_ALPHA.length; - int middle = framingRect.height() / 2 + framingRect.top; - canvas.drawRect((float)(framingRect.left + 2), (float)(middle - 1), (float)(framingRect.right - 1), (float)(middle + 2), this.mLaserPaint); - this.postInvalidateDelayed(80L, framingRect.left - 10, framingRect.top - 10, framingRect.right + 10, framingRect.bottom + 10); + mLaserPaint.setAlpha(SCANNER_ALPHA[scannerAlpha]); + scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length; + currentHeight = currentHeight + 6; + if (currentHeight >= framingRect.height() - 10) { + currentHeight = 0; + } + canvas.drawRect((float) (framingRect.left + 10), (float) (currentHeight - 1), (float) (framingRect.right - 10), (float) (currentHeight + 4), this.mLaserPaint); + this.postInvalidateDelayed(20L, framingRect.left - 10, framingRect.top - 10, framingRect.right + 10, framingRect.bottom + 10); } protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) { @@ -178,27 +183,27 @@ public class ViewFinderView extends View implements IViewFinder { int orientation = DisplayUtils.getScreenOrientation(this.getContext()); int width; int height; - if(this.mSquareViewFinder) { - if(orientation != 1) { - height = (int)((float)this.getHeight()); + if (this.mSquareViewFinder) { + if (orientation != 1) { + height = (int) ((float) this.getHeight()); width = height; } else { - width = (int)((float)this.getWidth()); + width = (int) ((float) this.getWidth()); height = width; } - } else if(orientation != 1) { - height = (int)((float)this.getHeight()); - width = (int)(1.4F * (float)height); + } else if (orientation != 1) { + height = (int) ((float) this.getHeight()); + width = (int) (1.4F * (float) height); } else { - width = (int)((float)this.getWidth()); - height = (int)(0.75F * (float)width); + width = (int) ((float) this.getWidth()); + height = (int) (0.75F * (float) width); } - if(width > this.getWidth()) { + if (width > this.getWidth()) { width = this.getWidth() - 50; } - if(height > this.getHeight()) { + if (height > this.getHeight()) { height = this.getHeight() - 50; } -- GitLab From 29d064c161b18aa7af3df1d43b670826c5d06f20 Mon Sep 17 00:00:00 2001 From: Pasha Shkaran Date: Tue, 28 Nov 2017 13:29:27 +0200 Subject: [PATCH 2/2] fix laser alpha --- .../com/nynja/mobile/communicator/utils/ViewFinderView.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/nynja/mobile/communicator/utils/ViewFinderView.java b/app/src/main/java/com/nynja/mobile/communicator/utils/ViewFinderView.java index c9b60a7cf9..47671a7329 100644 --- a/app/src/main/java/com/nynja/mobile/communicator/utils/ViewFinderView.java +++ b/app/src/main/java/com/nynja/mobile/communicator/utils/ViewFinderView.java @@ -19,8 +19,8 @@ import me.dm7.barcodescanner.core.R.integer; public class ViewFinderView extends View implements IViewFinder { private Rect mFramingRect; - private static final int[] SCANNER_ALPHA = new int[]{0, 42, 63, 74, 84, 96, 106, 128, 136, 147, 152, 168, 189, - 210, 231, 255, 242, 231, 222, 210, 200, 189, 177, 168, 147, 128, 106, 84, 63, 42, 21, 0, 21, 42, 63, 84, 106}; + private static final int[] SCANNER_ALPHA = new int[]{42, 63, 74, 84, 96, 106, 128, 136, 147, 152, 168, 189, + 210, 231, 255, 242, 231, 222, 210, 200, 189, 177, 168, 147, 128, 106, 84, 63, 42, 63, 84, 106}; private int scannerAlpha; private final int mDefaultLaserColor; private final int mDefaultMaskColor; -- GitLab