二维码

扫描二维码和生成二维码


导入依赖包libzxing


MainActivity:

package com.example.ass.myapplication2;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.xys.libzxing.zxing.activity.CaptureActivity;
import com.xys.libzxing.zxing.encoding.EncodingUtils;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private final int REQUESTCODE = 0;//请求码
    private final int RESULTCODE = 1;//结果码
    private TextView textView;

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

        Button start = (Button) findViewById(R.id.start);
        Button create = (Button) findViewById(R.id.create);
        final EditText content = (EditText) findViewById(R.id.content);
        final ImageView bit = (ImageView) findViewById(R.id.bit);
        textView = (TextView) findViewById(R.id.result_0);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("");
                Intent intent  = new Intent(MainActivity.this,CaptureActivity.class);
                startActivityForResult(intent,REQUESTCODE);
            }
        });
        create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content_string = content.getText().toString();
                Log.d(TAG, "onClick() returned: " + "取到的内容---" + content_string);
                Bitmap bitmap = EncodingUtils.createQRCode(content_string, 200, 200, null);
                bit.setImageBitmap(bitmap);
            }
        });
    }

    //接收返回结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.d(TAG, "onActivityResult() returned: " + "接收结果.....");

        if (resultCode==RESULT_OK){
            Log.d(TAG, "onActivityResult() returned: " + "返回成功");
            Bundle bundle = data.getExtras();
            String result = bundle.getString("result");
            textView.setText(result);
            Log.d(TAG,"----"+result+"---");
        }

    }
}

布局
activity_main.xml
<?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.example.ass.myapplication2.MainActivity">

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始扫描"
        />

    <TextView
        android:id="@+id/result"
        android:text="扫描结果是:"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/result_0"
        android:text=""
        android:textSize="35sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/create"
        android:text="生成二维码"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ImageView
        android:id="@+id/bit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

one_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/start"
        android:text="开始扫描"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ImageView
        android:id="@+id/img0"
        android:src="@drawable/aa"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <ImageView
        android:id="@+id/img1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值