From cecec15762b4054fc53bf2c69bcafbac2f4ce9b1 Mon Sep 17 00:00:00 2001 From: Branden Archer Date: Sat, 21 May 2016 18:26:21 -0400 Subject: [PATCH] Scale barcodes to ImageView's size without filtering It turns out that the library used to create datamatrix barcodes returns the smallest image necessary to contain the barcode. That is, the size passed into the barcode writer. If the ImageView scales the tiny image itself into the full size it will use bi-linear filtering, which results in a blurry barcode. To avoid this, if scaling is needed do so without using filtering. --- .../card_locker/BarcodeImageWriterTask.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/src/main/java/protect/card_locker/BarcodeImageWriterTask.java b/app/src/main/java/protect/card_locker/BarcodeImageWriterTask.java index cd32e0f35..d71bee2ac 100644 --- a/app/src/main/java/protect/card_locker/BarcodeImageWriterTask.java +++ b/app/src/main/java/protect/card_locker/BarcodeImageWriterTask.java @@ -67,6 +67,23 @@ class BarcodeImageWriterTask extends AsyncTask Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, bitMatrixWidth, 0, 0, bitMatrixWidth, bitMatrixHeight); + + // Determine if the image needs to be scaled. + // This is necessary because the datamatrix barcode generator + // ignores the requested size and returns the smallest image necessary + // to represent the barcode. If we let the ImageView scale the image + // it will use bi-linear filtering, which results in a blurry barcode. + // To avoid this, if scaling is needed do so without filtering. + + int heightScale = imageHeight / bitMatrixHeight; + int widthScale = imageWidth / bitMatrixHeight; + int scalingFactor = Math.min(heightScale, widthScale); + + if(scalingFactor > 1) + { + bitmap = Bitmap.createScaledBitmap(bitmap, bitMatrixWidth * scalingFactor, bitMatrixHeight * scalingFactor, false); + } + return bitmap; } catch (WriterException | IllegalArgumentException e)