diff --git a/app/src/main/java/net/sourceforge/opencamera/preview/OverlayQRCodeView.java b/app/src/main/java/net/sourceforge/opencamera/preview/OverlayQRCodeView.java new file mode 100644 index 0000000000000000000000000000000000000000..92d5f397ffa91932cbf677cae90757e2b6023cfb --- /dev/null +++ b/app/src/main/java/net/sourceforge/opencamera/preview/OverlayQRCodeView.java @@ -0,0 +1,64 @@ +/* + * Copyright MURENA SAS 2024 + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.sourceforge.opencamera.preview; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.drawable.Drawable; +import android.util.DisplayMetrics; +import android.view.View; + +import androidx.core.content.ContextCompat; + +import foundation.e.camera.R; + +public class OverlayQRCodeView extends View { + private Drawable qrcode; + // Do not draw the additional resource if something goes wrong + private boolean isValid = true; + + public OverlayQRCodeView(Context context) { + super(context); + init(); + } + + private void init() { + qrcode = ContextCompat.getDrawable(this.getContext(), R.drawable.scan_area); + DisplayMetrics displayMetrics =this.getContext().getResources().getDisplayMetrics(); + final int screenWidth = displayMetrics.widthPixels; + final int screenHeight = displayMetrics.heightPixels; + + final int left = (screenWidth - qrcode.getIntrinsicWidth()) / 2; + final int top = (screenHeight - qrcode.getIntrinsicHeight()) / 2; + final int right = left + qrcode.getIntrinsicWidth(); + final int bottom = top + qrcode.getIntrinsicHeight(); + + if (left <= 0 || top <= 0 || right <= 0 || bottom <= 0) { + isValid = false; + } else { + qrcode.setBounds(left, top, right, bottom); + } + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + if (isValid) { + qrcode.draw(canvas); + } + } +} diff --git a/app/src/main/java/net/sourceforge/opencamera/preview/Preview.java b/app/src/main/java/net/sourceforge/opencamera/preview/Preview.java index 8ce360f26d669a660224ea3955b09ac5f12b0736..3ffe7fee8de86bba4c3791944416b471242ae27b 100644 --- a/app/src/main/java/net/sourceforge/opencamera/preview/Preview.java +++ b/app/src/main/java/net/sourceforge/opencamera/preview/Preview.java @@ -14,6 +14,7 @@ import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; +import android.graphics.Paint; import android.graphics.Point; import android.graphics.Rect; import android.graphics.RectF; @@ -226,6 +227,8 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu * Important to call close() when the video recording is finished, to free up any resources * (e.g., supplied ParcelFileDescriptor). */ + + private OverlayQRCodeView overlayQRCodeView; private static class VideoFileInfo { private final ApplicationInterface.VideoMethod video_method; private final Uri video_uri; // for VideoMethod.SAF, VideoMethod.URI or VideoMethod.MEDIASTORE @@ -502,6 +505,8 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu if (canvasView != null) { parent.addView(canvasView); } + + overlayQRCodeView = new OverlayQRCodeView(getContext()); } /*private void previewToCamera(float [] coords) { @@ -4771,6 +4776,9 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu stopVideo(false); } this.functionalMode = FunctionalMode.QRCODE; + final Activity activity = (Activity) Preview.this.getContext(); + final FrameLayout rootLayout = activity.findViewById(android.R.id.content); + rootLayout.addView(overlayQRCodeView); int qrcodeCamId = ((MainActivity)getContext()).getBetterQRCodeCameraID(); if (qrcodeCamId >= 0) { applicationInterface.setCameraIdPref(qrcodeCamId); @@ -4778,6 +4786,11 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu } } else if (this.is_qrcode()) { this.functionalMode = FunctionalMode.PHOTO; + final Activity activity = (Activity) Preview.this.getContext(); + final FrameLayout rootLayout = activity.findViewById(android.R.id.content); + if (overlayQRCodeView.getParent() != null) { + rootLayout.removeView(overlayQRCodeView); + } } else if (this.is_photo()) { if (this.isOnTimer()) { cancelTimer(); @@ -4791,6 +4804,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu ((MainActivity)getContext()).setDecorFitsSystemWindows(true); this.functionalMode = FunctionalMode.VIDEO; } + // nothing to do for overlayQRCodeView in this mode } if (is_qrcode() != old_is_qrcode) { diff --git a/app/src/main/res/drawable/ic_switch_qrcode.xml b/app/src/main/res/drawable/ic_switch_qrcode.xml index 8b31106f9e36fda2f032cb5ec488c28b2c19a1bb..6fa827cf0454ee51ec98e86708e482d60ddb7c12 100644 --- a/app/src/main/res/drawable/ic_switch_qrcode.xml +++ b/app/src/main/res/drawable/ic_switch_qrcode.xml @@ -1,17 +1,47 @@ + android:width="36dp" + android:height="36dp" + android:viewportWidth="36" + android:viewportHeight="36"> + android:pathData="M0,0h36v36h-36z"/> + + + + + + + + + + diff --git a/app/src/main/res/drawable/scan_area.xml b/app/src/main/res/drawable/scan_area.xml new file mode 100644 index 0000000000000000000000000000000000000000..2486da0806f19991907d6f0422383cef29ae826c --- /dev/null +++ b/app/src/main/res/drawable/scan_area.xml @@ -0,0 +1,14 @@ + + + + + + diff --git a/app/src/main/res/layout/qr_bottom_sheet_dialog.xml b/app/src/main/res/layout/qr_bottom_sheet_dialog.xml index c0b49148969237cffa306a40b5fb8ac510e0a1b7..318616b31297fce777876edb2886b763768c582e 100644 --- a/app/src/main/res/layout/qr_bottom_sheet_dialog.xml +++ b/app/src/main/res/layout/qr_bottom_sheet_dialog.xml @@ -27,7 +27,7 @@ + tools:text="URL" + android:breakStrategy="high_quality" />