导航菜单

页面标题

页面副标题

爱心e站 v1.0.0 - CaptureActivity.java 源代码

正在查看: 爱心e站 v1.0.0 应用的 CaptureActivity.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.newheyd.JZKFcanjiren.qrcode;

import android.app.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.graphics.Matrix;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
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.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.iflytek.cloud.SpeechUtility;
import com.newheyd.JZKFcanjiren.R;
import com.newheyd.JZKFcanjiren.qrcode.camera.CameraManager;
import com.newheyd.JZKFcanjiren.qrcode.decoding.CaptureActivityHandler;
import com.newheyd.JZKFcanjiren.qrcode.decoding.InactivityTimer;
import com.newheyd.JZKFcanjiren.qrcode.decoding.RGBLuminanceSource;
import com.newheyd.JZKFcanjiren.qrcode.view.ViewfinderView;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;

public class CaptureActivity extends Activity implements SurfaceHolder.Callback {
    private static final float BEEP_VOLUME = 0.1f;
    private static final long VIBRATE_DURATION = 200;
    private ImageButton cancelScanButton;
    private String characterSet;
    private Vector<BarcodeFormat> decodeFormats;
    private CaptureActivityHandler handler;
    private boolean hasSurface;
    private InactivityTimer inactivityTimer;
    ProgressDialog mProgress;
    private MediaPlayer mediaPlayer;
    String photo_path;
    private boolean playBeep;
    Bitmap scanBitmap;
    private boolean vibrate;
    private ViewfinderView viewfinderView;
    int ifOpenLight = 0;
    final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    CaptureActivity.this.mProgress.dismiss();
                    String resultString = msg.obj.toString();
                    if (resultString.equals("")) {
                        Toast.makeText(CaptureActivity.this, "扫描失败!", 0).show();
                    } else {
                        Intent resultIntent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putString(SpeechUtility.TAG_RESOURCE_RESULT, resultString);
                        bundle.putParcelable("bitmap", CaptureActivity.this.scanBitmap);
                        resultIntent.putExtras(bundle);
                        CaptureActivity.this.setResult(-1, resultIntent);
                    }
                    CaptureActivity.this.finish();
                    break;
                case 2:
                    CaptureActivity.this.mProgress.dismiss();
                    Toast.makeText(CaptureActivity.this, "解析错误!", 1).show();
                    break;
            }
            super.handleMessage(msg);
        }
    };
    private final MediaPlayer.OnCompletionListener beepListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            mediaPlayer.seekTo(0);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        CameraManager.init(getApplication());
        this.viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
        this.cancelScanButton = (ImageButton) findViewById(R.id.btn_cancel_scan);
        this.hasSurface = false;
        this.inactivityTimer = new InactivityTimer(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
        SurfaceHolder surfaceHolder = surfaceView.getHolder();
        if (this.hasSurface) {
            initCamera(surfaceHolder);
        } else {
            surfaceHolder.addCallback(this);
            surfaceHolder.setType(3);
        }
        this.decodeFormats = null;
        this.characterSet = null;
        this.playBeep = true;
        AudioManager audioService = (AudioManager) getSystemService("audio");
        if (audioService.getRingerMode() != 2) {
            this.playBeep = false;
        }
        initBeepSound();
        this.vibrate = true;
        this.cancelScanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CaptureActivity.this.finish();
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (this.handler != null) {
            this.handler.quitSynchronously();
            this.handler = null;
        }
        CameraManager.get().closeDriver();
    }

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

    public void handleDecode(Result result, Bitmap barcode) {
        this.inactivityTimer.onActivity();
        playBeepSoundAndVibrate();
        String resultString = result.getText();
        if (resultString.equals("")) {
            Toast.makeText(this, "扫描失败!", 0).show();
        } else {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            Matrix matrix = new Matrix();
            matrix.postScale(0.5f, 0.5f);
            Bitmap bit = Bitmap.createBitmap(barcode, 0, 0, barcode.getWidth(), barcode.getHeight(), matrix, true);
            bundle.putString(SpeechUtility.TAG_RESOURCE_RESULT, resultString);
            bundle.putParcelable("bitmap", bit);
            resultIntent.putExtras(bundle);
            setResult(-1, resultIntent);
        }
        finish();
    }

    public void pickPictureFromAblum(View v) {
        Intent innerIntent = new Intent("android.intent.action.GET_CONTENT");
        innerIntent.setType("image/*");
        Intent wrapperIntent = Intent.createChooser(innerIntent, "选择二维码图片");
        startActivityForResult(wrapperIntent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == -1) {
            switch (requestCode) {
                case 1:
                    try {
                        Uri uri = data.getData();
                        if (!TextUtils.isEmpty(uri.getAuthority())) {
                            Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{"_data"}, null, null, null);
                            if (cursor == null) {
                                Toast.makeText(this, "图片没找到", 0).show();
                                return;
                            } else {
                                cursor.moveToFirst();
                                this.photo_path = cursor.getString(cursor.getColumnIndex("_data"));
                                cursor.close();
                            }
                        } else {
                            this.photo_path = data.getData().getPath();
                        }
                        this.mProgress = new ProgressDialog(this);
                        this.mProgress.setMessage("正在扫描...");
                        this.mProgress.setCancelable(false);
                        this.mProgress.show();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Result result = CaptureActivity.this.scanningImage(CaptureActivity.this.photo_path);
                                if (result != null) {
                                    Message m = CaptureActivity.this.mHandler.obtainMessage();
                                    m.what = 1;
                                    m.obj = result.getText();
                                    CaptureActivity.this.mHandler.sendMessage(m);
                                    return;
                                }
                                Message m2 = CaptureActivity.this.mHandler.obtainMessage();
                                m2.what = 2;
                                m2.obj = "Scan failed!";
                                CaptureActivity.this.mHandler.sendMessage(m2);
                            }
                        }).start();
                        break;
                    } catch (Exception e) {
                        Toast.makeText(this, "解析错误!", 1).show();
                        break;
                    }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public Result scanningImage(String path) {
        if (TextUtils.isEmpty(path)) {
            return null;
        }
        Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
        hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
        hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        this.scanBitmap = BitmapFactory.decodeFile(path, options);
        options.inJustDecodeBounds = false;
        int sampleSize = (int) (options.outHeight / 100.0f);
        if (sampleSize <= 0) {
            sampleSize = 1;
        }
        options.inSampleSize = sampleSize;
        this.scanBitmap = BitmapFactory.decodeFile(path, options);
        RGBLuminanceSource source = new RGBLuminanceSource(this.scanBitmap);
        BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
        QRCodeReader reader = new QRCodeReader();
        try {
            return reader.decode(bitmap1, hints);
        } catch (ChecksumException e) {
            e.printStackTrace();
            return null;
        } catch (FormatException e2) {
            e2.printStackTrace();
            return null;
        } catch (NotFoundException e3) {
            e3.printStackTrace();
            return null;
        }
    }

    public void IfOpenLight(View v) {
        this.ifOpenLight++;
        switch (this.ifOpenLight % 2) {
            case 0:
                CameraManager.get().closeLight();
                break;
            case 1:
                CameraManager.get().openLight();
                break;
        }
    }

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

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (!this.hasSurface) {
            this.hasSurface = true;
            initCamera(holder);
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        this.hasSurface = false;
    }

    public ViewfinderView getViewfinderView() {
        return this.viewfinderView;
    }

    public Handler getHandler() {
        return this.handler;
    }

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

    private void initBeepSound() {
        if (this.playBeep && this.mediaPlayer == null) {
            setVolumeControlStream(3);
            this.mediaPlayer = new MediaPlayer();
            this.mediaPlayer.setAudioStreamType(3);
            this.mediaPlayer.setOnCompletionListener(this.beepListener);
            AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep);
            try {
                this.mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
                file.close();
                this.mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
                this.mediaPlayer.prepare();
            } catch (IOException e) {
                this.mediaPlayer = null;
            }
        }
    }

    private void playBeepSoundAndVibrate() {
        if (this.playBeep && this.mediaPlayer != null) {
            this.mediaPlayer.start();
        }
        if (this.vibrate) {
            Vibrator vibrator = (Vibrator) getSystemService("vibrator");
            vibrator.vibrate(VIBRATE_DURATION);
        }
    }
}