C# - cannot cast from reader to result?

417 views
Skip to first unread message

ent...@gmail.com

unread,
Apr 17, 2009, 10:47:40 PM4/17/09
to zxing
Although I may have made a stupid assumption as to how this all works,
I cannot work my way around the following error:

com.google.zxing.ReaderException: Unable to cast object of type
'com.google.zxing.oned.MultiFormatOneDReader' to type
'com.google.zxing.Reader'.
at com.google.zxing.MultiFormatReader.decode(MonochromeBitmapSource
image)
at sshopper_poc.Form1.decodeBarcode() in C:\Users\entens\Documents
\Visual Studio 2008\Projects\sshopper_poc\sshopper_poc\Form1.cs:line
59

the code being executed is as follows:

try
{
if (picBarcode.Image != null)
{
// Get the image from the picture box and store it
Bitmap imgSource = new Bitmap(picBarcode.Image);

// Create a buffered version of the barcode image the can be
read.
BufferedImageMonochromeBitmapSource imgBuffered =
new BufferedImageMonochromeBitmapSource(imgSource,
false);

// Create the zxing objects to read the barcode
Reader myReader;
Result myResult;
myReader = new MultiFormatReader();
myResult = myReader.decode(imgBuffered); // line 59

// return the result if not empty
if (myResult.getText() != null)
{
MessageBox.Show(myResult.getText());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}


I am looking at 'barcorama' right now seeking an answer to my
conundrum, however if someone here could answer my question I'd be
grateful.

Thanks in advance,
Entens

srowen

unread,
Apr 18, 2009, 3:28:50 AM4/18/09
to zxing
This looks like ported C# code, not something from the project?
Nevertheless, we could try to help, but, the error here says that
MultiformatOneDReader does not implement Reader, whereas it definitely
does in the Java code. It sounds like an issue specific to your port
then.

srowen

unread,
Apr 18, 2009, 3:34:07 AM4/18/09
to zxing
Er, I should say I don't know for sure, are you using your own C# code
or that in csharp/ in this project? While I don't know much about that
code, I see it does declare:

public sealed class MultiFormatReader : Reader

... so I would not see how you can't cast to Reader using this code.

ent...@gmail.com

unread,
Apr 18, 2009, 1:51:25 PM4/18/09
to zxing
I compiled the c# port in the 1.3 release into a dll and added it as a
reference in my project. I didn't see anything different than what was
in the svn so I assume that the 1.3 release is current. I'm also
mystified. I'm assuming that there is a problem with inheritance in
the oned class to reader class.

srowen

unread,
Apr 18, 2009, 3:31:06 PM4/18/09
to zxing
I know less than you do about C#, so probably can't be of help. It
sure seems like all the *Reader classes inherit from Reader, so I
don't know what is going on.

ent...@gmail.com

unread,
Apr 18, 2009, 6:31:08 PM4/18/09
to zxing
Upon further investigation, it seems that this error is mitigated by
directly referencing the barcode type. For example instead of using
the MultiFormatReader function, I substituted '.oned.EAN13Reader' and
it can now successfully read EAN13 barcodes.

ent...@gmail.com

unread,
Apr 18, 2009, 6:47:29 PM4/18/09
to zxing
It seems that by using EAN13 I can properly scan UPC-A, UPC-E, EAN-8,
and EAN-13 codes. No need to dig further unless you want QRcodes.

scottn...@gmail.com

unread,
Apr 24, 2009, 5:35:59 PM4/24/09
to zxing
Do you happen to have some examples of images that you were able to
make work? I tried uploading a bar code image EAN-13 I think I keep
getting getting the exception in the determinNumSys method because it
never finds what i'm looking for:

private static void determineNumSysAndCheckDigit
(StringBuilder resultString, int lgPatternFound)
{

for (int numSys = 0; numSys <= 1; numSys++) {
for (int d = 0; d < 10; d++) {
if (lgPatternFound == NUMSYS_AND_CHECK_DIGIT_PATTERNS
[numSys][d]) {
resultString.Insert(0, (char) ('0' + numSys));
resultString.Append((char) ('0' + d));
return;
}
}
}
throw new ReaderException();
}

Thanks

scottn...@gmail.com

unread,
Apr 27, 2009, 11:28:25 AM4/27/09
to zxing
Sorry - I should be more clear. I'm trying to get food-related UPC
items working and none all of my barcode images are raising exceptions
in the determineNumSysAndCheckDigit method. Thanks.

srowen

unread,
Apr 27, 2009, 2:16:04 PM4/27/09
to zxing
Well this just means it failed to find the barcode in the image --
it's 'normal'.
For reference, try uploading it the decoder at zxing.org/w/decode.jspx
-- if it works there then it may be an issue with the port.
Reply all
Reply to author
Forward
0 new messages