Search code examples
androidantzxingqr-code

Zxing library for QR codes in Android


I want to implement an Android application that reads QR codes. I have read several post here and I don't serve any of the options I 've tried, in fact I have some trouble with as many options as I've found. I tried to do the following with the Zxing library without good result:

  • Copy com package of android to my src directory. There are errors in some classes.
  • Copy com package of core/src/main , and com package of core/src/test to my src directory.
  • Copy com package of android-integration to my src directory.
  • Create a library jar with the core directory using 'ant apache'. Error using the 'ant' command in cmd, I don't know what to do with the zip file downloaded of apache.
  • Download the 'andorid-zxinglib-1.0' directory to include it as a library to the project.

None of the options used allows me to run my project properly.

Please if anyone can help me and tell me the basic steps to read QR codes with my application, both in Eclipse emulator settings, as especially to add the Zxing library to my project and code for it.

Greetings , thank you very much.


Solution

  • The simplest way is to add Core.jar library from ZXing and follow this. But it require install BarcodeScanner app from Google PLAY

    The harder way is that what You try. Add src and res files to your project (probably there were some problems with resources, but it is easy to fix). Also You should add Core.jar. This solution is better, becouse You don't need any other apps. But is much harder. I can help if You will have problems with that

    Don't forget add permissions.

    Edit:

    Ok, everytihng again step by step.

    1. Download http://code.google.com/p/zxing/downloads/detail?name=ZXing-2.3.0.zip&can=2&q=
    2. Enter android catalog.
    3. Add core-2.3.0.jar as external jar to your project (if not present, the you have to build core folder with maven). Eventually I added here
    4. copy ZXing's src to your src files (copy com catalog from ZXing folder to your src catalog)
    5. copy ZXing's res to your res files (copy every catalog from ZXing's catalog to your res catalog). IMPORTANT. If you already have some files with the same name as ZXing's then you should rename them
    6. There will be some roors in src classes. You should enter every class and update reference to R class with your own R class (eg replace import com.google.zxing.client.android.R; to import com.your.package.name.R;)
    7. The scanner class is in CaptureActiviy.class

    You can also create your own Capture Activity

    /*
     * Copyright (C) 2008 ZXing authors
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    
    import com.yourpackage.name.R;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.ProgressDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.Button;
    import android.widget.FrameLayout;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    
    import com.google.zxing.Result;
    import com.google.zxing.client.android.CaptureActivityHandler;
    import com.google.zxing.client.android.ViewfinderView;
    import com.google.zxing.client.android.camera.CameraManager;
    import com.google.zxing.client.android.result.ResultHandler;
    import com.google.zxing.client.android.result.ResultHandlerFactory;
    
    public final class CaptureActivity extends Activity implements SurfaceHolder.Callback 
    {
      private CameraManager cameraManager=null;
      private CaptureActivityHandler handler=null;
      private Result savedResultToShow=null;
      private ViewfinderView viewfinderView=null;
      private boolean hasSurface=false;
      ProgressDialog progressDialog=null;
    
      FrameLayout root=null;
    
      @Override
        protected void onStop() {
            super.onStop();
    
            if(progressDialog!=null)
            {
                progressDialog.dismiss();
            }
        }
    
      public ViewfinderView getViewfinderView() {
        return viewfinderView;
      }
    
      public Handler getHandler() {
        return handler;
      }
    
      public CameraManager getCameraManager() {
        return cameraManager;
      }
    
      @Override
      public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        restoreLanguage(null);
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.capture);
      }
    
      @Override
      protected void onResume() {
        super.onResume();aManager = new CameraManager(getApplication());
        viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
        viewfinderView.setCameraManager(cameraManager);
    
    
        handler = null;
    
        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
        SurfaceHolder surfaceHolder = surfaceView.getHolder();
        if (hasSurface) 
        {
          initCamera(surfaceHolder);
        } 
        else 
        {
          surfaceHolder.addCallback(this);
          surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
      }
    
    
      @Override
      protected void onPause() 
      {
        if (handler != null) 
        {
          handler.quitSynchronously();
          handler = null;
        }
    
        cameraManager.closeDriver();
        if (!hasSurface) 
        {
          SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
          SurfaceHolder surfaceHolder = surfaceView.getHolder();
          surfaceHolder.removeCallback(this);
        }
    
        super.onPause();
      }
    
      @Override
      protected void onDestroy() {
        super.onDestroy();
      }
    
      private void decodeOrStoreSavedBitmap(Bitmap bitmap, Result result) 
      {
        if (handler == null) {
          savedResultToShow = result;
        } else {
          if (result != null) {
            savedResultToShow = result;
          }
          if (savedResultToShow != null) {
            Message message = Message.obtain(handler, R.id.decode_succeeded, savedResultToShow);
            handler.sendMessage(message);
          }
          savedResultToShow = null;
        }
      }
    
      @Override
      public void surfaceCreated(SurfaceHolder holder) {
        if (holder == null) {
        }
        if (!hasSurface) {
          hasSurface = true;
          initCamera(holder);
        }
      }
    
      @Override
      public void surfaceDestroyed(SurfaceHolder holder) 
      {
        hasSurface = false;
        cameraManager.stopPreview();
        cameraManager.closeDriver();
      }
    
      @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
      {
      }
    
      public void handleDecode(Result rawResult, Bitmap barcode) 
      {
    
        ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(this, rawResult);
    
        if (barcode == null) 
        {
          handleDecodeInternally(rawResult, resultHandler, null);
        } 
        else 
        {
                handleDecodeInternally(rawResult, resultHandler, barcode);
        }
      }
    
      @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) 
      {
            super.onActivityResult(requestCode, resultCode, data);
      }
    
      private void handleDecodeInternally(Result rawResult, ResultHandler resultHandler, Bitmap barcode) 
      {
        CharSequence displayContents = resultHandler.getDisplayContents(); //here is readed code. Do ehatever you want
    
        cameraManager.stopPreview();
    }
    
      private void initCamera(SurfaceHolder surfaceHolder) 
      {
        try 
        {
          cameraManager.openDriver(surfaceHolder);
          if (handler == null) 
          {
            handler = new CaptureActivityHandler(this, null, null, cameraManager);
          }
          decodeOrStoreSavedBitmap(null, null);
        } catch (Exception e) 
        {
        }
      }
    
      public void drawViewfinder() 
      {
        viewfinderView.drawViewfinder();
      }
    }
    

    and Permissions

    <uses-permission android:name="android.permission.CAMERA"/>
         <uses-permission
        android:name="android.permission.FLASHLIGHT"
        android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
        android:protectionLevel="normal" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
      <uses-feature android:name="android.hardware.camera"/>
      <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
    
      <uses-feature android:name="android.hardware.camera.flash" android:required="false"/>