package com.google.zxing.activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import com.dommy.qrcode.R;
import com.dommy.qrcode.util.Constant;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.camera.CameraManager;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.decoding.CaptureActivityHandler;
import com.google.zxing.decoding.InactivityTimer;
import com.google.zxing.decoding.RGBLuminanceSource;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.view.ViewfinderView;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;
/**
* Initial the camera
*
* @author Ryan.Tang
*/
public class CaptureActivity extends AppCompatActivity implements Callback {
private static final int REQUEST_CODE_SCAN_GALLERY = 100;
private CaptureActivityHandler handler;
private ViewfinderView viewfinderView;
private ImageButton back;
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;
private ProgressDialog mProgress;
private String photo_path;
private Bitmap scanBitmap;
// private Button cancelScanButton;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
//ViewUtil.addTopView(getApplicationContext(), this, R.string.scan_card);
CameraManager.init(getApplication());
viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_content);
back = (ImageButton) findViewById(R.id.btn_back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
// cancelScanButton = (Button) this.findViewById(R.id.btn_cancel_scan);
hasSurface = false;
inactivityTimer = new InactivityTimer(this);
//添加toolbar
// addToolbar();
}
private void addToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// ImageView more = (ImageView) findViewById(R.id.scanner_toolbar_more);
// assert more != null;
// more.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
//
// }
// });
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.scanner_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// switch (item.getItemId()){
// case R.id.scan_local:
// //打开手机中的相册
// Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); //"android.intent.action.GET_CONTENT"
// innerIntent.setType("image/*");
// Intent wrapperIntent = Intent.createChooser(innerIntent, "选择二维码图片");
// this.startActivityForResult(wrapperIntent, REQUEST_CODE_SCAN_GALLERY);
// return true;
// }
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(final int requestCode, int resultCode, Intent data) {
if (requestCode==RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE_SCAN_GALLERY:
//获取选中图片的路径
Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
if (cursor.moveToFirst()) {
photo_path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
mProgress = new ProgressDialog(CaptureActivity.this);
mProgress.setMessage("正在扫描...");
mProgress.setCancelable(false);
mProgress.show();
new Thread(new Runnable() {
@Override
public void run() {
Result result = scanningImage(photo_path);
if (result != null) {
// Message m = handler.obtainMessage();
// m.what = R.id.decode_succeeded;
// m.obj = result.getText();
// handler.sendMessage(m);
Intent resultIntent = new Intent();
Bundle bundle = new Bundle();
bundle.putString(Constant.INTENT_EXTRA_KEY_QR_SCAN ,result.getText());
// Logger.d("saomiao",result.getText());
// bundle.putParcelable("bitmap",result.get);
resultIntent.putExtras(bundle);
CaptureActivity.this.setResult(RESULT_OK, resultIntent);
} else {
Message m = handler.obtainMessage();
m.what = R.id.decode_failed;
m.obj = "Scan failed!";
handler.sendMessage(m);
}
}
}).start();
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 扫描二维码图片的方法
* @param path
* @return
*/
public Result scanningImage(String path) {
if(TextUtils.isEmpty(path)){
return null;
}
Hashtable<DecodeHintType, String> hints = new Hashtable<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); //设置二维码内容的编码
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 先获取原大小
scanBitmap = BitmapFactory.decodeFile(path, options);
options.inJustDecodeBounds = false; // 获取新的大小
int sampleSize = (int) (options.outHeight / (float) 200);
if (sampleSize <= 0)
sampleSize = 1;
options.inSampleSize = sampleSize;
scanBitmap = BitmapFactory.decodeFile(path, options);
RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
try {
retur