1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Android识别图片中的WIFI二维码 并自动连接

Android识别图片中的WIFI二维码 并自动连接

时间:2019-09-13 21:22:16

相关推荐

Android识别图片中的WIFI二维码 并自动连接

原理:扫描或识别图片中的二维码后,解析其内容,打开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.mon.GlobalHistogramBinarizer;import com.mon.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。为了避免图片太大,这里对图片进行了压缩。* 感谢 /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 (pareToIgnoreCase("wpa") == 0) {netType = WifiAdmin.TYPE_WPA;} else if (pareToIgnoreCase("wep") == 0) {netType = WifiAdmin.TYPE_WEP;} else {netType = WifiAdmin.TYPE_NO_PWD;}wifiAdmin.addNetwork(netWorkName, password, netType);

5.参考

/p/14ce26f5a5c7

/android_cmos/article/details/52214560

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。