Embed Zxing library without using Barcode Scanner app [duplicate]
Asked Answered
J

7

57

What is the preferred way to embed the zxing library to provide scanning without having the Barcode Scanner app installed? I am looking to embed it in android without having to prompt the user for any extra installs. (Similar to the way the iPhone src works).

Jemine answered 31/1, 2011 at 18:13 Comment(2)
Download the core and javase components and embed them, perhaps.Rex
But, you need to look at the license and see what you are allowed to do, whether you can just include it in your program.Rex
A
60

It's really easier to just integrate via Intent. More reliable, and you get updates automatically. While I don't advise it...

The complete source code is available from the ZXing project. You want to build core.jar from core/ and put it in your Android lib/ folder to include the core decoder in your app. You do not want to include javase. You can look to the code in android/ as it is the source to Barcode Scanner, but as the authors we'd suggest you not just copy and paste it.

It is Apache licensed which means you can use it freely, as long as you essentially give the user access to the license terms.

Andrews answered 31/1, 2011 at 18:34 Comment(19)
Wouldn't it make sense to have an Android library version? Sometimes asking users to download a 3rd party app is just not a solution...Mcnalley
There is already a library solution: core.jar. The entire Android app is available for reuse too (and sometimes is unethically copied). I'm not sure what can be easier.Andrews
There is a lot of android-specific code outside of core.jar. I think that MasterScrat was asking for an Android library project. Something that would give you views and camera support, etc, without everything that BarcodeScanner needs.Wade
I understand that. The parts that are really reusable as a library are already made available in core. The Android code is not so package-able; source is probably the most meaningful 'packaging' for these bits. Providing a complete embeddable UI is not a goal -- the whole app is accessible by Intent as an API already.Andrews
@SeanOwen I Have just downloaded Zxing and I can't find the core.jar in Core folder.. Where can I find it ?Triadelphous
Probably best to ask questions at groups.google.com/forum/?fromgroups#!forum/zxing . You can build it from the source you have, use Maven to depend on it in your Maven project, or get the .jar from Maven's repo directly: search.maven.org/…Andrews
Hello @SeanOwen I have some question for you. Can we implement bar code comparison using zxingGault
Wow core.jar comes out to 126 MB. That's quite large for an Android app. Does that make sense?Aeschylus
@Imray Not sure what you are looking at. It is about 0.5MB: search.maven.org/… Less after proguard shrinks things.Andrews
@SeanOwen I'm looking at the core folder in zxing-master, from the Github pageAeschylus
@Imray then you're looking at the size of all the test images too. That's not part of the artifact of course.Andrews
@SeanOwen so which core are you referring to?Aeschylus
@Imray the actual published artifact that you would depend on in your project and build into your app: search.maven.org/…Andrews
It seems there is no manual to build-in ZXing in Android app (and I don't know why). Just used ZBar instead.Gobert
@konopko the manual is github.com/zxing/zxing/wiki/Scanning-Via-IntentAndrews
@SeanOwen Dear Sean, I guess you not completely realize what people wants. They (and me too) do not want use your great Barcode Scanner app via Intent, because a user should install additional app (bad UX). We want to BUILD-IN the QR lib in our apps.Gobert
@konopko the decoding library is already broken out as a library. You just want a pre-made app to clone. I do not want you to do that. It's not really how Android works either. Your argument and others is just that it would be easier or more profitable than making your own app. I don't find that compelling.Andrews
It's bad User experience to demand that users download a third-party app for such a one-time task.Guild
@RowlandMtetezi then you should create your own embedded QR reader using this library. That's your job. Intents are also an option.Andrews
E
29

Android QR/Barcode/Multiformat Decoder.

I have made an Android application using the ZXing APIs and embedded only the decoding code into my application. The input to this decoder was given through the SD card of the Android emulator.

Here are the steps:

  1. First, I created an AVD(emulator) version 4 in my Eclipse IDE with the SDcard and Camera features turned ON.

  2. Next, I have created an SDCard using the commands below in the command prompt:

    c:\>mksdcard 40M mysdcard.iso
    

where 40M is the size of the SD card that i have created..This will be saved in the C: drive. Note, the .iso part is important.

  1. Next, we have to mount the SD card into the emulator using the commands below in the command prompt:

     c:\>emulator -sdcard "c:\mysdcard.iso" @myavd4
    

Here myavd4 is the name of the emulator/android virtual device that I created in step 1. The '@' sign before the avd name is important too.

Keep the emulator running all the time..If it gets closed, we have to redo the above 3 steps.

  1. We can push the QR code or other code images that we have to this SD card mounted on our emulator by using the commands below in the command prompt:

    c:\>adb push "c:\myqrcode.png" /sdcard
    
  2. Next, in the Eclipse IDE, start a new android project. The code below should be pasted in the QRDecoder.java file of our project.

    package com.example.palani;
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.TextView;
    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.BinaryBitmap;
    import com.google.zxing.ChecksumException;
    import com.google.zxing.FormatException;
    import com.google.zxing.LuminanceSource;
    import com.google.zxing.MultiFormatReader;
    import com.google.zxing.NotFoundException;
    import com.google.zxing.Reader;
    import com.google.zxing.Result;
    import com.google.zxing.ResultPoint;
    import com.google.zxing.client.androidtest.RGBLuminanceSource;
    import com.google.zxing.common.HybridBinarizer;
    public class QRDecoder extends Activity implements OnClickListener {
        public static class Global
        {
            public static String text=null;
        }
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Bitmap bMap = BitmapFactory.decodeFile("/sdcard/myqrcode.png");
            TextView textv = (TextView) findViewById(R.id.mytext);
            View webbutton=findViewById(R.id.webbutton);
            LuminanceSource source = new RGBLuminanceSource(bMap); 
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            Reader reader = new MultiFormatReader();
            try {
                 Result result = reader.decode(bitmap);
                 Global.text = result.getText(); 
                    byte[] rawBytes = result.getRawBytes(); 
                    BarcodeFormat format = result.getBarcodeFormat(); 
                    ResultPoint[] points = result.getResultPoints();
                    textv.setText(Global.text);
                    webbutton.setOnClickListener(this);
            } catch (NotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ChecksumException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (FormatException e) {
                // TODO Auto-generated catch block
        e.printStackTrace();
    
    
            }   
        }
    
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse(Global.text); 
            Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
            startActivity(intent);
    
        }
    
    }
    
  3. Next I downloaded the ZXing Source Code (ZXing-1.6.zip) from the below link.

    http://code.google.com/p/zxing/downloads/list
    

