From 3cc9031a8e81a3c77b56c62ff19f8da92494a95c Mon Sep 17 00:00:00 2001 From: garrettmills Date: Wed, 12 May 2021 11:20:58 -0500 Subject: [PATCH] Initial import w/ QR code4 --- .gitignore | 15 ++ .idea/.gitignore | 3 + .idea/.name | 1 + .idea/compiler.xml | 6 + .idea/gradle.xml | 20 ++ .idea/jarRepositories.xml | 30 +++ .idea/misc.xml | 9 + .idea/runConfigurations.xml | 10 + app/.gitignore | 1 + app/build.gradle | 48 +++++ app/proguard-rules.pro | 21 +++ .../hyperlink/ExampleInstrumentedTest.java | 26 +++ app/src/main/AndroidManifest.xml | 24 +++ .../starship/hyperlink/Hyperlink.java | 11 ++ .../starship/hyperlink/MainActivity.java | 126 +++++++++++++ .../scanner/QRCodeImageAnalyzer.java | 64 +++++++ .../hyperlink/scanner/QRCodeListener.java | 6 + .../drawable-v24/ic_launcher_foreground.xml | 30 +++ .../res/drawable/ic_launcher_background.xml | 170 +++++++++++++++++ app/src/main/res/layout/activity_main.xml | 23 +++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3593 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 5339 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2636 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 3388 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4926 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 7472 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7909 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 11873 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10652 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 16570 bytes app/src/main/res/values-night/themes.xml | 16 ++ app/src/main/res/values/colors.xml | 10 + app/src/main/res/values/strings.xml | 5 + app/src/main/res/values/themes.xml | 16 ++ .../starship/hyperlink/ExampleUnitTest.java | 17 ++ build.gradle | 25 +++ gradle.properties | 17 ++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 54329 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 172 ++++++++++++++++++ gradlew.bat | 84 +++++++++ settings.gradle | 2 + 44 files changed, 1024 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/.name create mode 100644 .idea/compiler.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 app/.gitignore create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/androidTest/java/dev/garrettmills/starship/hyperlink/ExampleInstrumentedTest.java create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/dev/garrettmills/starship/hyperlink/Hyperlink.java create mode 100644 app/src/main/java/dev/garrettmills/starship/hyperlink/MainActivity.java create mode 100644 app/src/main/java/dev/garrettmills/starship/hyperlink/scanner/QRCodeImageAnalyzer.java create mode 100644 app/src/main/java/dev/garrettmills/starship/hyperlink/scanner/QRCodeListener.java create mode 100644 app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/values-night/themes.xml create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/themes.xml create mode 100644 app/src/test/java/dev/garrettmills/starship/hyperlink/ExampleUnitTest.java create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..eb088c2 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Starship Hyperlink \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..5cd135a --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,20 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..0380d8d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..860da66 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..f65b8ad --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,48 @@ +plugins { + id 'com.android.application' +// id 'kotlin-android-extensions' +} + +android { + compileSdkVersion 30 + buildToolsVersion "30.0.3" + + defaultConfig { + applicationId "dev.garrettmills.starship.hyperlink" + minSdkVersion 28 + targetSdkVersion 30 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} + +dependencies { + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'com.google.android.material:material:1.2.1' + implementation 'androidx.constraintlayout:constraintlayout:2.0.1' + testImplementation 'junit:junit:4.+' + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + + def camerax_version = "1.0.0-beta07" +// CameraX core library using camera2 implementation + implementation "androidx.camera:camera-camera2:$camerax_version" +// CameraX Lifecycle Library + implementation "androidx.camera:camera-lifecycle:$camerax_version" +// CameraX View class + implementation "androidx.camera:camera-view:1.0.0-alpha14" + implementation 'com.google.zxing:core:3.3.0' +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/dev/garrettmills/starship/hyperlink/ExampleInstrumentedTest.java b/app/src/androidTest/java/dev/garrettmills/starship/hyperlink/ExampleInstrumentedTest.java new file mode 100644 index 0000000..3f04222 --- /dev/null +++ b/app/src/androidTest/java/dev/garrettmills/starship/hyperlink/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package dev.garrettmills.starship.hyperlink; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("dev.garrettmills.starship.hyperlink", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..1c4796b --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/dev/garrettmills/starship/hyperlink/Hyperlink.java b/app/src/main/java/dev/garrettmills/starship/hyperlink/Hyperlink.java new file mode 100644 index 0000000..9ef1fc6 --- /dev/null +++ b/app/src/main/java/dev/garrettmills/starship/hyperlink/Hyperlink.java @@ -0,0 +1,11 @@ +package dev.garrettmills.starship.hyperlink; + +import android.content.SharedPreferences; + +public class Hyperlink { + public static final String SHARED_PREFERENCES_NAME = "hyperlink.main"; + public static final String SERVER_ADDR = "hyperlink.server"; + public static final String SERVER_TOKEN = "hyperlink.token.server"; + + public static SharedPreferences preferences; +} diff --git a/app/src/main/java/dev/garrettmills/starship/hyperlink/MainActivity.java b/app/src/main/java/dev/garrettmills/starship/hyperlink/MainActivity.java new file mode 100644 index 0000000..0d54016 --- /dev/null +++ b/app/src/main/java/dev/garrettmills/starship/hyperlink/MainActivity.java @@ -0,0 +1,126 @@ +package dev.garrettmills.starship.hyperlink; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.camera.core.Camera; +import androidx.camera.core.CameraSelector; +import androidx.camera.core.ImageAnalysis; +import androidx.camera.core.Preview; +import androidx.camera.lifecycle.ProcessCameraProvider; +import androidx.camera.view.PreviewView; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; +import androidx.lifecycle.LifecycleOwner; + +import android.Manifest; +import android.content.pm.PackageManager; +import android.os.Bundle; +import android.util.Log; +import android.util.Size; +import android.view.View; +import android.widget.Button; +import android.widget.Toast; + +import com.google.common.util.concurrent.ListenableFuture; + +import java.util.concurrent.ExecutionException; + +import dev.garrettmills.starship.hyperlink.scanner.QRCodeImageAnalyzer; +import dev.garrettmills.starship.hyperlink.scanner.QRCodeListener; + +public class MainActivity extends AppCompatActivity { + private static final int PERMISSION_REQUEST_CAMERA = 0; + private PreviewView previewView; + private ListenableFuture cameraProviderFuture; + + private Button qrCodeFoundButton; + private String qrCode; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Hyperlink.preferences = getSharedPreferences(Hyperlink.SHARED_PREFERENCES_NAME, MODE_PRIVATE); + + setContentView(R.layout.activity_main); + previewView = findViewById(R.id.activity_main_previewView); + + qrCodeFoundButton = findViewById(R.id.activity_main_qrCodeFoundButton); + qrCodeFoundButton.setVisibility(View.INVISIBLE); + qrCodeFoundButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(getApplicationContext(), qrCode, Toast.LENGTH_LONG).show(); + Log.i(MainActivity.class.getSimpleName(), "QR Code Found: " + qrCode); + } + }); + + cameraProviderFuture = ProcessCameraProvider.getInstance(this); + requestCamera(); + } + + protected void requestCamera() { + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { + startCamera(); + } else { + if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { + ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CAMERA); + } else { + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CAMERA); + } + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + if (requestCode == PERMISSION_REQUEST_CAMERA) { + if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + startCamera(); + } else { + Toast.makeText(this, "Camera Permission Denied", Toast.LENGTH_SHORT).show(); + } + } + } + + private void startCamera() { + cameraProviderFuture.addListener(() -> { + try { + ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); + bindCameraPreview(cameraProvider); + } catch (ExecutionException | InterruptedException e) { + Toast.makeText(this, "Error starting camera: " + e.getMessage(), Toast.LENGTH_LONG).show(); + } + }, ContextCompat.getMainExecutor(this)); + } + + private void bindCameraPreview(@NonNull ProcessCameraProvider cameraProvider) { + previewView.setPreferredImplementationMode(PreviewView.ImplementationMode.SURFACE_VIEW); + + Preview preview = new Preview.Builder().build(); + + CameraSelector cameraSelector = new CameraSelector.Builder() + .requireLensFacing(CameraSelector.LENS_FACING_BACK) + .build(); + + preview.setSurfaceProvider(previewView.createSurfaceProvider()); + + ImageAnalysis imageAnalysis = new ImageAnalysis.Builder() + .setTargetResolution(new Size(1280, 720)) + .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) + .build(); + + imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(this), new QRCodeImageAnalyzer(new QRCodeListener() { + @Override + public void onQRCodeFound(String _qrCode) { + qrCode = _qrCode; + qrCodeFoundButton.setVisibility(View.VISIBLE); + } + + @Override + public void onQRCodeNotFound() { + qrCodeFoundButton.setVisibility(View.INVISIBLE); + } + })); + + Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner) this, cameraSelector, imageAnalysis, preview); + } +} diff --git a/app/src/main/java/dev/garrettmills/starship/hyperlink/scanner/QRCodeImageAnalyzer.java b/app/src/main/java/dev/garrettmills/starship/hyperlink/scanner/QRCodeImageAnalyzer.java new file mode 100644 index 0000000..aafa1b7 --- /dev/null +++ b/app/src/main/java/dev/garrettmills/starship/hyperlink/scanner/QRCodeImageAnalyzer.java @@ -0,0 +1,64 @@ +package dev.garrettmills.starship.hyperlink.scanner; + +import android.util.Log; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.camera.core.ImageAnalysis; +import androidx.camera.core.ImageProxy; +import androidx.core.content.ContextCompat; + +import java.nio.ByteBuffer; + +import com.google.zxing.BinaryBitmap; +import com.google.zxing.ChecksumException; +import com.google.zxing.FormatException; +import com.google.zxing.NotFoundException; +import com.google.zxing.PlanarYUVLuminanceSource; +import com.google.zxing.Result; +import com.google.zxing.common.HybridBinarizer; +import com.google.zxing.multi.qrcode.QRCodeMultiReader; + +import static android.graphics.ImageFormat.YUV_420_888; +import static android.graphics.ImageFormat.YUV_422_888; +import static android.graphics.ImageFormat.YUV_444_888; + +public class QRCodeImageAnalyzer implements ImageAnalysis.Analyzer { + private QRCodeListener listener; + + public QRCodeImageAnalyzer(QRCodeListener listener) { + this.listener = listener; + } + + @Override + public void analyze(@NonNull ImageProxy image) { + if ( image.getFormat() == YUV_420_888 || image.getFormat() == YUV_422_888 || image.getFormat() == YUV_444_888 ) { + ByteBuffer byteBuffer = image.getPlanes()[0].getBuffer(); + byte[] imageData = new byte[byteBuffer.capacity()]; + byteBuffer.get(imageData); + + PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource( + imageData, + image.getWidth(), image.getHeight(), + 0, 0, + image.getWidth(), image.getHeight(), + false + ); + + BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); + + try { + Result result = new QRCodeMultiReader().decode(binaryBitmap); + listener.onQRCodeFound(result.getText()); + Log.i("QRCodeImageAnalyzer", "QRCode found"); + } catch (FormatException | ChecksumException | NotFoundException e) { + listener.onQRCodeNotFound(); + Log.i("QRCodeImageAnalyzer", "QRCode not found"); + } + } else { + Log.i("QRCodeImageAnalyzer", "Incorrect image format"); + } + + image.close(); + } +} diff --git a/app/src/main/java/dev/garrettmills/starship/hyperlink/scanner/QRCodeListener.java b/app/src/main/java/dev/garrettmills/starship/hyperlink/scanner/QRCodeListener.java new file mode 100644 index 0000000..e5eabda --- /dev/null +++ b/app/src/main/java/dev/garrettmills/starship/hyperlink/scanner/QRCodeListener.java @@ -0,0 +1,6 @@ +package dev.garrettmills.starship.hyperlink.scanner; + +public interface QRCodeListener { + void onQRCodeFound(String qrCode); + void onQRCodeNotFound(); +} diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..859ee99 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,23 @@ + + + + + +