From d34554ac070f088c8410f51c0893533c37ae0602 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Sun, 28 Mar 2021 15:20:54 +0200 Subject: [PATCH] Split up code more --- .../protect/card_locker/ScanActivity.java | 2 +- .../main/java/protect/card_locker/Utils.java | 116 ++++++++++-------- app/src/main/res/values/strings.xml | 2 +- 3 files changed, 68 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/protect/card_locker/ScanActivity.java b/app/src/main/java/protect/card_locker/ScanActivity.java index 88de9943e..fd3ebbeba 100644 --- a/app/src/main/java/protect/card_locker/ScanActivity.java +++ b/app/src/main/java/protect/card_locker/ScanActivity.java @@ -158,6 +158,6 @@ public class ScanActivity extends AppCompatActivity { public void addFromImage(View view) { Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); - startActivityForResult(photoPickerIntent, Utils.BARCODE_IMPORT); + startActivityForResult(photoPickerIntent, Utils.BARCODE_IMPORT_FROM_IMAGE_FILE); } } diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index e67dd61e6..1c69d55ae 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -19,6 +19,7 @@ import java.util.GregorianCalendar; import androidx.core.graphics.ColorUtils; +import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; import com.google.zxing.LuminanceSource; import com.google.zxing.MultiFormatReader; @@ -34,7 +35,7 @@ public class Utils { public static final int MAIN_REQUEST = 1; public static final int SELECT_BARCODE_REQUEST = 2; public static final int BARCODE_SCAN = 3; - public static final int BARCODE_IMPORT = 4; + public static final int BARCODE_IMPORT_FROM_IMAGE_FILE = 4; static final double LUMINANCE_MIDPOINT = 0.5; @@ -59,59 +60,74 @@ public class Utils { } static public BarcodeValues parseSetBarcodeActivityResult(int requestCode, int resultCode, Intent intent, Context context) { - String contents = null; - String format = null; + String contents; + String format; - if (resultCode == Activity.RESULT_OK) - { - if (requestCode != Utils.BARCODE_IMPORT) { - if (requestCode == Utils.BARCODE_SCAN) { - Log.i(TAG, "Received barcode information from camera"); - } else if (requestCode == Utils.SELECT_BARCODE_REQUEST) { - Log.i(TAG, "Received barcode information from typing it"); - } else { - return new BarcodeValues(null, null); - } - - contents = intent.getStringExtra(BarcodeSelectorActivity.BARCODE_CONTENTS); - format = intent.getStringExtra(BarcodeSelectorActivity.BARCODE_FORMAT); - } else { - Log.i(TAG, "Received barcode image"); - - Bitmap bitmap = null; - try { - bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), intent.getData()); - } catch (IOException e) { - Log.e(TAG, "Error getting the image data"); - e.printStackTrace(); - Toast.makeText(context, R.string.errorReadingImage, Toast.LENGTH_LONG).show(); - return new BarcodeValues(null, null); - } - - // In order to decode it, the Bitmap must first be converted into a pixel array... - int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()]; - bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); - - // ...and then turned into a binary bitmap from its luminance - LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray); - BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); - - try { - Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap); - - contents = qrCodeResult.getText(); - format = qrCodeResult.getBarcodeFormat().name(); - } catch (NotFoundException e) { - Log.i(TAG, "No barcode was found"); - Toast.makeText(context, R.string.noBarcodeFound, Toast.LENGTH_LONG).show(); - } - } + if (resultCode != Activity.RESULT_OK) { + return new BarcodeValues(null, null); } - Log.i(TAG, "Read barcode id: " + contents); - Log.i(TAG, "Read format: " + format); + if (requestCode == Utils.BARCODE_IMPORT_FROM_IMAGE_FILE) { + Log.i(TAG, "Received image file with possible barcode"); - return new BarcodeValues(format, contents); + Bitmap bitmap; + try { + bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), intent.getData()); + } catch (IOException e) { + Log.e(TAG, "Error getting data from image file"); + e.printStackTrace(); + Toast.makeText(context, R.string.errorReadingImage, Toast.LENGTH_LONG).show(); + return new BarcodeValues(null, null); + } + + BarcodeValues barcodeFromBitmap = getBarcodeFromBitmap(bitmap); + + if (barcodeFromBitmap.isEmpty()) { + Log.i(TAG, "No barcode found in image file"); + Toast.makeText(context, R.string.noBarcodeFound, Toast.LENGTH_LONG).show(); + } + + Log.i(TAG, "Read barcode id: " + barcodeFromBitmap.content()); + Log.i(TAG, "Read format: " + barcodeFromBitmap.format()); + + return barcodeFromBitmap; + } + + if (requestCode == Utils.BARCODE_SCAN || requestCode == Utils.SELECT_BARCODE_REQUEST) { + if (requestCode == Utils.BARCODE_SCAN) { + Log.i(TAG, "Received barcode information from camera"); + } else if (requestCode == Utils.SELECT_BARCODE_REQUEST) { + Log.i(TAG, "Received barcode information from typing it"); + } + + contents = intent.getStringExtra(BarcodeSelectorActivity.BARCODE_CONTENTS); + format = intent.getStringExtra(BarcodeSelectorActivity.BARCODE_FORMAT); + + Log.i(TAG, "Read barcode id: " + contents); + Log.i(TAG, "Read format: " + format); + + return new BarcodeValues(format, contents); + } + + throw new UnsupportedOperationException("Unknown request code for parseSetBarcodeActivityResult"); + } + + static public BarcodeValues getBarcodeFromBitmap(Bitmap bitmap) { + // In order to decode it, the Bitmap must first be converted into a pixel array... + int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()]; + bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); + + // ...and then turned into a binary bitmap from its luminance + LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray); + BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); + + try { + Result barcodeResult = new MultiFormatReader().decode(binaryBitmap); + + return new BarcodeValues(barcodeResult.getBarcodeFormat().name(), barcodeResult.getText()); + } catch (NotFoundException e) { + return new BarcodeValues(null, null); + } } static public Boolean hasExpired(Date expiryDate) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 67144e59c..84a57d55f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -135,7 +135,7 @@ Leave without saving Are you sure you want to leave this screen? Changed made will not be saved. Manually enter card ID - Import from image + Select image from gallery Groups: %s Expires: %s Expired: %s