7

i would like to implement a QR Code/Barcode reader within my application. I would like to know what is the most lightweight solution to do this (disregarding intent integrator from zxing).

user1437481
  • 470
  • 1
  • 9
  • 30
  • 2
    what is wrong with intent integrator from zxing? – kjurkovic Dec 10 '12 at 15:29
  • 2
    i dont want users to leave the app in order to download barcode scanner, i want a solution within the app – user1437481 Dec 10 '12 at 15:30
  • Zxing provide complete code, you can integrate with your app – Anand Tiwari Dec 10 '12 at 15:32
  • 1
    you can use zxing as part of your application. Just add it as a library and use it calling the capture intent. – Adrián Rodríguez Dec 10 '12 at 15:34
  • http://www.jmanzano.es/blog/?p=244 you can look at the tutorial – Talha Dec 10 '12 at 15:34
  • Even though you seem deadset against it, the Intent integration is the best solution. If you (and everyone else) include copies of the decoding libraries in your own projects the end result is that the user ends up with several copies of decoding libraries all in separate apps wasting space on their device. The whole (android) system is built upon the idea of using Intents in this manner, you should embrace it IMO. – FoamyGuy Dec 10 '12 at 15:36
  • @FoamyGuy your boss won't be happy when the user have to leave the app to download 3rd party app to do a scan. – wtsang02 Dec 10 '12 at 15:38
  • @wtsang02 I've integrated via intents every time I've had to incorporate scanning into a project...I've had no pushback from anyone I've worked for. – FoamyGuy Dec 10 '12 at 15:40
  • I understand that intents is the best option as android enables that kind of "modular" coding,but its not my app, im just doing what im told.what I am looking for is another solution – user1437481 Dec 10 '12 at 15:45
  • @Talha Link you posted is broken now. You can find the new one here: http://jmanzano.me/integrating-zxing-in-our-own-android-app-barcodescanner/ – Javier Manzano Aug 08 '14 at 07:59

3 Answers3

10

I used zxing to build into my application. You will need a bit of coding. First include core.jar , its at core/core.jar,in your build path, then go to their client ,its at android/..../com.google.zxing, and get their code(This is not recommended by the devs, because your copy and pasting.) last, Add this code:

   package com.wtsang02.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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.common.HybridBinarizer;


public class QRDecoder extends Activity implements OnClickListener {

    private String text;
    private Button webbutton;
    private Bitmap bmp;
    private ImageView ivPicture;
    private TextView textv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mysales);
        webbutton = (Button)findViewById(R.id.webbutton);

        ivPicture = (ImageView) findViewById(R.id.ivPicture);
        textv= (TextView) findViewById(R.id.mytext);

        webbutton.setOnClickListener(this);
    }

    private void decode() {


        if (bmp == null) {
            Log.i("tag", "wtf");
        }
        bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);

        int[] intArray = new int[bmp.getWidth() * bmp.getHeight()];
        bmp.getPixels(intArray, 0, bmp.getWidth(), 0, 0, bmp.getWidth(),
                bmp.getHeight());

        LuminanceSource source = new com.google.zxing.RGBLuminanceSource(
                bmp.getWidth(), bmp.getHeight(), intArray);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Reader reader = new MultiFormatReader();
        try {
            Result result = reader.decode(bitmap);

            text = result.getText();
            byte[] rawBytes = result.getRawBytes();
            BarcodeFormat format = result.getBarcodeFormat();
            ResultPoint[] points = result.getResultPoints();
            textv.setText(text);

        } catch (NotFoundException e) {

            e.printStackTrace();
        } catch (ChecksumException e) {

            e.printStackTrace();
        } catch (FormatException e) {

            e.printStackTrace();

        }
        Log.i("done", "done");
        if(text!=null)
        Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG).show();
        else{
            Toast.makeText(getBaseContext(), "QQ", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onClick(View v) {

        Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            bmp = (Bitmap) extras.get("data");
            ivPicture.setImageBitmap(bmp);
            decode();
        }

    }

}

This code will use your phone's default camera, if you need to use their client, you will need to start their CaptureActivity, Your layout should include a TextView to show results, ImageView to show the image you captured, and Button to start the camera. . This is based off of 2.1zxing.

wtsang02
  • 18,603
  • 10
  • 49
  • 67
  • get what code from the client?i misunderstood that part – user1437481 Dec 10 '12 at 15:47
  • +1 for showing how to build a simple new app instead of copying the Barcode Scanner app totally. – Sean Owen Dec 10 '12 at 15:49
  • updated the post, where i state the location. Its basiclly all in the zip file, where you downloaded zxing2.1 – wtsang02 Dec 10 '12 at 15:51
  • 1
    This code throws me exception as soon as it starts and app crashes :( – tejas Feb 12 '13 at 05:12
  • com.google.zxing.NotFoundException, i was asked to add this exception while writing the lines LuminanceSource source = new RGBLuminanceSource(200, 200, intArray); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Reader reader = new MultiFormatReader(); Result result = reader.decode(bitmap); – tejas Feb 12 '13 at 07:32
  • While I debugged for it, I found that bitmap object is null I mean matrix is null in that. So it is throwing exception while decoding it at Result result = reader.decode(bitmap); – tejas Feb 12 '13 at 07:33
  • I have followed exact steps mentioned by you.Image is getting captured and is setting to the imageview but only while decoding it gives me the problem and string text becomes null and else block is executed – tejas Feb 12 '13 at 07:36
  • If you get NotFoundExcetion , 2 reasons, 1) have you correctly import the library? 2) have you added the Activity to your manifest? Since this exact code works on my device, I don't think the code is the problem. – wtsang02 Feb 12 '13 at 14:51
2

You can use:

Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21
0

Hey download a sample of Biggu Barcode Scanner from this link, extract the Demo Project and import it to eclipse. The zip file has Demo Example, which you can use and integrate in your app as per your requirement

TNR
  • 5,839
  • 3
  • 33
  • 62