-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Added border around scanning rectangle for scan view #558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
niharika2810
wants to merge
2
commits into
journeyapps:master
Choose a base branch
from
niharika2810:scan_view_with_border
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
* transparency outside it, as well as the laser scanner animation and result points. | ||
* | ||
* @author [email protected] (Daniel Switkin) | ||
* Updated By Niharika | ||
*/ | ||
public class ViewfinderView extends View { | ||
protected static final String TAG = ViewfinderView.class.getSimpleName(); | ||
|
@@ -60,17 +61,18 @@ public class ViewfinderView extends View { | |
protected List<ResultPoint> lastPossibleResultPoints; | ||
protected CameraPreview cameraPreview; | ||
|
||
// Cache the framingRect and previewSize, so that we can still draw it after the preview | ||
// Cache the framingRect and previewFramingRect, so that we can still draw it after the preview | ||
// stopped. | ||
protected Rect framingRect; | ||
protected Size previewSize; | ||
protected Rect previewFramingRect; | ||
|
||
// This constructor is used when the class is built from an XML resource. | ||
public ViewfinderView(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
|
||
// Initialize these once for performance rather than calling them every time in onDraw(). | ||
paint = new Paint(Paint.ANTI_ALIAS_FLAG); | ||
paint.setColor(getResources().getColor(android.R.color.white)); | ||
|
||
Resources resources = getResources(); | ||
|
||
|
@@ -131,84 +133,91 @@ protected void refreshSizes() { | |
return; | ||
} | ||
Rect framingRect = cameraPreview.getFramingRect(); | ||
Size previewSize = cameraPreview.getPreviewSize(); | ||
if (framingRect != null && previewSize != null) { | ||
Rect previewFramingRect = cameraPreview.getPreviewFramingRect(); | ||
if (framingRect != null && previewFramingRect != null) | ||
{ | ||
this.framingRect = framingRect; | ||
this.previewSize = previewSize; | ||
this.previewFramingRect = previewFramingRect; | ||
} | ||
} | ||
|
||
|
||
@SuppressLint("DrawAllocation") | ||
@Override | ||
public void onDraw(Canvas canvas) { | ||
public void onDraw(Canvas canvas) | ||
{ | ||
refreshSizes(); | ||
if (framingRect == null || previewSize == null) { | ||
if (framingRect == null || previewFramingRect == null) | ||
{ | ||
return; | ||
} | ||
|
||
final Rect frame = framingRect; | ||
final Size previewSize = this.previewSize; | ||
Rect frame = framingRect; | ||
Rect previewFrame = previewFramingRect; | ||
|
||
final int width = canvas.getWidth(); | ||
final int height = canvas.getHeight(); | ||
//inside onDraw | ||
int distance = (frame.bottom - frame.top) / 4; | ||
int thickness = 15; | ||
|
||
// Draw the exterior (i.e. outside the framing rect) darkened | ||
paint.setColor(resultBitmap != null ? resultColor : maskColor); | ||
canvas.drawRect(0, 0, width, frame.top, paint); | ||
canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint); | ||
canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint); | ||
canvas.drawRect(0, frame.bottom + 1, width, height, paint); | ||
//top left corner | ||
canvas.drawRect(frame.left - thickness, frame.top - thickness, distance + frame.left, frame.top, paint); | ||
canvas.drawRect(frame.left - thickness, frame.top, frame.left, distance + frame.top, paint); | ||
|
||
if (resultBitmap != null) { | ||
// Draw the opaque result bitmap over the scanning rectangle | ||
paint.setAlpha(CURRENT_POINT_OPACITY); | ||
canvas.drawBitmap(resultBitmap, null, frame, paint); | ||
} else { | ||
// If wanted, draw a red "laser scanner" line through the middle to show decoding is active | ||
if (laserVisibility) { | ||
paint.setColor(laserColor); | ||
//top right corner | ||
canvas.drawRect(frame.right - distance, frame.top - thickness, frame.right + thickness, frame.top, paint); | ||
canvas.drawRect(frame.right, frame.top, frame.right + thickness, distance + frame.top, paint); | ||
|
||
paint.setAlpha(SCANNER_ALPHA[scannerAlpha]); | ||
scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length; | ||
//bottom left corner | ||
canvas.drawRect(frame.left - thickness, frame.bottom, distance + frame.left, frame.bottom + thickness, paint); | ||
canvas.drawRect(frame.left - thickness, frame.bottom - distance, frame.left, frame.bottom, paint); | ||
|
||
final int middle = frame.height() / 2 + frame.top; | ||
canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint); | ||
} | ||
//bottom right corner | ||
canvas.drawRect(frame.right - distance, frame.bottom, frame.right + thickness, frame.bottom + thickness, paint); | ||
canvas.drawRect(frame.right, frame.bottom - distance, frame.right + thickness, frame.bottom, paint); | ||
|
||
final float scaleX = this.getWidth() / (float) previewSize.width; | ||
final float scaleY = this.getHeight() / (float) previewSize.height; | ||
|
||
// draw the last possible result points | ||
if (!lastPossibleResultPoints.isEmpty()) { | ||
paint.setAlpha(CURRENT_POINT_OPACITY / 2); | ||
if (resultBitmap != null) | ||
{ | ||
// Draw the opaque result bitmap over the scanning rectangle | ||
paint.setAlpha(CURRENT_POINT_OPACITY); | ||
canvas.drawBitmap(resultBitmap, null, frame, paint); | ||
} else | ||
{ | ||
|
||
float scaleX = frame.width() / (float) previewFrame.width(); | ||
float scaleY = frame.height() / (float) previewFrame.height(); | ||
|
||
List<ResultPoint> currentPossible = possibleResultPoints; | ||
List<ResultPoint> currentLast = lastPossibleResultPoints; | ||
int frameLeft = frame.left; | ||
int frameTop = frame.top; | ||
if (currentPossible.isEmpty()) | ||
{ | ||
lastPossibleResultPoints = null; | ||
} else | ||
{ | ||
possibleResultPoints = new ArrayList<>(5); | ||
lastPossibleResultPoints = currentPossible; | ||
paint.setAlpha(CURRENT_POINT_OPACITY); | ||
paint.setColor(resultPointColor); | ||
float radius = POINT_SIZE / 2.0f; | ||
for (final ResultPoint point : lastPossibleResultPoints) { | ||
canvas.drawCircle( | ||
(int) (point.getX() * scaleX), | ||
(int) (point.getY() * scaleY), | ||
radius, paint | ||
); | ||
for (ResultPoint point : currentPossible) | ||
{ | ||
canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX), | ||
frameTop + (int) (point.getY() * scaleY), | ||
POINT_SIZE, paint); | ||
} | ||
lastPossibleResultPoints.clear(); | ||
} | ||
|
||
// draw current possible result points | ||
if (!possibleResultPoints.isEmpty()) { | ||
paint.setAlpha(CURRENT_POINT_OPACITY); | ||
if (currentLast != null) | ||
{ | ||
paint.setAlpha(CURRENT_POINT_OPACITY / 2); | ||
paint.setColor(resultPointColor); | ||
for (final ResultPoint point : possibleResultPoints) { | ||
canvas.drawCircle( | ||
(int) (point.getX() * scaleX), | ||
(int) (point.getY() * scaleY), | ||
POINT_SIZE, paint | ||
); | ||
float radius = POINT_SIZE / 2.0f; | ||
for (ResultPoint point : currentLast) | ||
{ | ||
canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX), | ||
frameTop + (int) (point.getY() * scaleY), | ||
radius, paint); | ||
} | ||
|
||
// swap and clear buffers | ||
final List<ResultPoint> temp = possibleResultPoints; | ||
possibleResultPoints = lastPossibleResultPoints; | ||
lastPossibleResultPoints = temp; | ||
possibleResultPoints.clear(); | ||
} | ||
|
||
// Request another update at the animation interval, but only repaint the laser line, | ||
|
@@ -221,10 +230,12 @@ public void onDraw(Canvas canvas) { | |
} | ||
} | ||
|
||
public void drawViewfinder() { | ||
public void drawViewfinder() | ||
{ | ||
Bitmap resultBitmap = this.resultBitmap; | ||
this.resultBitmap = null; | ||
if (resultBitmap != null) { | ||
if (resultBitmap != null) | ||
{ | ||
resultBitmap.recycle(); | ||
} | ||
invalidate(); | ||
|
@@ -235,7 +246,8 @@ public void drawViewfinder() { | |
* | ||
* @param result An image of the result. | ||
*/ | ||
public void drawResultBitmap(Bitmap result) { | ||
public void drawResultBitmap(Bitmap result) | ||
{ | ||
resultBitmap = result; | ||
invalidate(); | ||
} | ||
|
@@ -245,9 +257,16 @@ public void drawResultBitmap(Bitmap result) { | |
* | ||
* @param point a point to draw, relative to the preview frame | ||
*/ | ||
public void addPossibleResultPoint(ResultPoint point) { | ||
if (possibleResultPoints.size() < MAX_RESULT_POINTS) | ||
possibleResultPoints.add(point); | ||
public void addPossibleResultPoint(ResultPoint point) | ||
{ | ||
List<ResultPoint> points = possibleResultPoints; | ||
points.add(point); | ||
int size = points.size(); | ||
if (size > MAX_RESULT_POINTS) | ||
{ | ||
// trim it | ||
points.subList(0, size - MAX_RESULT_POINTS / 2).clear(); | ||
} | ||
} | ||
|
||
public void setMaskColor(int maskColor) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please consider to not change the codestyle.
Unfortunately the maintainer is not active, otherwise I would make a pull request to check for right code style
Btw, thank you for the feature. Btw a screenshot would be cool, to see immediate what is introduced
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really sorry for this late. I missed this. Could you please check if we can have this kind of border as border always helps in background setting.