基本信息
源码名称:二维码扫描
源码大小:1.27M
文件格式:.rar
开发语言:Java
更新时间:2015-12-29
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
使用zbar库编译生成.so文件作扫描功能


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