This project has moved. For the latest updates, please go here.

Code 128 decode, Object reference not set to an instance of an object, Android

Jun 21, 2013 at 5:51 AM
This is my code for Win Form Application, and works fine:
BM = new Bitmap( "_Citacka_BarCode_Image_2.jpg" );
            
ZXing.OneD.Code128Reader C128 = new ZXing.OneD.Code128Reader();

Dictionary<ZXing.DecodeHintType,object> Hints = new Dictionary<ZXing.DecodeHintType, object>();
Hints.Add( ZXing.DecodeHintType.TRY_HARDER, true );
                
ZXing.BitmapLuminanceSource BLS = new ZXing.BitmapLuminanceSource( BM );
ZXing.BinaryBitmap BB = new ZXing.BinaryBitmap( new ZXing.Common.HybridBinarizer( BLS ) );
ZXing.Result R = C128.decode( BB, Hints );
This is my code for xamarin android, but with exception:
Android.Graphics.Bitmap BM = Android.Graphics.BitmapFactory.DecodeByteArray( Data, 0, Data.Length );

ZXing.OneD.Code128Reader C128 = new ZXing.OneD.Code128Reader();
                        
Dictionary<ZXing.DecodeHintType, object> Hints = new Dictionary<ZXing.DecodeHintType, object>();
Hints.Add( ZXing.DecodeHintType.TRY_HARDER, true );

ZXing.BitmapLuminanceSource BLS = new ZXing.BitmapLuminanceSource( BM );
ZXing.BinaryBitmap BB = new ZXing.BinaryBitmap( new ZXing.Common.HybridBinarizer( BLS ) );
ZXing.Result ODR = C128.decode( BB, Hints );
Exception si on line:
"ZXing.BitmapLuminanceSource BLS = new ZXing.BitmapLuminanceSource( BM );"

BM is not null, contains photo from camera.

Exception:
Object reference not set to an instance of an object

StackTrace
at (wrapper delegate-invoke) <Module>:invoke_void__this___intptr_intptr_int_int_int[] (intptr,intptr,int,int,int[])
at Android.Runtime.JNIEnv.CopyArray (System.Int32[] src, IntPtr dest) [0x00000] in <filename unknown>:0
at Android.Runtime.JNIEnv.NewArray (System.Int32[] array) [0x00000] in <filename unknown>:0
at Android.Graphics.Bitmap.GetPixels (System.Int32[] pixels, Int32 offset, Int32 stride, Int32 x, Int32 y, Int32 width, Int32 height) [0x00000] in <filename unknown>:0
at ZXing.BitmapLuminanceSource..ctor (Android.Graphics.Bitmap bitmap) [0x00000] in <filename unknown>:0
at Citacka.aTabScanner+eJPEG_PictureCallback_Def.OnPictureTaken (System.Byte[] Data, Android.Hardware.Camera eCamera) [0x000a2] in c:\_VS2012_\_Android_\_Projekty_\\Citacka\Citacka\aTabScanner.cs:310

I don't know, where i make mistake.
Coordinator
Jun 21, 2013 at 8:09 AM
It seems to be something Android-specific. The exception is not thrown by the ZXing.Net library:
at (wrapper delegate-invoke) <Module>:invoke_void__this___intptr_intptr_int_int_int[] (intptr,intptr,int,int,int[])
at Android.Runtime.JNIEnv.CopyArray (System.Int32[] src, IntPtr dest) [0x00000] in <filename unknown>:0 
...
Perhaps there is something wrong with the following line:
Android.Graphics.Bitmap BM = Android.Graphics.BitmapFactory.DecodeByteArray( Data, 0, Data.Length );
Are you sure that the bitmap BM is fully and correct initialized?
What are the property values like height and width of the bitmap?
Jun 21, 2013 at 9:43 AM
You have true, is something wrong with Bitmap.
First i try this code:
Android.Graphics.Bitmap BM = Android.Graphics.Bitmap.CreateBitmap( 640, 480, Android.Graphics.Bitmap.Config.Argb8888 );                     
It's is empty bitmap. It works fine, no exception.

Then i try this, and it's works:
Android.Graphics.BitmapFactory.Options eOptions = new Android.Graphics.BitmapFactory.Options();
eOptions.InPreferredConfig = Android.Graphics.Bitmap.Config.Argb8888;                       
Android.Graphics.Bitmap BM = Android.Graphics.BitmapFactory.DecodeByteArray( Data, 0, Data.Length, eOptions );
I specify config to ARGB8888, and no exception is raise, and barcode is decoded ok.

Thanks you for your hint.
Coordinator
Jun 21, 2013 at 6:56 PM
Btw. a simpler version of your code (which do the same) is the following:
Android.Graphics.Bitmap BM = ...;
var reader = new BarcodeReader
   {
      TryHarder = true,
      PossibleFormats = new List<BarcodeFormat> {BarcodeFormat.CODE_128}
   };
var ODR = reader.Decode(BM);