Feed image to android emulators camera - android

I have developed an application that calls the ZXing library to invoke the camera.
I have now just been told that we need this working in the emulator for testing purposes.
I don't want to change the code to read from a file, I wan't to call the library, which will call the camera and I wan't the camera to think its looking at an image (if that makes sense).
I've seen a lot around of using webcams but I don't want a live feed.
Does anyone know if this is at all possible?

It depends on how you're integrating with Zxing. If you are scanning barcodes via intent, then the web-cam style live feed may be your only option.
AFAIK there is a reader class that expects you to supply the images. Something like this...
Reader reader = new MultiFormatReader(); //If you are calling the reader in your code
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); //The source as
//supplied by you
Result result = reader.decode(bitmap);
String text = result.getText();
Note that the above code is bits and pieces taken from here .
So it looks like you should be able to supply some image and have the bar code reader take a shot at it.

Related

Revert Barcode object to image in Android

I am getting Barcode(com.google.android.gms.vision.barcode) object from a physical sales card via camera. Is there a way to convert it back to a image using gms:play-services-vision library?
I think you will get barcode value in a string. you may try to below code
TextView tv = (TextView)findViewById(R.id.textview);
tv.setText(bitmapstring);
tv.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.imageview);
img.setImageBitmap(tv.getDrawingCache());
Seems like there's no way of doing that by using the play-services-vision library. My first thought would be to take a screen capture of the preview of the camera before actually reading the barcode, but let's suppose you cannot do that.
If you have the actual data and the format of the barcode you can use ZXing to generate back the barcode image.
Here's an example of how to do it: Generate barcode image in Android application

Display Bitmaps in Xamarin Forms created in Android and iOS

There are the Bitmap for Android and UIImage for iOS. Is there a way to display both somehow in the Xamarin Forms Image control?
Obviously I need the Dependency Service. I will have two implementations that create either a bitmap or an uiimage using some source, but how do I bring those two products together to a single forms control? Both Android and iOS methods have to return something, that the image control can understand and display. I don't know what that might be.
Edit: I look for a way where I don't use storage space, if possible.
Edit2:
I tried Jasons suggestion and it works fine.
I create a bitmap in the Android project and return a MemoryStream object:
MemoryStream stream = new MemoryStream();
newImage.Compress(Bitmap.CompressFormat.Png, 0, stream);
return stream;
Then I consume it in my Xamarin.Forms Image control:
var stream = DependencyService.Get<ICrossPlatformImageProcesor>().Combine_Images(imagePath);
stream.Position = 0;
img_ImageView.Source = Xamarin.Forms.ImageSource.FromStream(() => stream);
I will have two implementations that create either a bitmap or an uiimage using some source, but how do I bring those two products together to a single forms control?
You can simply use Image Control of xamarin forms, images can be loaded specifically for each platform, or they can be downloaded for display.
For more information, you can refer to Working with Images.
I look for a way where I don't use storage space, if possible.
I'm not quite understand this, if you mean don't use memory, then I think it is not possible. If you mean your images are not saved in storage, then possibly you have an URL address on internet of your images?
Anyway, Image control in Xamarin.Forms support image source form ImageSource instance, file, Uri, and resources, to load image from uri, you can simply code like this:
var webImage = new Image { Aspect = Aspect.AspectFit };
webImage.Source = ImageSource.FromUri(new Uri("https://xamarin.com/content/images/pages/forms/example-app.png"));

Zxing detects on Android device but not on pc

I am using Zxing core.jar to scan bar codes and qr codes and it's working perfectly, but I want to make tests using Robolectric, to run automated tests without an emulator or device, that will take N number of images through zxing, so my problem is that zxing for some reason is not detecting any type of code format from the .jpg files, I'm not sure if there is a limit size on the image, or something else that I'm missing. The piece of code that does the detection is this, which works fine with frame previews converted to jpgs
BinaryBitmap bitmap = getBitmapFromImageData(input);
Collection<BarcodeFormat> decodeFormats = getDecodeFormats();
Map<DecodeHintType, Object> hints = getHints(decodeFormats);
MultiFormatReader multiFormatReader = getMultiFormatReader(hints);
Result rawResult;
try {
rawResult = multiFormatReader.decodeWithState(bitmap);
} catch (NotFoundException e) {
// not really an error, just QR code not found
Log.e(LOG_TAG, "Code Detection not found exception.");
return null;
}
Where the input contains the byte array in jpg format and is being converted by getBitmapFromImageData to Zxings internal formats. And like I said the library is working fine in the device, but I can't find any logical reason on why running this code from my pc won't work.
You're not showing any key code, like how you are reading your image. You should check whether it is being read correctly, being binarized correctly by your code, etc. Look at the unit tests in the project to see how to correctly read and process and image on the desktop. I don't know what your code may be doing in the "get" methods that is different.

