Skip to content
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

Support set camera frame position #583

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions sample/src/main/res/layout/capture_appcompat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
android:layout_below="@+id/my_awesome_toolbar"
android:layout_alignParentBottom="true"
android:id="@+id/zxing_barcode_scanner"
app:zxing_framing_offset_y="100dp"
app:zxing_use_texture_view="true"/>


Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/layout/capture_small.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
android:layout_marginTop="150dp"
android:layout_marginBottom="150dp"
android:id="@+id/zxing_barcode_scanner"
app:zxing_framing_offset_y_ratio="0.32"
app:zxing_use_texture_view="false"
app:zxing_preview_scaling_strategy="fitXY"/>

Expand Down
2 changes: 2 additions & 0 deletions zxing-android-embedded/res/values/zxing_attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<declare-styleable name="zxing_camera_preview">
<attr name="zxing_framing_rect_width" format="dimension" />
<attr name="zxing_framing_rect_height" format="dimension" />
<attr name="zxing_framing_offset_y" format="dimension" />
<attr name="zxing_framing_offset_y_ratio" format="float" />
<attr name="zxing_use_texture_view" format="boolean" />
<attr name="zxing_preview_scaling_strategy" format="enum">
<enum name="centerCrop" value="1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
Expand Down Expand Up @@ -139,6 +138,12 @@ public interface StateListener {
// must be smaller than 0.5;
private double marginFraction = 0.1d;

// Frame rectangle Y axis offset
private int framingOffsetY;

// Frame rectangle Y axis offset percentage
private float framingOffsetYRatio = 0.5f;

private PreviewScalingStrategy previewScalingStrategy = null;

private boolean torchOn = false;
Expand Down Expand Up @@ -276,6 +281,13 @@ protected void initializeAttributes(AttributeSet attrs) {
this.framingRectSize = new Size(framingRectWidth, framingRectHeight);
}

this.framingOffsetY = styledAttributes.getDimensionPixelSize(R.styleable.zxing_camera_preview_zxing_framing_offset_y, -1);

float framingOffsetYRatio = styledAttributes.getFloat(R.styleable.zxing_camera_preview_zxing_framing_offset_y_ratio, 0.5f);
if (framingOffsetYRatio >= 0.0f && framingOffsetYRatio <= 1.0f) {
this.framingOffsetYRatio = framingOffsetYRatio;
}

this.useTextureView = styledAttributes.getBoolean(R.styleable.zxing_camera_preview_zxing_use_texture_view, true);

// See zxing_attrs.xml for the enum values
Expand Down Expand Up @@ -840,6 +852,14 @@ protected Rect calculateFramingRect(Rect container, Rect surface) {
int horizontalMargin = Math.max(0, (intersection.width() - framingRectSize.width) / 2);
int verticalMargin = Math.max(0, (intersection.height() - framingRectSize.height) / 2);
intersection.inset(horizontalMargin, verticalMargin);

// Y axis offset
if (framingOffsetY == -1) {
intersection.offset(0, (int) ((container.height() - intersection.height()) * framingOffsetYRatio - verticalMargin));
} else {
intersection.offset(0, framingOffsetY - verticalMargin);
}

return intersection;
}
// margin as 10% (default) of the smaller of width, height
Expand All @@ -849,6 +869,14 @@ protected Rect calculateFramingRect(Rect container, Rect surface) {
// We don't want a frame that is taller than wide.
intersection.inset(0, (intersection.height() - intersection.width()) / 2);
}

// Y axis offset
if (framingOffsetY == -1) {
intersection.offset(0, (int) ((container.height() - intersection.height()) * (framingOffsetYRatio - 0.5f)));
} else {
intersection.offset(0, (int) (framingOffsetY - ((container.height() - intersection.height()) * 0.5f)));
}

return intersection;
}

Expand Down