最简单的方法是从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=
- 进入安卓目录。
- 将 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 类的引用
(例如将 import
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"/>