二维码扫描扫描到网址跳转到wenView中

来源:互联网 发布:怎么样设置网络打印机 编辑:程序博客网 时间:2024/06/06 19:21

上代码:

compile 'com.journeyapps:zxing-android-embedded:3.3.0'

1 设置一个按钮跳转界面:

调用:

public void customScan(){    new IntentIntegrator(this)            .setOrientationLocked(false)            .setCaptureActivity(CustomScanActivity.class) // 设置自定义的activity是CustomActivity            .initiateScan(); // 初始化扫描}

用OnActivityResult接收

    @Override// 通过 onActivityResult的方法获取 扫描回来的 值    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);        if(intentResult != null) {            if(intentResult.getContents() == null) {                Toast.makeText(this,"内容为空",Toast.LENGTH_LONG).show();            } else {                String ScanResult = intentResult.getContents();                getResult(ScanResult);            }        } else {            super.onActivityResult(requestCode,resultCode,data);        }    }

对结果进行判断:

/** * 对结果进行判断,如果是http开头的,就跳转到webview * @param result * || str1 == "www" || str1 == "WWW" */private String str;private String str1;public void getResult(String result){    //0 到 4 ,截取的时候是包括左边(0),不包括右边(4),所以是0,1,2,3 前四个。    String str = result.substring(0,4);    String str1 = result.substring(0,3);    if ("http".equals(str) || "WWW".equals(str1) || "www".equals(str1)) {        Intent intent = new Intent(MainActivity.this,WebActivity.class);        intent.putExtra("url",result);        startActivity(intent);    }else if("OMI".equals(str1)){        Toast.makeText(MainActivity.this,"OMI",Toast.LENGTH_SHORT).show();    }else{        Toast.makeText(MainActivity.this,"qita",Toast.LENGTH_SHORT).show();    }    text.setText(result);}

这个界面的布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:background="@color/white"    >    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/btn"        android:id="@+id/btn"        />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="200dp"        android:orientation="horizontal"        android:gravity="center">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/image"            />    </LinearLayout>    <TextView        android:layout_width="match_parent"        android:layout_height="50dp"        android:id="@+id/text"        android:text="@string/jieshou"        android:gravity="center"        /></LinearLayout>


2 第二个界面:

扫描界面:

CustomScanActivity


package com.example.administrator.eitherdemo1;import android.content.pm.PackageManager;import android.os.Bundle;import android.os.PersistableBundle;import android.support.v7.app.AppCompatActivity;import android.view.KeyEvent;import android.view.View;import android.view.Window;import android.widget.Button;import com.journeyapps.barcodescanner.CaptureManager;import com.journeyapps.barcodescanner.DecoratedBarcodeView;/** * Created by Administrator on 2017/4/7 0007. */public class CustomScanActivity extends AppCompatActivity implements DecoratedBarcodeView.TorchListener{ // 实现相关接口    // 添加一个按钮用来控制闪光灯,同时添加两个按钮表示其他功能,先用Toast表示    Button swichLight;    DecoratedBarcodeView mDBV;    private CaptureManager captureManager;    private boolean isLightOn = false;    public CustomScanActivity instance;    public CustomScanActivity getInstance(){        if(instance == null){            instance =  new CustomScanActivity();        }        return instance;    }    @Override    protected void onPause() {        super.onPause();        captureManager.onPause();    }    @Override    protected void onResume() {        super.onResume();        captureManager.onResume();    }    @Override    protected void onDestroy() {        super.onDestroy();        captureManager.onDestroy();        finish();    }    @Override    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {        super.onSaveInstanceState(outState, outPersistentState);        captureManager.onSaveInstanceState(outState);    }    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        return mDBV.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_custom);        initView();        mDBV.setTorchListener(this);        // 如果没有闪光灯功能,就去掉相关按钮        if(!hasFlash()) {            swichLight.setVisibility(View.GONE);        }        //重要代码,初始化捕获        captureManager = new CaptureManager(this,mDBV);        captureManager.initializeFromIntent(getIntent(),savedInstanceState);        captureManager.decode();    }    //初始化控件  Button swichLight,hintlShow,hint2Show;    private void initView() {        mDBV = (DecoratedBarcodeView) findViewById(R.id.dbv_custom);        swichLight = (Button) findViewById(R.id.btn_switch);        swichLight.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                getswichLight();            }        });    }    // torch 手电筒    @Override    public void onTorchOn() {        isLightOn = true;    }    @Override    public void onTorchOff() {        isLightOn = false;    }    private boolean hasFlash() {        return getApplicationContext().getPackageManager()                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);    }    // 点击切换闪光灯    public void getswichLight(){        if(isLightOn){            mDBV.setTorchOff();        }else{            mDBV.setTorchOn();        }    }}

布局时:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    xmlns:app="http://schemas.android.com/apk/res-auto"    >        <Button            android:id="@+id/btn_switch"            android:layout_width="wrap_content"            android:layout_height="50dp"            android:background="@color/touming"            android:textColor="@color/white"            android:text="@string/shanguang"            android:layout_alignParentTop="true"            android:layout_centerHorizontal="true" />    <!-- 扫描窗口 -->    <com.journeyapps.barcodescanner.DecoratedBarcodeView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/dbv_custom"        app:zxing_framing_rect_width="250dp"        app:zxing_framing_rect_height="250dp"        app:zxing_preview_scaling_strategy="fitXY"        app:zxing_use_texture_view="true"        app:zxing_scanner_layout="@layout/barcode_scanner"        android:layout_alignParentBottom="true">    </com.journeyapps.barcodescanner.DecoratedBarcodeView></RelativeLayout>

3 需要用到自定义的扫描界面:

app:zxing_scanner_layout="@layout/barcode_scanner"
需要自己去创建:

<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    >    <com.journeyapps.barcodescanner.BarcodeView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/zxing_barcode_surface"        app:zxing_framing_rect_width="250dp"        app:zxing_framing_rect_height="250dp">    </com.journeyapps.barcodescanner.BarcodeView>    <com.example.administrator.eitherdemo1.CustomViewfinderView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/zxing_viewfinder_view"        app:zxing_possible_result_points="@color/zxing_custom_possible_result_points"        app:zxing_result_view="@color/zxing_custom_result_view"        app:zxing_viewfinder_laser="#FFFFFF"        app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask"/>    <TextView        android:id="@+id/zxing_status_view"        android:layout_width="wrap_content"        android:layout_height="44dp"        android:layout_gravity="bottom|center_horizontal"        android:layout_above="@id/zxing_viewfinder_view"        android:background="@color/zxing_transparent"        android:text="@string/zxing_msg_default_status"        android:textColor="@color/zxing_status_text" /></merge>

对应的

CustomViewfinderView

需要自己定义:

package com.example.administrator.eitherdemo1;/** * Created by Administrator on 2017/4/7 0007. */import android.content.Context;import android.graphics.Canvas;import android.graphics.LinearGradient;import android.graphics.Rect;import android.graphics.Shader;import android.util.AttributeSet;import com.google.zxing.ResultPoint;import com.journeyapps.barcodescanner.ViewfinderView;import java.util.ArrayList;import java.util.List;/** * 自定义zxing二维码扫描界面 * Created by IBM on 2016/10/20. */public class CustomViewfinderView extends ViewfinderView {    public int laserLinePosition=0;    public float[] position=new float[]{0f,0.5f,1f};    public int[] colors=new int[]{0x00ffffff,0xffffffff,0x00ffffff};    public LinearGradient linearGradient ;    public CustomViewfinderView(Context context, AttributeSet attrs) {        super(context, attrs);    }    /**     * 重写draw方法绘制自己的扫描框     * @param canvas     */    @Override    public void onDraw(Canvas canvas) {        refreshSizes();        if (framingRect == null || previewFramingRect == null) {            return;        }        Rect frame = framingRect;        Rect previewFrame = previewFramingRect;        int width = canvas.getWidth();        int height = canvas.getHeight();        //绘制4个角        paint.setColor(0xFFFFFFFF);//定义画笔的颜色        canvas.drawRect(frame.left, frame.top, frame.left+70, frame.top+10, paint);        canvas.drawRect(frame.left, frame.top, frame.left + 10, frame.top + 70, paint);        canvas.drawRect(frame.right-70, frame.top, frame.right, frame.top+10, paint);        canvas.drawRect(frame.right-10, frame.top, frame.right, frame.top+70, paint);        canvas.drawRect(frame.left, frame.bottom-10, frame.left+70, frame.bottom, paint);        canvas.drawRect(frame.left, frame.bottom-70, frame.left+10, frame.bottom, paint);        canvas.drawRect(frame.right-70, frame.bottom-10, frame.right, frame.bottom, paint);        canvas.drawRect(frame.right-10, frame.bottom-70, frame.right, frame.bottom, paint);        // Draw the exterior (i.e. outside the framing rect) darkened        paint.setColor(resultBitmap != null ? resultColor : maskColor);        canvas.drawRect(0, 0, width, frame.top, paint);        canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);        canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);        canvas.drawRect(0, frame.bottom + 1, width, height, paint);        if (resultBitmap != null) {            // Draw the opaque result bitmap over the scanning rectangle            paint.setAlpha(CURRENT_POINT_OPACITY);            canvas.drawBitmap(resultBitmap, null, frame, paint);        } else {            //  paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);            //  scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;            int middle = frame.height() / 2 + frame.top;            laserLinePosition=laserLinePosition+5;            if(laserLinePosition>frame.height())            {                laserLinePosition=0;            }            linearGradient= new LinearGradient(frame.left + 1, frame.top+laserLinePosition , frame.right -1 , frame.top +10+laserLinePosition, colors, position, Shader.TileMode.CLAMP);            // Draw a red "laser scanner" line through the middle to show decoding is active            //  paint.setColor(laserColor);            paint.setShader(linearGradient);            //绘制扫描线            canvas.drawRect(frame.left + 1, frame.top+laserLinePosition , frame.right -1 , frame.top +10+laserLinePosition, paint);            paint.setShader(null);            float scaleX = frame.width() / (float) previewFrame.width();            float scaleY = frame.height() / (float) previewFrame.height();            List<ResultPoint> currentPossible = possibleResultPoints;            List<ResultPoint> currentLast = lastPossibleResultPoints;            int frameLeft = frame.left;            int frameTop = frame.top;            if (currentPossible.isEmpty()) {                lastPossibleResultPoints = null;            } else {                possibleResultPoints = new ArrayList<>(5);                lastPossibleResultPoints = currentPossible;                paint.setAlpha(CURRENT_POINT_OPACITY);                paint.setColor(resultPointColor);                for (ResultPoint point : currentPossible) {                    canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),                            frameTop + (int) (point.getY() * scaleY),                            POINT_SIZE, paint);                }            }            if (currentLast != null) {                paint.setAlpha(CURRENT_POINT_OPACITY / 2);                paint.setColor(resultPointColor);                float radius = POINT_SIZE / 2.0f;                for (ResultPoint point : currentLast) {                    canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),                            frameTop + (int) (point.getY() * scaleY),                            radius, paint);                }            }            postInvalidateDelayed(16,                    frame.left ,                    frame.top ,                    frame.right ,                    frame.bottom);            // postInvalidate();        }    }}

webView 界面就不写了,请注意使用webView一定要加联网权限。




0 0
原创粉丝点击