diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index 3e3689113..8eaa7f74c 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -5,6 +5,7 @@ import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; import android.content.res.TypedArray; +import android.graphics.Bitmap; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; @@ -69,6 +70,8 @@ public class LoyaltyCardEditActivity extends AppCompatActivity View barcodeImageLayout; View barcodeCaptureLayout; + Button captureButton; + Button importImageButton; Button enterButton; int loyaltyCardId; @@ -503,6 +506,7 @@ public class LoyaltyCardEditActivity extends AppCompatActivity } } + class ColorSelectListener implements View.OnClickListener { final int defaultColor; @@ -702,7 +706,7 @@ public class LoyaltyCardEditActivity extends AppCompatActivity { super.onActivityResult(requestCode, resultCode, intent); - BarcodeValues barcodeValues = Utils.parseSetBarcodeActivityResult(requestCode, resultCode, intent); + BarcodeValues barcodeValues = Utils.parseSetBarcodeActivityResult(requestCode, resultCode, intent, this); barcodeType = barcodeValues.format(); cardId = barcodeValues.content(); diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 1faf8f23f..407c8bc15 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -162,7 +162,7 @@ public class MainActivity extends AppCompatActivity implements GestureDetector.O return; } - BarcodeValues barcodeValues = Utils.parseSetBarcodeActivityResult(requestCode, resultCode, intent); + BarcodeValues barcodeValues = Utils.parseSetBarcodeActivityResult(requestCode, resultCode, intent, this); if(!barcodeValues.isEmpty()) { Intent newIntent = new Intent(getApplicationContext(), LoyaltyCardEditActivity.class); diff --git a/app/src/main/java/protect/card_locker/ScanActivity.java b/app/src/main/java/protect/card_locker/ScanActivity.java index d1e9fd6ba..88de9943e 100644 --- a/app/src/main/java/protect/card_locker/ScanActivity.java +++ b/app/src/main/java/protect/card_locker/ScanActivity.java @@ -132,7 +132,7 @@ public class ScanActivity extends AppCompatActivity { { super.onActivityResult(requestCode, resultCode, intent); - BarcodeValues barcodeValues = Utils.parseSetBarcodeActivityResult(requestCode, resultCode, intent); + BarcodeValues barcodeValues = Utils.parseSetBarcodeActivityResult(requestCode, resultCode, intent, this); if (!barcodeValues.isEmpty()) { Intent manualResult = new Intent(); @@ -154,4 +154,10 @@ public class ScanActivity extends AppCompatActivity { } startActivityForResult(i, Utils.SELECT_BARCODE_REQUEST); } + + public void addFromImage(View view) { + Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); + photoPickerIntent.setType("image/*"); + startActivityForResult(photoPickerIntent, Utils.BARCODE_IMPORT); + } } diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index 3d0ed569f..5a016170c 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -3,15 +3,27 @@ package protect.card_locker; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.graphics.Bitmap; import android.graphics.Color; +import android.provider.MediaStore; import android.util.Log; +import android.widget.Toast; +import java.io.IOException; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import androidx.core.graphics.ColorUtils; +import com.google.zxing.BinaryBitmap; +import com.google.zxing.LuminanceSource; +import com.google.zxing.MultiFormatReader; +import com.google.zxing.NotFoundException; +import com.google.zxing.RGBLuminanceSource; +import com.google.zxing.Result; +import com.google.zxing.common.HybridBinarizer; + public class Utils { private static final String TAG = "Catima"; @@ -19,6 +31,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; static final double LUMINANCE_MIDPOINT = 0.5; @@ -42,22 +55,54 @@ public class Utils { return ColorUtils.calculateLuminance(backgroundColor) > LUMINANCE_MIDPOINT; } - static public BarcodeValues parseSetBarcodeActivityResult(int requestCode, int resultCode, Intent intent) { + static public BarcodeValues parseSetBarcodeActivityResult(int requestCode, int resultCode, Intent intent, Context context) { String contents = null; String format = null; if (resultCode == Activity.RESULT_OK) { - 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); - } + 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); + 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); + 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); + } + } } Log.i(TAG, "Read barcode id: " + contents); diff --git a/app/src/main/res/layout/custom_barcode_scanner.xml b/app/src/main/res/layout/custom_barcode_scanner.xml index 038cb949d..e6ccacfd4 100644 --- a/app/src/main/res/layout/custom_barcode_scanner.xml +++ b/app/src/main/res/layout/custom_barcode_scanner.xml @@ -19,11 +19,25 @@ app:zxing_viewfinder_laser="@color/zxing_custom_viewfinder_laser" app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask"/> -