我想实现一个读取二维码的android应用程序。
我在这里读了好几篇文章,我没有提供任何我尝试过的选项,事实上,我发现有很多选项都有问题。我试着用下面的程序库完成以下操作,但效果不佳:
把android的com包复制到我的src目录。有些课程有错误。
将core/src/main的com包和core/src/test的com包复制到我的src目录。
将android集成的com包复制到我的src目录。
使用“ant apache”创建一个带有核心目录的库jar。在cmd中使用“ant”命令时出错,我不知道如何处理下载的apache的zip文件。
下载“ANDORID-ZANLILB-1.0”目录,将其作为一个库包含到项目中。
使用的任何选项都不允许我正确运行项目。
如果有人可以帮助我,告诉我用我的应用程序读取QR代码的基本步骤,无论是在Eclipse仿真器设置中,还是特别是将Zigin库添加到我的项目和代码中。
你好,非常感谢。

最佳答案

最简单的方法是从zxing添加core.jar库并遵循this。但它需要安装google play的barcodescanner应用程序
更难的方法是你尝试。将src和res文件添加到项目中(可能有
是一些资源问题,但很容易解决)。还应该添加core.jar。
这个解决方案更好,因为你不需要任何其他应用程序。但要困难得多。如果你有问题我可以帮忙
不要忘记添加权限。
编辑:
好吧,每次都是一步一步的。
下载http://code.google.com/p/zxing/downloads/detail?name=ZXing-2.3.0.zip&can=2&q=
输入android目录。
将core-2.3.0.jar作为外部jar添加到项目中(如果不存在,则必须使用maven构建core文件夹)。最后我添加了here
将zxing的src复制到src文件(将com目录从zxing文件夹复制到src目录)
将zxing的res复制到res文件(将zxing目录中的每个目录复制到res目录)。很重要。如果你已经有一些和zxing同名的文件,那么你应该重命名它们
src类中会有一些roor。您应该输入每个类并用自己的r类更新对r类的引用
(例如将importcom.google.zxing.client.android.R;替换为import com.your.package.name.R;
scanner类位于CaptureActiviy.class
您还可以创建自己的捕获活动

/*
 * 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();
  }
}

和权限
<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"/>

关于android - Zxing库,用于Android中的QR码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20654385/

10-12 00:23
查看更多