Then, extract this and navigate to D:\zxing-1.6\core\src\com

copy the com folder and paste it in our package in Eclipse.

(Note, right click on the package of our project and paste...if it asks for replacing the existing folder, select yes)

  1. Next, copy and paste the below code in the res/layout/main.xml file

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="20dip"
        >
    
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    
    android:textColor="@color/mbackground1"
    android:gravity="center_horizontal"
    android:text="@string/decode_label"
    android:padding="20dip" 
    />
    
    <TextView
    android:id="@+id/mytext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:background="@color/mbackground2" 
    android:textColor="@color/mytextcolor" 
    android:padding="20dip"
    />
    
    
     <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/continue_label"
    android:gravity="center_horizontal"
    android:textColor="@color/mytextcolor"
    android:padding="20dip"
    />
    
    <Button 
    android:id="@+id/webbutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/web_button"
    android:textColor="@color/mytextcolor"
    />
    
    </LinearLayout>
    
  2. Next, copy and paste the below code in the res/values/strings.xml file

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, QRDecoder!</string>
        <string name="app_name">QRDecoder</string>
        <string name="continue_label">Click below to load the URL!!</string>
        <string name="web_button">Load the URL!!</string>
        <string name="decode_label">Decoded URL</string>
    
    </resources>
    
  3. Next, copy and paste the below code in the res/values/color.xml file, if it does not exist, create one.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <color name="mbackground1">#7cfc00</color>
    <color name="mbackground2">#ffff00</color>
    <color name="mytextcolor">#d2691e</color>
    </resources>
    
  4. Next, copy and paste the below code in the manifest file after the opening tag

    <manifest>
    
    
    <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />      
    
  5. So, these above steps done...our application is ready. Now, u can run the application and it will give u the decoded result of the input image we have given.

  6. In order to change the input, push another file to SD card using the command below in the command prompt

    c:\>adb push "c:\image2.png" /sdcard
    

and change the input in our QRDecoder.java to reflect the same

    Bitmap bMap = BitmapFactory.decodeFile("/sdcard/image.png");

the inputs can be any format like QRCode, Barcode, etc....the types of image can be bmp, jpg or png.

I used the below website for generating the QR codes for test purpose

http://barcode.tec-it.com/

