原理:扫描或识别图片中的二维码后,解析其内容,打开WiFi管理器,加入此WiFi。
实现步骤:
1.导入依赖库;
2.封装识别指定路径下二维码图片的方法;
3.获取WiFi的账号密码
4.打开WiFi管理器,连接此WiFi
1.导入依赖库
implementation 'com.google.zxing:core:3.2.1'
implementation 'cn.bingoogolapple:bga-qrcodecore:1.1.7@aar'
implementation 'cn.bingoogolapple:bga-zxing:1.1.7@aar'
2.封装识别指定路径下二维码图片的方法;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.common.GlobalHistogramBinarizer;
import com.google.zxing.common.HybridBinarizer;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
public class QRCodeUtil {
private static final String TAG = "QRCodeUtil";
private static final Map<DecodeHintType, Object> HINTS = new EnumMap<>(DecodeHintType.class);
static {
List<BarcodeFormat> allFormats = new ArrayList<>();
allFormats.add(BarcodeFormat.AZTEC);
allFormats.add(BarcodeFormat.CODABAR);
allFormats.add(BarcodeFormat.CODE_39);
allFormats.add(BarcodeFormat.CODE_93);
allFormats.add(BarcodeFormat.CODE_128);
allFormats.add(BarcodeFormat.DATA_MATRIX);
allFormats.add(BarcodeFormat.EAN_8);
allFormats.add(BarcodeFormat.EAN_13);
allFormats.add(BarcodeFormat.ITF);
allFormats.add(BarcodeFormat.MAXICODE);
allFormats.add(BarcodeFormat.PDF_417);
allFormats.add(BarcodeFormat.QR_CODE);
allFormats.add(BarcodeFormat.RSS_14);
allFormats.add(BarcodeFormat.RSS_EXPANDED);
allFormats.add(BarcodeFormat.UPC_A);
allFormats.add(BarcodeFormat.UPC_E);
allFormats.add(BarcodeFormat.UPC_EAN_EXTENSION);
HINTS.put(DecodeHintType.TRY_HARDER, BarcodeFormat.QR_CODE);
HINTS.put(DecodeHintType.POSSIBLE_FORMATS, allFormats);
HINTS.put(DecodeHintType.CHARACTER_SET, "utf-8");
}
/**
* 同步解析本地图片二维码。该方法是耗时操作,请在子线程中调用。
*
* @param picturePath 要解析的二维码图片本地路径
* @return 返回二维码图片里的内容 或 null
*/
public static String syncDecodeQRCode(String picturePath) {
return syncDecodeQRCode(getDecodeAbleBitmap(picturePath));
}
/**
* 同步解析bitmap二维码。该方法是耗时操作,请在子线程中调用。
*
* @param bitmap 要解析的二维码图片
* @return 返回二维码图片里的内容 或 null
*/
private static String syncDecodeQRCode(Bitmap bitmap) {
Result result = null;
RGBLuminanceSource source = null;
try {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
source = new RGBLuminanceSource(width, height, pixels);
result = new MultiFormatReader()
.decode(new BinaryBitmap(new HybridBinarizer(source)), HINTS);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
if (source != null) {
try {
result = new MultiFormatReader()
.decode(new BinaryBitmap(new GlobalHistogramBinarizer(source)), HINTS);
return result.getText();
} catch (Throwable e2) {
e2.printStackTrace();
}
}
return null;
}
}
/**
* 将本地图片文件转换成可解码二维码的 Bitmap。为了避免图片太大,这里对图片进行了压缩。
* 感谢 https://github.com/devilsen 提的 PR
*
* @param picturePath 本地图片文件路径
* @return
*/
private static Bitmap getDecodeAbleBitmap(String picturePath) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(picturePath, options);
int sampleSize = options.outHeight / 400;
if (sampleSize <= 0) {
sampleSize = 1;
}
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(picturePath, options);
} catch (Exception e) {
return null;
}
}
}
3.获取WiFi的账号和密码。
调用第2步中封装的识别图片二维码的方法
String strResult = QRCodeUtil.syncDecodeQRCode("/storage/emulated/0/test.png");
Log.d(TAG, "result: " + strResult);
字符串分割,获取网络名,密码及网络类型。
String netWorkNameTemp = strResult.substring(strResult.indexOf("S:"));
netWorkName = netWorkNameTemp.substring(3, netWorkNameTemp.indexOf(";") - 1);
String netWorkTypeTemp = strResult.substring(strResult.indexOf("T:"));
netWorkType = netWorkTypeTemp.substring(2, netWorkTypeTemp.indexOf(";"));
String passwordTemp = strResult.substring(strResult.indexOf("P:"));
password = passwordTemp.substring(3, passwordTemp.indexOf(";") - 1);
Log.i(TAG, "netWorkName: " + netWorkName + " netWorkType: " + netWorkType + " password: " + password);
4.打开WiFi管理器,连接此WiFi
if (!wifiAdmin.mWifiManager.isWifiEnabled()) {
wifiAdmin.openWifi();
}
int netType;
if (netWorkType.compareToIgnoreCase("wpa") == 0) {
netType = WifiAdmin.TYPE_WPA;
} else if (netWorkType.compareToIgnoreCase("wep") == 0) {
netType = WifiAdmin.TYPE_WEP;
} else {
netType = WifiAdmin.TYPE_NO_PWD;
}
wifiAdmin.addNetwork(netWorkName, password, netType);
5.参考
https://www.jianshu.com/p/14ce26f5a5c7
https://blog.csdn.net/android_cmos/article/details/52214560