Merge pull request #24598 from AleksandrPanov:add_android_qr_sample

Add android QR detection sample #24598

Android QR detection sample was added.

![image](https://github.com/opencv/opencv/assets/22337800/0aaf6689-36ee-4201-b609-256d3278641a)
![image](https://github.com/opencv/opencv/assets/22337800/1cf54758-2c96-4108-888b-4796d8825340)
![image](https://github.com/opencv/opencv/assets/22337800/4ecc0933-a2e6-4cc0-abae-7525213c4145)

current interface:
![image](https://github.com/opencv/opencv/assets/22337800/aff5b06d-6a9d-4762-be6e-ce20ff2c2271)


### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Alexander Panov 2023-11-29 12:51:59 +03:00 committed by GitHub
parent 34c406ea3a
commit 7833c63388
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 329 additions and 0 deletions

View File

@ -8,6 +8,7 @@ ocv_warnings_disable(CMAKE_CXX_FLAGS -Wmissing-declarations)
add_subdirectory(15-puzzle)
add_subdirectory(face-detection)
add_subdirectory(qr-detection)
add_subdirectory(image-manipulations)
add_subdirectory(camera-calibration)
add_subdirectory(color-blob-detection)

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.opencv.samples.qrdetection"
android:versionCode="301"
android:versionName="3.01" >
<uses-sdk android:minSdkVersion="8"/>
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".QRdetectionActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>
</manifest>

View File

@ -0,0 +1,6 @@
set(sample example-qr-detection)
add_android_project(${sample} "${CMAKE_CURRENT_SOURCE_DIR}" LIBRARY_DEPS "${OPENCV_ANDROID_LIB_DIR}" SDK_TARGET 11 "${ANDROID_SDK_TARGET}")
if(TARGET ${sample})
add_dependencies(opencv_android_examples ${sample})
endif()

View File

@ -0,0 +1,36 @@
apply plugin: 'com.android.application'
android {
namespace 'org.opencv.samples.qrdetection'
compileSdkVersion @ANDROID_COMPILE_SDK_VERSION@
defaultConfig {
applicationId "org.opencv.samples.qrdetection"
minSdkVersion @ANDROID_MIN_SDK_VERSION@
targetSdkVersion @ANDROID_TARGET_SDK_VERSION@
versionCode 301
versionName "3.01"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
java.srcDirs = @ANDROID_SAMPLE_JAVA_PATH@
aidl.srcDirs = @ANDROID_SAMPLE_JAVA_PATH@
res.srcDirs = @ANDROID_SAMPLE_RES_PATH@
manifest.srcFile '@ANDROID_SAMPLE_MANIFEST_PATH@'
}
}
}
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
if (gradle.opencv_source == "sdk_path") {
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.opencv.samples.qrdetection">
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:exported="true"
android:name=".QRdetectionActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>
</manifest>

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">OpenCV QR Detector</string>
</resources>

View File

@ -0,0 +1,87 @@
package org.opencv.samples.qrdetection;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.core.Point;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.GraphicalCodeDetector;
import org.opencv.objdetect.QRCodeDetector;
import org.opencv.objdetect.QRCodeDetectorAruco;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class QRProcessor {
private GraphicalCodeDetector detector;
private static final String TAG = "QRProcessor";
private Scalar LineColor = new Scalar(255, 0, 0);
private Scalar FontColor = new Scalar(0, 0, 255);
public QRProcessor(boolean useArucoDetector) {
if (useArucoDetector)
detector = new QRCodeDetectorAruco();
else
detector = new QRCodeDetector();
}
private boolean findQRs(Mat inputFrame, List<String> decodedInfo, MatOfPoint points,
boolean tryDecode, boolean multiDetect) {
boolean result = false;
if (multiDetect) {
if (tryDecode)
result = detector.detectAndDecodeMulti(inputFrame, decodedInfo, points);
else
result = detector.detectMulti(inputFrame, points);
}
else {
if(tryDecode) {
String s = detector.detectAndDecode(inputFrame, points);
result = !points.empty();
if (result)
decodedInfo.add(s);
}
else {
result = detector.detect(inputFrame, points);
}
}
return result;
}
private void renderQRs(Mat inputFrame, List<String> decodedInfo, MatOfPoint points) {
for (int i = 0; i < points.rows(); i++) {
for (int j = 0; j < points.cols(); j++) {
Point pt1 = new Point(points.get(i, j));
Point pt2 = new Point(points.get(i, (j + 1) % 4));
Imgproc.line(inputFrame, pt1, pt2, LineColor, 3);
}
if (!decodedInfo.isEmpty()) {
String decode = decodedInfo.get(i);
if (decode.length() > 15) {
decode = decode.substring(0, 12) + "...";
}
int baseline[] = {0};
Size textSize = Imgproc.getTextSize(decode, Imgproc.FONT_HERSHEY_COMPLEX, .95, 3, baseline);
Scalar sum = Core.sumElems(points.row(i));
Point start = new Point(sum.val[0] / 4. - textSize.width / 2., sum.val[1] / 4. - textSize.height / 2.);
Imgproc.putText(inputFrame, decode, start, Imgproc.FONT_HERSHEY_COMPLEX, .95, FontColor, 3);
}
}
}
/* this method to be called from the outside. It processes the frame to find QR codes. */
public synchronized Mat handleFrame(Mat inputFrame, boolean tryDecode, boolean multiDetect) {
List<String> decodedInfo = new ArrayList<String>();
MatOfPoint points = new MatOfPoint();
boolean result = findQRs(inputFrame, decodedInfo, points, tryDecode, multiDetect);
if (result) {
renderQRs(inputFrame, decodedInfo, points);
}
points.release();
return inputFrame;
}
}

View File

@ -0,0 +1,130 @@
package org.opencv.samples.qrdetection;
import org.opencv.android.CameraActivity;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener;
import org.opencv.android.JavaCameraView;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.Toast;
import java.util.Collections;
import java.util.List;
public class QRdetectionActivity extends CameraActivity implements CvCameraViewListener {
private static final String TAG = "QRdetection::Activity";
private CameraBridgeViewBase mOpenCvCameraView;
private QRProcessor mQRDetector;
private MenuItem mItemQRCodeDetectorAruco;
private MenuItem mItemQRCodeDetector;
private MenuItem mItemTryDecode;
private MenuItem mItemMulti;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (OpenCVLoader.initLocal()) {
Log.i(TAG, "OpenCV loaded successfully");
} else {
Log.e(TAG, "OpenCV initialization failed!");
(Toast.makeText(this, "OpenCV initialization failed!", Toast.LENGTH_LONG)).show();
return;
}
Log.d(TAG, "Creating and setting view");
mOpenCvCameraView = new JavaCameraView(this, -1);
setContentView(mOpenCvCameraView);
mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mQRDetector = new QRProcessor(true);
}
@Override
public void onPause()
{
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onResume()
{
super.onResume();
if (mOpenCvCameraView != null) {
mOpenCvCameraView.enableView();
}
}
@Override
protected List<? extends CameraBridgeViewBase> getCameraViewList() {
return Collections.singletonList(mOpenCvCameraView);
}
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i(TAG, "called onCreateOptionsMenu");
mItemQRCodeDetectorAruco = menu.add("Aruco-based QR code detector");
mItemQRCodeDetectorAruco.setCheckable(true);
mItemQRCodeDetectorAruco.setChecked(true);
mItemQRCodeDetector = menu.add("Legacy QR code detector");
mItemQRCodeDetector.setCheckable(true);
mItemQRCodeDetector.setChecked(false);
mItemTryDecode = menu.add("Try to decode QR codes");
mItemTryDecode.setCheckable(true);
mItemTryDecode.setChecked(true);
mItemMulti = menu.add("Use multi detect/decode");
mItemMulti.setCheckable(true);
mItemMulti.setChecked(true);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.i(TAG, "Menu Item selected " + item);
if (item == mItemQRCodeDetector && !mItemQRCodeDetector.isChecked()) {
mQRDetector = new QRProcessor(false);
mItemQRCodeDetector.setChecked(true);
mItemQRCodeDetectorAruco.setChecked(false);
} else if (item == mItemQRCodeDetectorAruco && !mItemQRCodeDetectorAruco.isChecked()) {
mQRDetector = new QRProcessor(true);
mItemQRCodeDetector.setChecked(false);
mItemQRCodeDetectorAruco.setChecked(true);
} else if (item == mItemTryDecode) {
mItemTryDecode.setChecked(!mItemTryDecode.isChecked());
} else if (item == mItemMulti) {
mItemMulti.setChecked(!mItemMulti.isChecked());
}
return true;
}
public void onCameraViewStarted(int width, int height) {
}
public void onCameraViewStopped() {
}
public Mat onCameraFrame(Mat inputFrame) {
return mQRDetector.handleFrame(inputFrame, mItemTryDecode.isChecked(), mItemMulti.isChecked());
}
}