问题
My project with Xamarin and MvvmCross has a Core-Project (to share code).
In this Core-Project, I generate a QR-Image and save it to the local-drive in my Android-App. The following code generates the bytes for a content (always an url):
var options = new QrCodeEncodingOptions
{
Height = 300,
Width = 300,
Margin = 0,
PureBarcode = true
};
var writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = options };
return writer.Write(content);
I save this byte-array with the File-Plugin (MvvmCross) in my app-data-dir. The "Exists"-Method returns true, so the file is there (I hope so).
But when i call following code to get a bitmap to show the saved qr-image in an activity, the Bitmapfactory returns NULL every time:
// First decode with inJustDecodeBounds=true to check dimensions
var options = new BitmapFactory.Options();
options.InJustDecodeBounds = true;
BitmapFactory.DecodeFile(filepath, options);
// Calculate inSampleSize
options.InSampleSize = CalculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.InJustDecodeBounds = false;
return BitmapFactory.DecodeFile(filepath, options);
No exception in no place. I have no idea, whats going wrong. With other pictures (saved and loaded in the same way, but no generated qr-image), all works fine.
I use following zxing in my portable (core) library: zxing.portable (v4.0.30319) Description: port of the java based barcode scanning library for .net (java zxing 13.03.2014 15:19:08)
Any help is welcome =) Thanks
Edit (Solution):
The problem was, that I implemented the generation-code of the QR-Image in the PCL-Library. In this setup, the zxing-lib generated a RAW-byte array (thanks to 'Michael' for his help).
The solution: I just implemented a MvvmCross-Plugin for each platform. Now the zxing-lib (for android) generates a Bitmap and not a RAW-byte array. Final input for the solution: BarcodeWriter.cs
回答1:
The default renderer of zxing.portable gives back a byte array of the raw ARGB data. You should use the method BitmapFactory.DecodeByteArray or you build a supported file format from the byte array before saving it.
来源:https://stackoverflow.com/questions/23953590/xamarin-android-qr-image-zxing-not-working