Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();

Expand Down Expand Up @@ -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)
{
Copy link
Contributor

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMG_20201113_233240 120

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.

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,
Expand All @@ -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();
Expand All @@ -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();
}
Expand All @@ -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) {
Expand Down