ZXing: Scanning Barcode with UPC + 5 Supplemental

I'm trying to scan barcode as shown below using the ZXing library.
(source: minus.com)
// start scanning
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent, REQUEST_CODE);
If I replace "ONE_D_MODE" with "TWO_D_MODE", the app can successfully detect both barcodes; but the value of result won't change (still 051488005995).
// onActivityResult
if(requestCode == REQUEST_CODE && resultCode == RESULT_OK)
String result = intent.getStringExtra("SCAN_RESULT");
Is there any way to get both barcode value 051488005995 and 50115?
Any other way to obtain the isbn (0142501158) above the barcodes without getting the supplemental +5 barcode (50115) would also be great.
Thanks.
ONE_D_MODE will work for you. You probably really want PRODUCT_MODE. There is no such thing as TWO_D_MODE. By setting this it just scans all default formats.
It is not scanning both barcodes. It is just scanning the product code. So I'm not sure what you mean about getting just the product code: that's what you already have. I assume you want both.
MultipleBarcodeReader is not quite for this situation as no part of the library scans for the UPC/EAN supplement by itself. It is scanned for as an extension to UPC and EAN codes only.
It will already scan for some types of extension barcode in UPCEANExtensionSupport. It doesn't give you back the raw values but rather tries to parse out metadata and returns that in result metadata. If that's what you really want, it already does this. Otherwise you have to modify the code.
If it reads the UPC/EAN code but can't find an extension code, it will not fail the scan, and will only return the primary code. If you want it to only return if both are found, again you'd have to change your copy of the core library.
FYI you can scan UPC 12+5 codes via Intent/zxing. The only catch is that you have to force the +5, so you can't scan normal barcodes.
From my thread with #srowen: https://github.com/zxing/zxing/issues/217#issuecomment-54818496
Passed the hint as an extra to the Intent, inside IntentIntegrator=>initiateScan (I'll have to make an overriden method to make this optional later):
// Force 5 digit extension
intentScan.putExtra("ALLOWED_EAN_EXTENSIONS", new int[] {5});
I confirmed it was recognized from my Android logcat:
DecodeHintManager﹕ Hints from the Intent: {ALLOWED_EAN_EXTENSIONS=[I#42a38540}
Retrieved extension values in my scan result:
String extension = intent.getStringExtra("SCAN_RESULT_UPC_EAN_EXTENSION");
Now I got some UPC 12+5 :)
Content:079808007955, Format:UPC_A, Extension: 74700
As far as I know, this is not possible using zxing via an Intent.
However, you can embed ZXing in your code directly (by adding the ZXing source to your code directory). Then you are able to use the com.google.zxing.MultipleBarcodeReader. The function decodeMultiple() returns an array of barcodes which then can be processed further.
A small example:
// data: YUV camera preview; width/height: preview size
Result[] decode(byte[] data, int width, int height) {
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
MultipleBarcodeReader reader = new MultipleBarcodeReader(new MultiFormatOneDReader(null));
return reader.decodeMultiple(bitmap);
}

zxing not found exception in android

Hello everyone,
If any one of you can help me this. I am using zxing to decode barCode image, but it returns com.google.zxing.NotFoundException, don't know why. The same image gets decoded via Intent provided to zxing, but not when I use it to decode from image file.
The code that I am using is below :
mMultiFormatReader = new MultiFormatReader();
mMultiFormatReader.setHints(null);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new RGBLuminanceSource(path)));
Result result = mMultiFormatReader.decodeWithState(bitmap);
I don't think it's exactly the same image, since you can't have it scan a file by Intent. I assume you mean that you can scan the image off your screen fine, but the image itself does not decode.
That's just life, really. Some images won't happen to decode. But you may try TRY_HARDER mode or use a different binarizer to see if that works.

Categories

Resources