wifi生成二维码

wifi生成二维码

package com.atomic.moretool;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileOutputStream;

public class WifiQRCode extends AppCompatActivity {
    private EditText Qrw,Qrh,WifiName,WifiPwd;
    private ImageView ShowWifiInfo;
    private Button GenerateWifiQRCode;
    private RadioGroup Rg;
    private RadioButton Rb1,Rb2,Rb3,Rb4,Rb5;
    private String ET;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi_qrcode);
        Qrw=findViewById(R.id.qr_w);
        Qrh=findViewById(R.id.qr_h);
        WifiName=findViewById(R.id.wifi_name);
        WifiPwd=findViewById(R.id.wifi_pwd);
        ShowWifiInfo=findViewById(R.id.show_wifi_info);
        GenerateWifiQRCode=findViewById(R.id.generate_wifi_qrcode);
        Rg=findViewById(R.id.rg);
        Rb1=findViewById(R.id.rb1);
        Rb2=findViewById(R.id.rb2);
        Rb3=findViewById(R.id.rb3);
        Rb4=findViewById(R.id.rb4);
        Rb5=findViewById(R.id.rb5);
        init();
    }

    private void init() {
        Rg.setOnCheckedChangeListener((radioGroup, i) -> {
            if (i==Rb1.getId()){
                ET=Rb1.getText().toString().trim();
            }else if (i==Rb2.getId()){
                ET=Rb2.getText().toString().trim();
            }else if (i==Rb3.getId()){
                ET=Rb3.getText().toString().trim();
            }else if (i==Rb4.getId()){
                ET=Rb4.getText().toString().trim();
            }else
                ET=Rb5.getText().toString().trim();
        });
        GenerateWifiQRCode.setOnClickListener(view->{
            String wifiname=WifiName.getText().toString().trim();
            String wifipwd=WifiPwd.getText().toString().trim();
            String qrw=Qrw.getText().toString().trim();
            String qrh=Qrh.getText().toString().trim();
            if (!wifiname.equals("") && !wifipwd.equals("") && !qrw.equals("") && !qrh.equals("")){
                try {
                    if (!ET.equals("")){
                        String qrstr="WIFI:T:"+ET+";"+"S:"+wifiname+";"+"P:"+wifipwd+";;";
                        saveQrcode(qrstr,qrw,qrh);
                    }
                }catch (Exception e){
                    Toast.makeText(this, "需要选择加密类型", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }else{
                Toast.makeText(this, "先输入完整信息", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void saveQrcode(String qrstr, String qrw, String qrh) {
        Bitmap bitmap=QRCodeUtil.createQRCodeBitmap(qrstr,Integer.parseInt(qrw),Integer.parseInt(qrh));
        ShowWifiInfo.setImageBitmap(bitmap);
        try {
            String filepath= Environment.getExternalStorageDirectory().getAbsolutePath().trim()+"/Atomic/"+"wifi_qrcode"+".jpg";
            File file=new File(filepath);
            if (!file.exists()){
                file.createNewFile();
            }
            FileOutputStream fos=new FileOutputStream(file);
            if (bitmap!=null){
                bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
                fos.flush();
                fos.close();
                Toast.makeText(this, "保存到Atomic文件夹中", Toast.LENGTH_SHORT).show();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_margin="15sp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:text="二维码宽:"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <EditText
                android:id="@+id/qr_w"
                android:text="400"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <TextView
                android:text="二维码高:"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <EditText
                android:id="@+id/qr_h"
                android:text="400"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:text="wifi名称:"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <EditText
                android:text="TP_LINK_0A5C"
                android:layout_weight="1"
                android:id="@+id/wifi_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <TextView
                android:text="wifi密码:"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <EditText
                android:text="ab336008"
                android:layout_weight="1"
                android:id="@+id/wifi_pwd"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
        <LinearLayout
            android:layout_marginTop="10sp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:text="加密类型:"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <RadioGroup
                android:id="@+id/rg"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:ignore="UselessParent">
                <RadioButton
                    android:id="@+id/rb1"
                    android:text="WPA"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <RadioButton
                    android:id="@+id/rb2"
                    android:text="WPA2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <RadioButton
                    android:id="@+id/rb3"
                    android:text="WPA/WPA2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <RadioButton
                    android:id="@+id/rb4"
                    android:text="WEP"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <RadioButton
                    android:id="@+id/rb5"
                    android:text="EAP"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
            </RadioGroup>
        </LinearLayout>
        <ImageView
            android:id="@+id/show_wifi_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <Button
        android:id="@+id/generate_wifi_qrcode"
        android:layout_margin="15sp"
        android:layout_gravity="bottom|right"
        android:text="生成"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="RtlHardcoded" />
</FrameLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Atomic_space

你的鼓励就是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值