我想实现一个读取QR码的Android应用程序。 我在这里阅读了几篇文章并且我没有提供任何我尝试过的选项,事实上我遇到了一些问题,因为我找到了很多选项。我尝试使用Zxing库执行以下操作但没有很好的结果:
所使用的选项都不允许我正确运行我的项目。
如果有人能帮助我并告诉我使用我的应用程序读取QR码的基本步骤,请在Eclipse模拟器设置中,特别是将Zxing库添加到我的项目和代码中。
问候,非常感谢。
答案 0 :(得分:3)
最简单的方法是从ZXing添加Core.jar库并关注this。但它需要安装来自Google PLAY的BarcodeScanner应用程序
更难的是你尝试的东西。将src和res文件添加到项目中(可能在那里 有一些资源问题,但很容易修复)。你还应该添加Core.jar。 这个解决方案更好,因为您不需要任何其他应用程序。但要困难得多。如果您遇到问题,我可以提供帮助
不要忘记添加权限。
编辑:
好的,继续一步一步。
src
复制到您的src
文件(从ZXing文件夹复制com目录到您的src目录)res
复制到您的res
文件(将每个目录从ZXing的目录复制到您的资源目录)。重要。如果您已经有一些与ZXing同名的文件,那么您应该重命名它们com.google.zxing.client.android.R;
替换为import com.your.package.name.R;
)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"/>
答案 1 :(得分:1)
添加到您的清单
<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);
答案 2 :(得分:0)
如果有人想在Android工作室使用Zxing(jul-2015),请按照此回复,希望此帮助