AND http://qrcode.kaywa.com

Thanks and I would like to mention the point that I am just a beginner in android and mobile application development and sorry for any mistakes that I might have done...

Escuage answered 10/2, 2011 at 9:6 Comment(1)
Great description. Really helped me to get my barcode reader running. Thank you!Hailey
O
4

If are following Palani answer and only want to import zxing core. Here is how you can use RGBLuminanceSource without importing zxing.androidtest.

// import com.google.zxing.client.androidtest.RGBLuminanceSource;
import com.google.zxing.RGBLuminanceSource;

// Bitmap mBitmap; // some bitmap...

int width = mBitmap.getWidth();
int height = mBitmap.getHeight();
int[] pixels = new int[width * height];
mBitmap.getPixels(pixels, 0, width, 0, 0, width, height);

RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);

BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));

try {
   Result result = zxingReader.decode(binaryBitmap);
} catch (Exception e) {
   e.printStackTrace();
}
Owenism answered 1/2, 2014 at 22:48 Comment(1)
Yes that is what I showed here instead of using RGBLuminanceSource from com.google.zxing.client.androidtest, like in Palani's answer, which has different parameters.Owenism
B
4

Now you can use the official Barcode API from Google:

The Barcode API detects barcodes in real-time, on device, in any orientation. It can also detect multiple barcodes at once.

It reads the following barcode formats:

  • 1D barcodes: EAN-13, EAN-8, UPC-A, UPC-E, Code-39, Code-93, Code-128, ITF, Codabar
  • 2D barcodes: QR Code, Data Matrix, PDF-417, AZTEC

It automatically parses QR Codes, Data Matrix, PDF-417, and Aztec values, for the following supported formats:

  • URL
  • Contact information (VCARD, etc.)
  • Calendar event
  • Email
  • Phone
  • SMS
  • ISBN
  • WiFi
  • Geo-location (latitude and longitude)
  • AAMVA driver license/ID
Belloc answered 18/8, 2015 at 10:57 Comment(6)
Only if your user has an active internet connection, not suitable for our warehouse!Handmaiden
to use this api Google play service version 7.8 is required.Capitalistic
@dave: have you tested that? The post specifically says "Importantly, all barcode parsing is done locally", and the codelab specifically mentions "Importantly, all bar code parsing is done locally, so you don't need to do a server round trip to read the data from the code. "Granese
@DanDascalescu: I did at the time, our warehouse network does not have an connection to the outside world, and using the code resulted in an error as the latest version of google play services could not be checked / downloaded. The scanning itself may be local, but google play service requirement was not. I haven't tested it again recently so not aware if this has changed.Handmaiden
@dave: Maybe once you have the latest Google Play Services installed, scanning works locally?Granese
@DanDascalescu Maybe the case, if the requirement for something like this comes up again I will give it a go, the main problem being that the units don't have a google account tied to them, so could be an effort to update them!Handmaiden
T
2

I myself tried this method an most of all seemed to work.

Though I have a few points to make

  1. It will complain about the package com.google.zxing.client.androidtest which is needed for the RGBLuminanceSource class found in the package and used in the QRDecoder Activity.So import the zxing/androidtest package as well.

  2. If you are adding the Zxing Library outside your package then you will need to edit all the R.java references as it wont find the R.java file in its package.

For Example:

Instead of

mRunBenchmarkButton = (Button) findViewById(R.id.benchmark_run);

in the BenchmarkActivity.java file use

mRunBenchmarkButton = (Button) findViewById(yourpackage.R.id.benchmark_run);

We can also use the DDMS interface of Eclipse to push the QRCode to the device SDCard.

Using DDMS

Tanh answered 21/2, 2012 at 12:26 Comment(0)
H
0

https://github.com/dm77/barcodescanner

I prefered this lib over Google Play Services because as usual, Google Play Services requires the same version installed on the device.

It embeds Zxing with the new build system and provides an aar. Really cool.

Hatchet answered 29/10, 2015 at 15:37 Comment(0)
W
-1

I tried to embed ZXing (XZing) for a while, until i discovered Zbar. They have a easyer way of embeding, less code and easy examples.

http://sourceforge.net/projects/zbar/

Willianwillie answered 28/2, 2013 at 9:25 Comment(2)
ZBar hasn't been updated in more than a year.Aeschylus
ZBar hasn't been updated since Feb 2013.Granese

© 2022 - 2024 — McMap. All rights reserved.