LibZxing包导入后的实现方法

网上找到的demo没了出处。感谢这位代码标注sdj的作者~~

 

在需要打开摄像头扫描的页面Intent跳转至ScanResultActivity即可。



import com.google.zxing.client.android.CaptureActivity2;


/**
 * Created by sdj on 2017/11/24.
 */

public class ScanResultActivity extends AppCompatActivity {
    private static final int SCAN_REQUEST_CODE = 100;
    private static final int CAMERA_PERMISSION = 110;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_result);
        findViewById(R.id.scanTv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT > 22) {
                    if (ContextCompat.checkSelfPermission(ScanResultActivity.this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(ScanResultActivity.this, new String[]{android.Manifest.permission.CAMERA}, CAMERA_PERMISSION);
                    } else {
                        startScanActivity();
                    }
                } else {
                    startScanActivity();
                }
            }
        });
    }

    private void startScanActivity() {
        Intent intent = new Intent(ScanResultActivity.this, CaptureActivity2.class);
        intent.putExtra(CaptureActivity2.USE_DEFUALT_ISBN_ACTIVITY, true);
        startActivityForResult(intent, SCAN_REQUEST_CODE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case CAMERA_PERMISSION:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    startScanActivity();
                } else {
                    Toast.makeText(ScanResultActivity.this, "请手动打开摄像头权限", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public int checkSelfPermission(String permission) {
        return super.checkSelfPermission(permission);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SCAN_REQUEST_CODE) {
                //Todo Handle the isbn number entered manually
                String isbn = data.getStringExtra("CaptureIsbn");
                if (!TextUtils.isEmpty(isbn)) {
                    //todo something
                    Toast.makeText(this, "解析到的内容为" + isbn, Toast.LENGTH_LONG).show();
                    if (isbn.contains("http")) {
                        Intent intent = new Intent(this, WebViewActivity.class);
                        intent.putExtra(WebViewActivity.RESULT, isbn);
                        startActivity(intent);
                    }
                }
            }
        }
    }
}

其中若二维码解析出http页面,跳转至webViewActivity,这里是一个webView承载。


public class WebViewActivity extends AppCompatActivity {
    private WebView webView;
    public static final String RESULT = "result_data";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        initViews();
    }
    private void initViews(){
        webView = (WebView)findViewById(R.id.webView);
        Intent intent = getIntent();
        String result = intent.getStringExtra(RESULT);
        webView.loadUrl(result);
    }
}

有一个point是Manifest中关于CaptureActivity2的注册(刚才两个自己新建的activity正常注册即可)


        
            
                

                
            
            
            
                

                
                

                
            
            
            
                

                
                

                
            
            
            
                

                
                

                
            
        

 

附上 两个布局:

activity_scan_result:




    

activity_webview:




    

 

你可能感兴趣的:(Android)