首页>代码>Android客户端高仿开心网ui实例源码>/KaiXin/src/com/kaixin/android/activity/CaptureActivity.java
package com.kaixin.android.activity;

import java.io.IOException;
import java.util.Vector;

import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.MediaPlayer;
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.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.kaixin.android.KXActivity;
import com.kaixin.android.R;
import com.kaixin.android.qrcode.CameraManager;
import com.kaixin.android.qrcode.CaptureActivityHandler;
import com.kaixin.android.qrcode.InactivityTimer;
import com.kaixin.android.ui.base.ViewfinderView;
import com.kaixin.android.utils.Utils;

/**
 * 扫描二维码类
 * 
 * @author rendongwei
 * 
 */
public class CaptureActivity extends KXActivity implements Callback {
	private FrameLayout mScanningLayout;
	private RelativeLayout mInformationLayout;
	private ViewfinderView viewfinderView;
	private ImageView mImageView;
	private TextView mTime;
	private TextView mContent;
	private Button mAgain;

	private CaptureActivityHandler handler;
	private boolean hasSurface;
	private Vector<BarcodeFormat> decodeFormats;
	private String characterSet;
	private InactivityTimer inactivityTimer;
	private MediaPlayer mediaPlayer;
	private boolean playBeep;
	private static final float BEEP_VOLUME = 0.10f;
	private boolean vibrate;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capture_activity);
		// 初始化 CameraManager
		CameraManager.init(getApplication());
		mScanningLayout = (FrameLayout) findViewById(R.id.capture_scanning_layout);
		mInformationLayout = (RelativeLayout) findViewById(R.id.capture_information_layout);
		viewfinderView = (ViewfinderView) findViewById(R.id.capture_viewfinder_view);
		mImageView = (ImageView) findViewById(R.id.capture_image);
		mTime = (TextView) findViewById(R.id.capture_time);
		mContent = (TextView) findViewById(R.id.capture_content);
		mAgain = (Button) findViewById(R.id.capture_again);
		mAgain.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				stop();
				start();
			}
		});
		hasSurface = false;
		inactivityTimer = new InactivityTimer(this);
	}

	protected void onResume() {
		super.onResume();
		start();
	}

	@Override
	protected void onPause() {
		super.onPause();
		stop();
	}

	@Override
	protected void onDestroy() {
		inactivityTimer.shutdown();
		super.onDestroy();
	}

	private void start() {
		mScanningLayout.setVisibility(View.VISIBLE);
		mInformationLayout.setVisibility(View.GONE);
		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);
		}
		decodeFormats = null;
		characterSet = null;

		playBeep = true;
		AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE);
		if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
			playBeep = false;
		}
		initBeepSound();
		vibrate = true;
	}

	private void stop() {
		if (handler != null) {
			handler.quitSynchronously();
			handler = null;
		}
		CameraManager.get().closeDriver();
	}

	private void initCamera(SurfaceHolder surfaceHolder) {
		try {
			CameraManager.get().openDriver(surfaceHolder);
		} catch (IOException ioe) {
			return;
		} catch (RuntimeException e) {
			return;
		}
		if (handler == null) {
			handler = new CaptureActivityHandler(this, decodeFormats,
					characterSet);
		}
	}

	@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 ViewfinderView getViewfinderView() {
		return viewfinderView;
	}

	public Handler getHandler() {
		return handler;
	}

	public void drawViewfinder() {
		viewfinderView.drawViewfinder();

	}

	public void handleDecode(Result obj, Bitmap barcode) {
		mScanningLayout.setVisibility(View.GONE);
		mInformationLayout.setVisibility(View.VISIBLE);
		inactivityTimer.onActivity();
		playBeepSoundAndVibrate();
		mTime.setText(Utils.formatDate(this, obj.getTimestamp()));
		mContent.setText(obj.getText());
		mImageView.setImageBitmap(barcode);
		Toast.makeText(CaptureActivity.this, "请扫描开心网提供的二维码,以获取正确的地理位置信息",
				Toast.LENGTH_SHORT).show();
	}

	private void initBeepSound() {
		if (playBeep && mediaPlayer == null) {
			// The volume on STREAM_SYSTEM is not adjustable, and users found it
			// too loud,
			// so we now play on the music stream.
			setVolumeControlStream(AudioManager.STREAM_MUSIC);
			mediaPlayer = new MediaPlayer();
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

			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);
		}

	};
}
最近下载更多
skipple3  LV39 2022年9月8日
xkyssb  LV12 2021年1月12日
1294862613  LV11 2020年12月29日
歌颂者  LV5 2020年12月17日
ddman9009  LV25 2020年9月7日
cpemy_abc  LV10 2020年6月9日
sianhxyeiangu  LV14 2020年6月3日
axuhongbo1  LV4 2020年5月27日
rain112  LV31 2020年4月1日
戎马书生  LV15 2019年12月26日
最近浏览更多
anjuw521  LV1 7月10日
2636804923  LV6 6月19日
AIWU1234  LV2 6月7日
求学的熊猫  LV11 5月20日
pangzhihui  LV14 4月25日
wxb666  LV3 3月13日
学习112  LV2 2023年12月21日
陈小小222 2023年7月15日
暂无贡献等级
yyyyyyzh  LV8 2023年6月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友