Android 二维码扫描怎样实现第二次扫描
3个回答
2015-06-01 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517193
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
首先看一下扫描界面
CaptureCodeActivity.java代码:
package com.haier.qr.code;
import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
import java.util.Vector;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.DecodeHintType;
import com.google.zxing.Result;
import com.haier.cabinet.customer.R;
import com.haier.cabinet.customer.activity.DeliveryBoxListActivity;
import com.haier.common.util.AppToast;
import com.haier.qr.code.zxing.camera.CameraManager;
import com.haier.qr.code.zxing.decoding.CaptureActivityHandler;
import com.haier.qr.code.zxing.decoding.FinishListener;
import com.haier.qr.code.zxing.decoding.InactivityTimer;
import com.haier.qr.code.zxing.view.ViewfinderView;
/**
* Initial the camera
*
* @author jdsjlzx
*/
public class CaptureCodeActivity extends Activity implements Callback,View.OnClickListener {
private static final String TAG = CaptureCodeActivity.class
.getSimpleName();
private CameraManager cameraManager;
private CaptureActivityHandler handler;
private ViewfinderView viewfinderView;
private boolean hasSurface;
private Map<DecodeHintType, ?> decodeHints;
private Vector<BarcodeFormat> decodeFormats;
private String characterSet;
private InactivityTimer inactivityTimer;
private boolean isFlashlightOpen;
/**
* 声音震动管理器。如果扫描成功后可以播放一段音频,也可以震动提醒,可以通过配置来决定扫描成功后的行为。
*/
private BeepManager beepManager;
/**
* 闪光灯调节器。自动检测环境光线强弱并决定是否开启闪光灯
*/
private AmbientLightManager ambientLightManager;
public CameraManager getCameraManager() {
return cameraManager;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_capture);
hasSurface = false;
inactivityTimer = new InactivityTimer(this);
beepManager = new BeepManager(this);
ambientLightManager = new AmbientLightManager(this);
//按钮监听事件
findViewById(R.id.capture_flashlight).setOnClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
cameraManager = new CameraManager(getApplication());
viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
viewfinderView.setCameraManager(cameraManager);
viewfinderView.setVisibility(View.VISIBLE);
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
if (hasSurface) {
initCamera(surfaceHolder);
} else {
// 防止sdk8的设备初始化预览异常
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// Install the callback and wait for surfaceCreated() to init the
// camera.
surfaceHolder.addCallback(this);
}
decodeFormats = null;
characterSet = null;
// 加载声音配置,其实在BeemManager的构造器中也会调用该方法,即在onCreate的时候会调用一次
beepManager.updatePrefs();
// 启动闪光灯调节器
ambientLightManager.start(cameraManager);
// 恢复活动监控器
inactivityTimer.onResume();
}
@Override
protected void onPause() {
Log.d(TAG, "onPause");
if (handler != null) {
handler.quitSynchronously();
handler = null;
}
inactivityTimer.onPause();
ambientLightManager.stop();
beepManager.close();
// 关闭摄像头
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() {
inactivityTimer.shutdown();
super.onDestroy();
}
/**
* A valid barcode has been found, so give an indication of success and show
* the results.
*
* @param rawResult
* The contents of the barcode.
* @param scaleFactor
* amount by which thumbnail was scaled
* @param barcode
* A greyscale bitmap of the camera data which was decoded.
*/
public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
Log.d(TAG, "handleDecode");
inactivityTimer.onActivity();
beepManager.playBeepSoundAndVibrate();
String resultString = rawResult.getText();
if (TextUtils.isEmpty(resultString)) {
Toast.makeText(CaptureCodeActivity.this, "Scan failed!",
Toast.LENGTH_SHORT).show();
} else {
//扫码完成,处理结果(跳转到箱子列表)
/*Intent resultIntent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("result", resultString);
bundle.putParcelable("bitmap", barcode);
resultIntent.putExtras(bundle);
this.setResult(RESULT_OK, resultIntent);*/
String terminalNo = getIntent().getStringExtra("terminal_no");
if (resultString.equals(terminalNo)) {
AppToast.makeToast(this, "确认成功!");
Intent intent = new Intent(this, DeliveryBoxListActivity.class);
intent.putExtra("terminal_no", terminalNo);
intent.putExtra("box_list", (Serializable) getIntent().getSerializableExtra("box_list"));
startActivity(intent);
CaptureCodeActivity.this.finish();
} else {
/*AppToast.makeToast(this, "确认失败,请重新扫描确认");*/
showErrorDialog();
}
}
}
private void initCamera(SurfaceHolder surfaceHolder) {
if (surfaceHolder == null) {
throw new IllegalStateException("No SurfaceHolder provided");
}
if (cameraManager.isOpen()) {
Log.w(TAG,
"initCamera() while already open -- late SurfaceView callback?");
return;
}
try {
cameraManager.openDriver(surfaceHolder);
// Creating the handler starts the preview, which can also throw a
// RuntimeException.
if (handler == null) {
handler = new CaptureActivityHandler(this, decodeFormats,
decodeHints, characterSet, cameraManager);
}
} catch (IOException ioe) {
Log.w(TAG, ioe);
displayFrameworkBugMessageAndExit();
} catch (RuntimeException e) {
// Barcode Scanner has seen crashes in the wild of this variety:
// java.?lang.?RuntimeException: Fail to connect to camera service
Log.w(TAG, "Unexpected error initializing camera", e);
displayFrameworkBugMessageAndExit();
}
}
private void displayFrameworkBugMessageAndExit() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.app_name));
builder.setMessage(getString(R.string.msg_camera_framework_bug));
builder.setPositiveButton(android.R.string.yes, new FinishListener(this));
builder.setOnCancelListener(new FinishListener(this));
builder.show();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (holder == null) {
Log.e(TAG,
"*** WARNING *** surfaceCreated() gave us a null surface!");
}
if (!hasSurface) {
hasSurface = true;
initCamera(holder);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
hasSurface = false;
}
public ViewfinderView getViewfinderView() {
return viewfinderView;
}
public Handler getHandler() {
return handler;
}
public void drawViewfinder() {
viewfinderView.drawViewfinder();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.capture_flashlight:
if (isFlashlightOpen) {
cameraManager.setTorch(false); // 关闭闪光灯
isFlashlightOpen = false;
}
else {
cameraManager.setTorch(true); // 打开闪光灯
isFlashlightOpen = true;
}
break;
default:
break;
}
}
private void showErrorDialog() {
closeCamera();
viewfinderView.setVisibility(View.GONE);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.app_name));
builder.setMessage("扫描确认出错,请重新扫描二维码!");
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
restartCamera();
}
});
builder.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
CaptureCodeActivity.this.finish();
}
});
builder.show();
}
void restartCamera(){
Log.d(TAG, "hasSurface " + hasSurface);
viewfinderView.setVisibility(View.VISIBLE);
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
initCamera(surfaceHolder);
// 恢复活动监控器
inactivityTimer.onResume();
}
void closeCamera(){
if (handler != null) {
handler.quitSynchronously();
handler = null;
}
inactivityTimer.onPause();
// 关闭摄像头
cameraManager.closeDriver();
}
}
在扫描出现问题事弹出dialog,当用户点击确定按钮后重新开始扫描。
private void showErrorDialog() {
closeCamera();
viewfinderView.setVisibility(View.GONE);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.app_name));
builder.setMessage("扫描确认出错,请重新扫描柜子上的二维码!");
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
restartCamera();
}
});
builder.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
CaptureCodeActivity.this.finish();
}
});
builder.show();
}
展开全部
弄两个二维码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在扫描出现问题事弹出dialog,当用户点击确定按钮后重新开始扫描。
private void showErrorDialog() {
closeCamera();
viewfinderView.setVisibility(View.GONE);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.app_name));
builder.setMessage("扫描确认出错,请重新扫描柜子上的二维码!");
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
restartCamera();
}
});
builder.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
CaptureCodeActivity.this.finish();
}
});
builder.show();
}
private void showErrorDialog() {
closeCamera();
viewfinderView.setVisibility(View.GONE);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.app_name));
builder.setMessage("扫描确认出错,请重新扫描柜子上的二维码!");
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
restartCamera();
}
});
builder.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
CaptureCodeActivity.this.finish();
}
});
builder.show();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询