【问题标题】:Zxing library for QR codes in AndroidAndroid中二维码的Zxing库
【发布时间】:2013-12-18 09:37:44
【问题描述】:

我想实现一个读取 QR 码的 Android 应用程序。 我在这里阅读了几篇文章,但我没有提供任何我尝试过的选项,事实上,我在找到尽可能多的选项时遇到了一些麻烦。我尝试使用 Zxing 库执行以下操作,但效果不佳:

  • 将android的com包复制到我的src目录下。某些类中存在错误。
  • 将 core/src/main 的 com 包和 core/src/test 的 com 包复制到我的 src 目录下。
  • 将android-integration的com包复制到我的src目录下。
  • 使用“ant apache”创建一个包含核心目录的库 jar。 cmd中使用ant命令出错,apache下载的zip文件不知道怎么处理。
  • 下载“andorid-zxinglib-1.0”目录以将其作为库包含到项目中。

所使用的选项都不能让我正确运行我的项目。

如果有人可以帮助我并告诉我在 Eclipse 模拟器设置中使用我的应用程序读取 QR 码的基本步骤,尤其是将 Zxing 库添加到我的项目和代码中。

您好,非常感谢。

【问题讨论】:

标签: android ant zxing qr-code


【解决方案1】:

最简单的方法是从ZXing添加Core.jar库并关注this。但它需要从 Google PLAY 安装 BarcodeScanner 应用程序

更难的方法是你所尝试的。将 src 和 res 文件添加到您的项目中(可能在那里 资源有一些问题,但很容易解决)。此外,您应该添加 Core.jar。 这个解决方案更好,因为您不需要任何其他应用程序。但要困难得多。如果您对此有问题,我可以提供帮助

不要忘记添加权限。

编辑:

好的,再一步一步来。

  1. 下载http://code.google.com/p/zxing/downloads/detail?name=ZXing-2.3.0.zip&can=2&q=
  2. 进入安卓目录。
  3. 将 core-2.3.0.jar 作为外部 jar 添加到您的项目中(如果不存在,您必须使用 maven 构建 core 文件夹)。最后我加了here
  4. 将 ZXing 的 src 复制到您的 src 文件(将 com 目录从 ZXing 文件夹复制到您的 src 目录)
  5. 将 ZXing 的 res 复制到您的 res 文件中(将 ZXing 目录中的每个目录复制到您的 res 目录中)。重要的。如果您已经有一些与 ZXing 同名的文件,那么您应该重命名它们
  6. src 类中会有一些 roor。您应该输入每个类并使用您自己的 R 类更新对 R 类的引用 (例如将 import com.google.zxing.client.android.R; 替换为 import com.your.package.name.R;
  7. 扫描器类在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"/>

【讨论】:

  • 好的,我不需要在智能手机上安装 BarcodeScanner 应用程序,因为用户不舒服。我必须将哪些 src 和 res 文件添加到我的项目中?非常感谢。
  • 我从android 文件夹中添加整个src 和整个res 文件夹。那么可能在许多文件中 R 类会出现问题。所以你必须用你自己的替换 ZXing R 类导入
  • 好的,完成。但是我几乎所有卡在我的 src 目录中的包都有错误。
  • 什么样的错误?正如我所说,您必须将 import R class form 默认替换为 Yours
  • Zxing R类在哪里?
【解决方案2】:

添加到您的清单

<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"/>

并在清单中声明CaptureActivity 活动

然后在代码中

startActivityForResult(new Intent(this, CaptureActivity.class), 0);

【讨论】:

  • Permits 已经写好了,我已经在 Manifest 中添加了 CaptureActivity 类。在“onClick”方法中,我只添加了您在 startActivityForResult 中添加的行...但是当我按下按钮时,应用程序继续给出错误。
  • java.lang.ExceptionInitializerError
  • at java.lang.Class.newInstanceImpl(Native Method)
  • 你能发布整条消息吗?并更新您现在开始 Activity 的方式
  • 在 dalvik.system.NativeStart.main(Native Method) ... 引起:java.lang.NoClassDefFoundError: com.google.zxing.ResultMetadataType
【解决方案3】:

如果有人想现在(2015 年 7 月)在 android studio 上使用 Zxing,请关注此回复,希望对您有所帮助

https://stackoverflow.com/a/30628337/2904625

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多