Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Commit

Permalink
Customize View Finder offset
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpoignot committed Mar 23, 2017
1 parent 19f11df commit 0edb6f2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public abstract class BarcodeScannerView extends FrameLayout implements Camera.P
private int mCornerRadius = 0;
private boolean mSquaredFinder = false;
private float mBorderAlpha = 1.0f;
private int mViewFinderOffset = 0;

public BarcodeScannerView(Context context) {
super(context);
Expand Down Expand Up @@ -60,6 +61,7 @@ public BarcodeScannerView(Context context, AttributeSet attributeSet) {
mCornerRadius = a.getDimensionPixelSize(R.styleable.BarcodeScannerView_cornerRadius, mCornerRadius);
mSquaredFinder = a.getBoolean(R.styleable.BarcodeScannerView_squaredFinder, mSquaredFinder);
mBorderAlpha = a.getFloat(R.styleable.BarcodeScannerView_borderAlpha, mBorderAlpha);
mViewFinderOffset = a.getDimensionPixelSize(R.styleable.BarcodeScannerView_finderOffset, mViewFinderOffset);
} finally {
a.recycle();
}
Expand Down Expand Up @@ -113,6 +115,7 @@ protected IViewFinder createViewFinderView(Context context) {
viewFinderView.setBorderCornerRounded(mRoundedCorner);
viewFinderView.setBorderCornerRadius(mCornerRadius);
viewFinderView.setSquareViewFinder(mSquaredFinder);
viewFinderView.setViewFinderOffset(mViewFinderOffset);
return viewFinderView;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface IViewFinder {
void setBorderCornerRounded(boolean isBorderCornersRounded);
void setBorderAlpha(float alpha);
void setBorderCornerRadius(int borderCornersRadius);
void setViewFinderOffset(int offset);
void setSquareViewFinder(boolean isSquareViewFinder);
/**
* Method that executes when Camera preview is starting.
Expand Down
17 changes: 12 additions & 5 deletions core/src/main/java/me/dm7/barcodescanner/core/ViewFinderView.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ViewFinderView extends View implements IViewFinder {
private static final float LANDSCAPE_WIDTH_HEIGHT_RATIO = 1.4f;
private static final int MIN_DIMENSION_DIFF = 50;

private static final float SQUARE_DIMENSION_RATIO = 5f/8;
private static final float DEFAULT_SQUARE_DIMENSION_RATIO = 5f / 8;

private static final int[] SCANNER_ALPHA = {0, 64, 128, 192, 255, 192, 128, 64};
private int scannerAlpha;
Expand All @@ -42,7 +42,8 @@ public class ViewFinderView extends View implements IViewFinder {
protected int mBorderLineLength;
protected boolean mSquareViewFinder;
private boolean mIsLaserEnabled;
private float bordersAlpha;
private float mBordersAlpha;
private int mViewFinderOffset = 0;

public ViewFinderView(Context context) {
super(context);
Expand Down Expand Up @@ -114,6 +115,7 @@ public void setBorderCornerRounded(boolean isBorderCornersRounded) {
@Override
public void setBorderAlpha(float alpha) {
int colorAlpha = (int) (255 * alpha);
mBordersAlpha = alpha;
mBorderPaint.setAlpha(colorAlpha);
}

Expand All @@ -122,6 +124,11 @@ public void setBorderCornerRadius(int borderCornersRadius) {
mBorderPaint.setPathEffect(new CornerPathEffect(borderCornersRadius));
}

@Override
public void setViewFinderOffset(int offset) {
mViewFinderOffset = offset;
}

// TODO: Need a better way to configure this. Revisit when working on 2.0
@Override
public void setSquareViewFinder(boolean set) {
Expand Down Expand Up @@ -220,10 +227,10 @@ public synchronized void updateFramingRect() {

if(mSquareViewFinder) {
if(orientation != Configuration.ORIENTATION_PORTRAIT) {
height = (int) (getHeight() * SQUARE_DIMENSION_RATIO);
height = (int) (getHeight() * DEFAULT_SQUARE_DIMENSION_RATIO);
width = height;
} else {
width = (int) (getWidth() * SQUARE_DIMENSION_RATIO);
width = (int) (getWidth() * DEFAULT_SQUARE_DIMENSION_RATIO);
height = width;
}
} else {
Expand All @@ -246,7 +253,7 @@ public synchronized void updateFramingRect() {

int leftOffset = (viewResolution.x - width) / 2;
int topOffset = (viewResolution.y - height) / 2;
mFramingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
mFramingRect = new Rect(leftOffset + mViewFinderOffset, topOffset + mViewFinderOffset, leftOffset + width - mViewFinderOffset, topOffset + height - mViewFinderOffset);
}
}

1 change: 1 addition & 0 deletions core/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<attr name="cornerRadius" format="dimension" />
<attr name="squaredFinder" format="boolean" />
<attr name="borderAlpha" format="float" />
<attr name="finderOffset" format="dimension" />
</declare-styleable>
</resources>

0 comments on commit 0edb6f2

Please sign in to comment.