com.dongbang.yutian.activity.CommonScanActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.dongbang.yutian.activity.CommonScanActivity.java

Source

/*
 * Copyright (C) 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dongbang.yutian.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dongbang.yutian.R;
import com.dongbang.yutian.beans.ScanResult;
import com.dongbang.yutian.common.AppConfig;
import com.dongbang.yutian.scancodeneed.Constant;
import com.dongbang.yutian.scancodeneed.ScanListener;
import com.dongbang.yutian.scancodeneed.ScanManager;
import com.dongbang.yutian.scancodeneed.decode.DecodeThread;
import com.dongbang.yutian.scancodeneed.decode.Utils;
import com.google.zxing.Result;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ???
 *
 * @author ruiqin
 */
public final class CommonScanActivity extends BaseActivity implements ScanListener, View.OnClickListener {
    static final String TAG = CommonScanActivity.class.getSimpleName();
    public final static int RESULT_CODE = 1;
    SurfaceView scanPreview = null;
    View scanContainer;
    View scanCropView;
    ImageView scanLine;
    ScanManager scanManager;
    TextView iv_light;
    TextView qrcode_g_gallery;
    TextView qrcode_ic_back;
    private String result;
    final int PHOTOREQUESTCODE = 1111;

    @Bind(R.id.service_register_rescan)
    Button rescan;
    @Bind(R.id.scan_image)
    ImageView scan_image;
    @Bind(R.id.authorize_return)
    ImageView authorize_return;
    private int scanMode;//????

    @Bind(R.id.scan_hint)
    TextView scan_hint;
    @Bind(R.id.tv_scan_result)
    TextView tv_scan_result;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_scan_code);
        ButterKnife.bind(this);
        scanMode = getIntent().getIntExtra(Constant.REQUEST_SCAN_MODE, Constant.REQUEST_SCAN_MODE_ALL_MODE);
        initView();
    }

    void initView() {
        switch (scanMode) {
        case DecodeThread.BARCODE_MODE:
            scan_hint.setText(R.string.scan_barcode_hint);
            break;
        case DecodeThread.QRCODE_MODE:
            scan_hint.setText(R.string.scan_qrcode_hint);
            break;
        case DecodeThread.ALL_MODE:
            scan_hint.setText(R.string.scan_allcode_hint);
            break;
        }
        scanPreview = (SurfaceView) findViewById(R.id.capture_preview);
        scanContainer = findViewById(R.id.capture_container);
        scanCropView = findViewById(R.id.capture_crop_view);
        scanLine = (ImageView) findViewById(R.id.capture_scan_line);
        qrcode_g_gallery = (TextView) findViewById(R.id.qrcode_g_gallery);
        qrcode_g_gallery.setOnClickListener(this);
        qrcode_ic_back = (TextView) findViewById(R.id.qrcode_ic_back);
        qrcode_ic_back.setOnClickListener(this);
        iv_light = (TextView) findViewById(R.id.iv_light);
        iv_light.setOnClickListener(this);
        rescan.setOnClickListener(this);
        authorize_return.setOnClickListener(this);
        //???
        scanManager = new ScanManager(this, scanPreview, scanContainer, scanCropView, scanLine, scanMode, this);
    }

    @Override
    public void onResume() {
        super.onResume();
        scanManager.onResume();
        rescan.setVisibility(View.INVISIBLE);
        scan_image.setVisibility(View.GONE);
    }

    @Override
    public void onPause() {
        super.onPause();
        scanManager.onPause();
    }

    /**
     *
     */
    public void scanResult(Result rawResult, Bundle bundle) {
        //????????????reScan()
        //scanManager.reScan();
        //      Toast.makeText(that, "result="+rawResult.getText(), Toast.LENGTH_LONG).show();

        if (!scanManager.isScanning()) { //?????
            //???
            rescan.setVisibility(View.VISIBLE);
            scan_image.setVisibility(View.VISIBLE);
            Bitmap barcode = null;
            byte[] compressedBitmap = bundle.getByteArray(DecodeThread.BARCODE_BITMAP);
            if (compressedBitmap != null) {
                barcode = BitmapFactory.decodeByteArray(compressedBitmap, 0, compressedBitmap.length, null);
                barcode = barcode.copy(Bitmap.Config.ARGB_8888, true);
            }
            scan_image.setImageBitmap(barcode);
        }
        rescan.setVisibility(View.VISIBLE);
        scan_image.setVisibility(View.VISIBLE);
        tv_scan_result.setVisibility(View.VISIBLE);
        tv_scan_result.setText("" + rawResult.getText());
        //        Toast.makeText(this,""+rawResult.getText(),Toast.LENGTH_SHORT).show();
        //      result=rawResult.getText();
        ScanResult.setResult(rawResult.getText());
        finish();
        Intent it = new Intent(this, ProductInfoActivity.class);
        startActivity(it);
    }

    void startScan() {
        if (rescan.getVisibility() == View.VISIBLE) {
            rescan.setVisibility(View.INVISIBLE);
            scan_image.setVisibility(View.GONE);
            scanManager.reScan();
        }
    }

    @Override
    public void scanError(Exception e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        //??
        if (e.getMessage() != null && e.getMessage().startsWith("")) {
            scanPreview.setVisibility(View.INVISIBLE);
        }
    }

    public void showPictures(int requestCode) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String photo_path;
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case PHOTOREQUESTCODE:
                String[] proj = { MediaStore.Images.Media.DATA };
                Cursor cursor = this.getContentResolver().query(data.getData(), proj, null, null, null);
                if (cursor.moveToFirst()) {
                    int colum_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    photo_path = cursor.getString(colum_index);
                    if (photo_path == null) {
                        photo_path = Utils.getPath(getApplicationContext(), data.getData());
                    }
                    scanManager.scanningImage(photo_path);
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        Log.i("qin:", v.toString());
        switch (v.getId()) {

        case R.id.qrcode_g_gallery:
            showPictures(PHOTOREQUESTCODE);
            break;
        case R.id.iv_light:
            scanManager.switchLight();
            break;
        case R.id.qrcode_ic_back:
            finish();
            break;
        case R.id.service_register_rescan://????
            startScan();
            break;
        case R.id.authorize_return:
            finish();
            break;
        }

    }
}