mirror of
https://github.com/CatimaLoyalty/Android.git
synced 2026-01-05 21:48:01 -05:00
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.
This commit is contained in:
@@ -67,6 +67,23 @@ class BarcodeImageWriterTask extends AsyncTask<Void, Void, Bitmap>
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user