├── .gitattributes ├── .gitignore ├── .idea ├── .name ├── codeStyles │ └── Project.xml ├── gradle.xml ├── jarRepositories.xml ├── misc.xml └── runConfigurations.xml ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── learntodroid │ │ └── androidqrcodescanner │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── learntodroid │ │ │ └── androidqrcodescanner │ │ │ ├── MainActivity.java │ │ │ ├── QRCodeFoundListener.java │ │ │ └── QRCodeImageAnalyzer.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── learntodroid │ └── androidqrcodescanner │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | Android QR Code Scanner -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | xmlns:android 14 | 15 | ^$ 16 | 17 | 18 | 19 |
20 |
21 | 22 | 23 | 24 | xmlns:.* 25 | 26 | ^$ 27 | 28 | 29 | BY_NAME 30 | 31 |
32 |
33 | 34 | 35 | 36 | .*:id 37 | 38 | http://schemas.android.com/apk/res/android 39 | 40 | 41 | 42 |
43 |
44 | 45 | 46 | 47 | .*:name 48 | 49 | http://schemas.android.com/apk/res/android 50 | 51 | 52 | 53 |
54 |
55 | 56 | 57 | 58 | name 59 | 60 | ^$ 61 | 62 | 63 | 64 |
65 |
66 | 67 | 68 | 69 | style 70 | 71 | ^$ 72 | 73 | 74 | 75 |
76 |
77 | 78 | 79 | 80 | .* 81 | 82 | ^$ 83 | 84 | 85 | BY_NAME 86 | 87 |
88 |
89 | 90 | 91 | 92 | .* 93 | 94 | http://schemas.android.com/apk/res/android 95 | 96 | 97 | ANDROID_ATTRIBUTE_ORDER 98 | 99 |
100 |
101 | 102 | 103 | 104 | .* 105 | 106 | .* 107 | 108 | 109 | BY_NAME 110 | 111 |
112 |
113 |
114 |
115 |
116 |
-------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 20 | 21 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 29 5 | 6 | defaultConfig { 7 | applicationId "com.learntodroid.androidqrcodescanner" 8 | minSdkVersion 21 9 | targetSdkVersion 29 10 | versionCode 1 11 | versionName "1.0" 12 | 13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 14 | } 15 | 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | 23 | compileOptions { 24 | sourceCompatibility JavaVersion.VERSION_1_8 25 | targetCompatibility JavaVersion.VERSION_1_8 26 | } 27 | } 28 | 29 | dependencies { 30 | implementation fileTree(dir: "libs", include: ["*.jar"]) 31 | implementation 'androidx.appcompat:appcompat:1.1.0' 32 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 33 | testImplementation 'junit:junit:4.12' 34 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 35 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 36 | 37 | def camerax_version = "1.0.0-beta07" 38 | implementation "androidx.camera:camera-camera2:$camerax_version" 39 | implementation "androidx.camera:camera-lifecycle:$camerax_version" 40 | implementation "androidx.camera:camera-view:1.0.0-alpha14" 41 | 42 | // qrcode reader 43 | implementation 'com.google.zxing:core:3.3.0' 44 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/com/learntodroid/androidqrcodescanner/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.learntodroid.androidqrcodescanner; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | assertEquals("com.learntodroid.androidqrcodescanner", appContext.getPackageName()); 25 | } 26 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/learntodroid/androidqrcodescanner/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.learntodroid.androidqrcodescanner; 2 | 3 | import androidx.annotation.NonNull; 4 | import androidx.appcompat.app.AppCompatActivity; 5 | import androidx.camera.core.Camera; 6 | import androidx.camera.core.CameraSelector; 7 | import androidx.camera.core.ImageAnalysis; 8 | import androidx.camera.core.Preview; 9 | import androidx.camera.lifecycle.ProcessCameraProvider; 10 | import androidx.camera.view.PreviewView; 11 | import androidx.core.app.ActivityCompat; 12 | import androidx.core.content.ContextCompat; 13 | import androidx.lifecycle.LifecycleOwner; 14 | 15 | import android.Manifest; 16 | import android.content.pm.PackageManager; 17 | import android.os.Bundle; 18 | import android.util.Log; 19 | import android.util.Size; 20 | import android.view.View; 21 | import android.widget.Button; 22 | import android.widget.Toast; 23 | 24 | import com.google.common.util.concurrent.ListenableFuture; 25 | 26 | import java.util.concurrent.ExecutionException; 27 | 28 | public class MainActivity extends AppCompatActivity { 29 | private static final int PERMISSION_REQUEST_CAMERA = 0; 30 | 31 | private PreviewView previewView; 32 | private ListenableFuture cameraProviderFuture; 33 | 34 | private Button qrCodeFoundButton; 35 | private String qrCode; 36 | 37 | @Override 38 | protected void onCreate(Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | setContentView(R.layout.activity_main); 41 | 42 | previewView = findViewById(R.id.activity_main_previewView); 43 | 44 | qrCodeFoundButton = findViewById(R.id.activity_main_qrCodeFoundButton); 45 | qrCodeFoundButton.setVisibility(View.INVISIBLE); 46 | qrCodeFoundButton.setOnClickListener(new View.OnClickListener() { 47 | @Override 48 | public void onClick(View v) { 49 | Toast.makeText(getApplicationContext(), qrCode, Toast.LENGTH_SHORT).show(); 50 | Log.i(MainActivity.class.getSimpleName(), "QR Code Found: " + qrCode); 51 | } 52 | }); 53 | 54 | cameraProviderFuture = ProcessCameraProvider.getInstance(this); 55 | requestCamera(); 56 | } 57 | 58 | private void requestCamera() { 59 | if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { 60 | startCamera(); 61 | } else { 62 | if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { 63 | ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CAMERA); 64 | } else { 65 | ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CAMERA); 66 | } 67 | } 68 | } 69 | 70 | @Override 71 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 72 | if (requestCode == PERMISSION_REQUEST_CAMERA) { 73 | if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 74 | startCamera(); 75 | } else { 76 | Toast.makeText(this, "Camera Permission Denied", Toast.LENGTH_SHORT).show(); 77 | } 78 | } 79 | } 80 | 81 | private void startCamera() { 82 | cameraProviderFuture.addListener(() -> { 83 | try { 84 | ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); 85 | bindCameraPreview(cameraProvider); 86 | } catch (ExecutionException | InterruptedException e) { 87 | Toast.makeText(this, "Error starting camera " + e.getMessage(), Toast.LENGTH_SHORT).show(); 88 | } 89 | }, ContextCompat.getMainExecutor(this)); 90 | } 91 | 92 | private void bindCameraPreview(@NonNull ProcessCameraProvider cameraProvider) { 93 | previewView.setPreferredImplementationMode(PreviewView.ImplementationMode.SURFACE_VIEW); 94 | 95 | Preview preview = new Preview.Builder() 96 | .build(); 97 | 98 | CameraSelector cameraSelector = new CameraSelector.Builder() 99 | .requireLensFacing(CameraSelector.LENS_FACING_BACK) 100 | .build(); 101 | 102 | preview.setSurfaceProvider(previewView.createSurfaceProvider()); 103 | 104 | ImageAnalysis imageAnalysis = 105 | new ImageAnalysis.Builder() 106 | .setTargetResolution(new Size(1280, 720)) 107 | .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) 108 | .build(); 109 | 110 | imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(this), new QRCodeImageAnalyzer(new QRCodeFoundListener() { 111 | @Override 112 | public void onQRCodeFound(String _qrCode) { 113 | qrCode = _qrCode; 114 | qrCodeFoundButton.setVisibility(View.VISIBLE); 115 | } 116 | 117 | @Override 118 | public void qrCodeNotFound() { 119 | qrCodeFoundButton.setVisibility(View.INVISIBLE); 120 | } 121 | })); 122 | 123 | Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector, imageAnalysis, preview); 124 | } 125 | } -------------------------------------------------------------------------------- /app/src/main/java/com/learntodroid/androidqrcodescanner/QRCodeFoundListener.java: -------------------------------------------------------------------------------- 1 | package com.learntodroid.androidqrcodescanner; 2 | 3 | public interface QRCodeFoundListener { 4 | void onQRCodeFound(String qrCode); 5 | void qrCodeNotFound(); 6 | } 7 | -------------------------------------------------------------------------------- /app/src/main/java/com/learntodroid/androidqrcodescanner/QRCodeImageAnalyzer.java: -------------------------------------------------------------------------------- 1 | package com.learntodroid.androidqrcodescanner; 2 | 3 | import androidx.annotation.NonNull; 4 | import androidx.camera.core.ImageAnalysis; 5 | import androidx.camera.core.ImageProxy; 6 | 7 | import com.google.zxing.BinaryBitmap; 8 | import com.google.zxing.ChecksumException; 9 | import com.google.zxing.FormatException; 10 | import com.google.zxing.NotFoundException; 11 | import com.google.zxing.PlanarYUVLuminanceSource; 12 | import com.google.zxing.Result; 13 | import com.google.zxing.common.HybridBinarizer; 14 | import com.google.zxing.multi.qrcode.QRCodeMultiReader; 15 | 16 | import java.nio.ByteBuffer; 17 | 18 | import static android.graphics.ImageFormat.YUV_420_888; 19 | import static android.graphics.ImageFormat.YUV_422_888; 20 | import static android.graphics.ImageFormat.YUV_444_888; 21 | 22 | public class QRCodeImageAnalyzer implements ImageAnalysis.Analyzer { 23 | private QRCodeFoundListener listener; 24 | 25 | public QRCodeImageAnalyzer(QRCodeFoundListener listener) { 26 | this.listener = listener; 27 | } 28 | 29 | @Override 30 | public void analyze(@NonNull ImageProxy image) { 31 | if (image.getFormat() == YUV_420_888 || image.getFormat() == YUV_422_888 || image.getFormat() == YUV_444_888) { 32 | ByteBuffer byteBuffer = image.getPlanes()[0].getBuffer(); 33 | byte[] imageData = new byte[byteBuffer.capacity()]; 34 | byteBuffer.get(imageData); 35 | 36 | PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource( 37 | imageData, 38 | image.getWidth(), image.getHeight(), 39 | 0, 0, 40 | image.getWidth(), image.getHeight(), 41 | false 42 | ); 43 | 44 | BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); 45 | 46 | try { 47 | Result result = new QRCodeMultiReader().decode(binaryBitmap); 48 | listener.onQRCodeFound(result.getText()); 49 | } catch (FormatException | ChecksumException | NotFoundException e) { 50 | listener.qrCodeNotFound(); 51 | } 52 | } 53 | 54 | image.close(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 |