实例介绍
【实例截图】
【核心代码】
package com.zbar.lib;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.Point;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.zbar.lib.camera.CameraManager;
import com.zbar.lib.decode.CaptureActivityHandler;
import com.zbar.lib.decode.InactivityTimer;
/**
* 描述: 扫描界面
* CaptureActivity.java是拍照取景的类,
* camera包下面的类主要与照相机相关的类
*/
public class CaptureActivity extends Activity implements Callback {
private CaptureActivityHandler handler;
private boolean hasSurface;
private InactivityTimer inactivityTimer;
private MediaPlayer mediaPlayer;
private boolean playBeep;
private static final float BEEP_VOLUME = 0.50f;
private boolean vibrate;
private int x = 0;
private int y = 0;
private int cropWidth = 0;
private int cropHeight = 0;
private RelativeLayout mContainer = null;
private RelativeLayout mCropLayout = null;
private boolean isNeedCapture = false;
public boolean isNeedCapture() {
return isNeedCapture;
}
public void setNeedCapture(boolean isNeedCapture) {
this.isNeedCapture = isNeedCapture;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getCropWidth() {
return cropWidth;
}
public void setCropWidth(int cropWidth) {
this.cropWidth = cropWidth;
}
public int getCropHeight() {
return cropHeight;
}
public void setCropHeight(int cropHeight) {
this.cropHeight = cropHeight;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr_scan);
// 初始化 CameraManager
CameraManager.init(getApplication());
hasSurface = false;
inactivityTimer = new InactivityTimer(this);
mContainer = (RelativeLayout) findViewById(R.id.capture_containter);
mCropLayout = (RelativeLayout) findViewById(R.id.capture_crop_layout);
ImageView mQrLineView = (ImageView) findViewById(R.id.capture_scan_line);
TranslateAnimation mAnimation = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0f, TranslateAnimation.RELATIVE_TO_PARENT, 0.9f);
mAnimation.setDuration(1500);
mAnimation.setRepeatCount(-1);
mAnimation.setRepeatMode(Animation.REVERSE);
mAnimation.setInterpolator(new LinearInterpolator());
mQrLineView.setAnimation(mAnimation);
}
boolean flag = true;
protected void light() {
if (flag == true) {
flag = false;
// 开闪光灯
CameraManager.get().openLight();
} else {
flag = true;
// 关闪光灯
CameraManager.get().offLight();
}
}
@SuppressWarnings("deprecation")
@Override
protected void onResume() {
super.onResume();
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.capture_preview);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
if (hasSurface) {
initCamera(surfaceHolder);
} else {
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
playBeep = true;
AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE);
if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
playBeep = false;
}
initBeepSound();
vibrate = true;
}
@Override
protected void onPause() {
super.onPause();
if (handler != null) {
handler.quitSynchronously();
handler = null;
}
CameraManager.get().closeDriver();
/*public void closeDriver(){
if(camera!=null){
FlashlightManager.disabaleFlashlight();
camera.release();
camera=null;
}
}
*/
}
@Override
protected void onDestroy() {
inactivityTimer.shutdown();
super.onDestroy();
}
/*public void handleDecode(String result) {
inactivityTimer.onActivity();
playBeepSoundAndVibrate();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
// 连续扫描,不发送此消息扫描一次结束后就不能再次扫描
// handler.sendEmptyMessage(R.id.restart_preview);
}
*/
//从CaptureActivityHandler.java解码获取数据返回到这里
public void handleDecode(String result) {
inactivityTimer.onActivity();
playBeepSoundAndVibrate();
String resultString = result;
if (resultString.equals("")) {
Toast.makeText(CaptureActivity.this, "Scan failed!", Toast.LENGTH_SHORT).show();
}else {
// System.out.println("Result:" resultString);
/*Intent result = new Intent(CaptureActivity.this,你的Activity.class);
result.putExtra("result", obj.getText());
setResult(RESULT_OK,result);
finish();
*/
Intent resultIntent = new Intent(CaptureActivity.this,Main.class);
Bundle bundle = new Bundle();
bundle.putString("result", resultString);
resultIntent.putExtras(bundle);
CaptureActivity.this.setResult(RESULT_OK, resultIntent);
}
CaptureActivity.this.finish();
}
private void initCamera(SurfaceHolder surfaceHolder) {
try {
CameraManager.get().openDriver(surfaceHolder);
Point point = CameraManager.get().getCameraResolution();
int width = point.y;
int height = point.x;
int x = mCropLayout.getLeft() * width / mContainer.getWidth();
int y = mCropLayout.getTop() * height / mContainer.getHeight();
int cropWidth = mCropLayout.getWidth() * width / mContainer.getWidth();
int cropHeight = mCropLayout.getHeight() * height / mContainer.getHeight();
setX(x);
setY(y);
setCropWidth(cropWidth);
setCropHeight(cropHeight);
// 设置是否需要截图
setNeedCapture(true);
} catch (IOException ioe) {
return;
} catch (RuntimeException e) {
return;
}
if (handler == null) {
handler = new CaptureActivityHandler(CaptureActivity.this);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (!hasSurface) {
hasSurface = true;
initCamera(holder);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
hasSurface = false;
}
public Handler getHandler() {
return handler;
}
private void initBeepSound() {
if (playBeep && mediaPlayer == null) {
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(beepListener);
AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
file.close();
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
} catch (IOException e) {
mediaPlayer = null;
}
}
}
private static final long VIBRATE_DURATION = 200L;
private void playBeepSoundAndVibrate() {
if (playBeep && mediaPlayer != null) {
mediaPlayer.start();
}
if (vibrate) {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(VIBRATE_DURATION);
}
}
private final OnCompletionListener beepListener = new OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.seekTo(0);
}
};
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论