初写二维码的使用

activity_er_wei_ma

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bwie.test.erweimademo.ErWeiMaActivity">
    <Button
        android:text="默认页面扫描"
        android:onClick="defaultScan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:text="定制页面扫描"
        android:onClick="dingZhiScan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:text="选择图片解析"
        android:onClick="picScan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:text="生成"
        android:onClick="shengCheng"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

activity_sheng_cheng

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bwie.test.erweimademo.ShengChengActivity">
    <EditText
        android:id="@+id/edit_text_01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:onClick="normal"
        android:text="普通的二维码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:onClick="logoQR"
        android:text="logo二维码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <ImageView
        android:id="@+id/image_view"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

activity_custom_captrue

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwie.test.erweimademo.CustomCaptrueActivity">
    <FrameLayout
        android:id="@+id/fl_my_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">
    </FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:background="#AA333333"
        android:orientation="horizontal"
        android:paddingBottom="15dp"
        android:paddingTop="15dp">

        <LinearLayout
            android:id="@+id/linear1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="闪光灯"
                android:textColor="#FFFFFF" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="封面"
                android:textColor="#FFFFFF" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="街景"
                android:textColor="#FFFFFF" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="翻译"
                android:textColor="#FFFFFF" />
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

my_camera

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <SurfaceView
        android:id="@+id/preview_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!--取景框-->
    <com.dash.zxinglibrary.view.ViewfinderView
        android:id="@+id/viewfinder_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:inner_corner_color="@color/scan_corner_color"
        app:inner_corner_length="30dp"
        app:inner_corner_width="5dp"
        app:inner_height="200dp"
        app:inner_margintop="150dp"
        app:inner_scan_bitmap="@drawable/scan_image"
        app:inner_scan_iscircle="true"
        app:inner_scan_speed="10"
        app:inner_width="200dp" />
</FrameLayout>

values文件夹下colors

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="scan_corner_color">#ff0000</color>
</resources>

dimens

<resources>
    <dimen name="fab_margin">16dp</dimen>
</resources>

styles

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

ErWeiMaActivity

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.dash.zxinglibrary.activity.CaptureActivity;
import com.dash.zxinglibrary.activity.CodeUtils;

public class ErWeiMaActivity extends AppCompatActivity {
    private final int REQUEST_CODE = 1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_er_wei_ma);
    }

    //默认页面扫描
    public void defaultScan(View view) {
        Intent intent = new Intent(ErWeiMaActivity.this, CaptureActivity.class);
        startActivityForResult(intent, REQUEST_CODE);
    }

    //定制页面扫描
    public void dingZhiScan(View view) {
        Intent intent = new Intent(ErWeiMaActivity.this, CustomCaptrueActivity.class);
        startActivityForResult(intent, REQUEST_CODE);
    }

    //选择图片解析
    public void picScan(View view) {
    }

    //生成
    public void shengCheng(View view) {
        Intent intent = new Intent(ErWeiMaActivity.this, ShengChengActivity.class);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE){
            if (null != data){
                //拿到传递的数据
                Bundle bundle = data.getExtras();
                if (bundle == null){
                    return;
                }

                if(bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS){
                    //解析得到的结果
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED){
                    Toast.makeText(ErWeiMaActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}

ShengChengActivity

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.dash.zxinglibrary.activity.CodeUtils;

public class ShengChengActivity extends AppCompatActivity {
    private EditText editText;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sheng_cheng);
        //查找控件
        editText = findViewById(R.id.edit_text_01);
        imageView = findViewById(R.id.image_view);
    }

    //普通的二维码
    public void normal(View view) {
        String s = editText.getText().toString();
        if (s==""){
            return;
        }
        Bitmap bitmap = CodeUtils.createImage(s, 300, 300, null);
        imageView.setImageBitmap(bitmap);
    }

    //logo二维码
    public void logoQR(View view) {
        String s = editText.getText().toString();
        if (s== ""){
            return;
        }
        //logo
        Bitmap bitmap = CodeUtils.createImage(s,300,300, BitmapFactory.decodeResource(getResources(),
                           R.mipmap.ic_launcher));
        imageView.setImageBitmap(bitmap);
    }
}

CustomCaptrueActivity

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.dash.zxinglibrary.activity.CaptureFragment;
import com.dash.zxinglibrary.activity.CodeUtils;

public class CustomCaptrueActivity extends AppCompatActivity {
    //二维码解析回调函数
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        //解析成功的回调
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();

            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
            bundle.putString(CodeUtils.RESULT_STRING, result);
            resultIntent.putExtras(bundle);

            setResult(RESULT_OK, resultIntent);
            finish();
        }

        //解析失败的回调
        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
            bundle.putString(CodeUtils.RESULT_STRING, "");
            resultIntent.putExtras(bundle);
            setResult(RESULT_OK, resultIntent);
            finish();
        }
    };
    private boolean flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_captrue);
        CaptureFragment captureFragment = new CaptureFragment();

        //给扫描的fragment定制一个页面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);
        //设置监听
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container,captureFragment)
                              .commit();
        flag = false;

        //闪光灯
        findViewById(R.id.linear1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flag){//关闭
                    //打开
                    CodeUtils.isLightEnable(false);
                    flag = false;
                }else {
                    //打开
                    CodeUtils.isLightEnable(true);
                    flag = true;
                }
            }
        });
    }
}

MyApplication

import android.app.Application;
import com.dash.zxinglibrary.activity.ZXingLibrary;

public class MyApplication extends Application{
    @Override
    public void onCreate() {
        super.onCreate();

        //初始化zxing库
        ZXingLibrary.initDisplayOpinion(this);

    }
}
使用的依赖和方法

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

android:name=".MyApplication"
implementation project(':zxinglibrary')
阅读更多
个人分类: Android网络请求
上一篇点击图片放大跳转页面
下一篇MVP登录注册界面
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