知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷(xiāo)的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷(xiāo)工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
android二維碼工程之仿QQ二維碼實(shí)現(xiàn)
發(fā)表時(shí)間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):50
二維碼成長(zhǎng)到如今幾乎是每一個(gè)App都有的功能,之前銜目里用到了二維碼功能就研究了下若何嵌入zxing二維碼工程,之前的用法制包含了最根本的二維碼掃碼工能,用QQ時(shí)看到QQd的掃一掃,功能相對(duì)較全,可以掃圖片,可以開(kāi)閃光燈,還可以生成二維碼,都是比較常用的功能,于是就模仿QQ的二維碼樣式和功能,本身也做了一個(gè)common工程,如許,今后要用二維碼是就不必再做設(shè)備等工作了,直接接洽關(guān)系到這個(gè)二維碼工程即可.文┞仿最后我會(huì)將全部工程上傳到我的資本中,有須要的可以下載。
下面先看一下整體效不雅圖: 點(diǎn)擊相冊(cè)后的效不雅圖:
[img]http://img.blog.csdn.net/20150104162748581?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGNxNTIxMTMxNDEyMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center[img]http://img.blog.csdn.net/20150104164554904?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGNxNTIxMTMxNDEyMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
開(kāi)燈后的效不雅圖和設(shè)備串號(hào)生成的二維碼:
[img]http://img.blog.csdn.net/20150104195000878?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGNxNTIxMTMxNDEyMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast[img]http://img.blog.csdn.net/20150104194525453?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGNxNTIxMTMxNDEyMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
因?yàn)闆](méi)有美工合營(yíng),樣子真心是不如QQ好看,好在功能根本都實(shí)現(xiàn)了,相冊(cè)就是大年夜本地圖庫(kù)中選去二維碼掃描,開(kāi)燈就是打開(kāi)手機(jī)閃光燈,二維碼是將手機(jī)序列號(hào)生成一個(gè)二維碼。下面是代碼部分,修改的處所和添加的處所都做了注釋?zhuān)刨嚧竽暌辜夷芸炊??!?
1.相冊(cè)功能添加代碼,代碼添加在zxing工程已有的CaptureActivity.java類(lèi)中。
相冊(cè)點(diǎn)擊事宜處理:
public void onClick(View v) { switch (v.getId()) { case R.id.photo_btn: // 打開(kāi)手機(jī)中的相冊(cè) Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); // "android.intent.action.GET_CONTENT" innerIntent.setType("image/*"); Intent wrapperIntent = Intent.createChooser(innerIntent, "選擇二維碼圖片"); this.startActivityForResult(wrapperIntent, REQUEST_CODE); break; } }應(yīng)用zxing履行圖片掃碼:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch (requestCode) { case REQUEST_CODE: // 獲取選中圖片的路徑 Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null); if (cursor.moveToFirst()) { photo_path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); } cursor.close(); new Thread(new Runnable() { @Override public void run() { Result result = scanningImage(photo_path); if (result != null) { Message m = mHandler.obtainMessage(); m.what = PARSE_BARCODE_SUC; m.obj = result.getText(); mHandler.sendMessage(m); } else { Message m = mHandler.obtainMessage(); m.what = PARSE_BARCODE_FAIL; m.obj = "Scan failed!"; mHandler.sendMessage(m); } } }).start(); break; } } }scanneringImage()辦法代碼:
/** * 掃描二維碼圖片的辦法 * * @param path * @return */ public Result scanningImage(String path) { if (TextUtils.isEmpty(path)) { return null; } Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>(); hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); // 設(shè)置二維碼內(nèi)容的編碼 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 先獲取原大年夜小 scanBitmap = BitmapFactory.decodeFile(path, options); options.inJustDecodeBounds = false; // 獲取新的大年夜小 int sampleSize = (int) (options.outHeight / (float) 200); if (sampleSize <= 0) sampleSize = 1; options.inSampleSize = sampleSize; scanBitmap = BitmapFactory.decodeFile(path, options); RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap); BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { return reader.decode(bitmap1, hints); } catch (NotFoundException e) { e.printStackTrace(); } catch (ChecksumException e) { e.printStackTrace(); } catch (FormatException e) { e.printStackTrace(); } return null; }2.開(kāi)閃光燈,下面是開(kāi)閃光燈要添加的代碼,代碼添加在CameraManager.java中。
/** * 經(jīng)由過(guò)程設(shè)置Camera打開(kāi)閃光燈 */ public void turnLightOn() { if (camera == null) { return; } Parameters parameters = camera.getParameters(); if (parameters == null) { return; } List<String> flashModes = parameters.getSupportedFlashModes(); if (flashModes == null) { return; } String flashMode = parameters.getFlashMode(); Log.i(TAG, "Flash mode: " + flashMode); Log.i(TAG, "Flash modes: " + flashModes); // 閃光燈封閉狀況 if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) { // Turn on the flash if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) { parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(parameters); camera.startPreview(); } else { } } } /** * 經(jīng)由過(guò)程設(shè)置Camera封閉閃光燈 * @param mCamera */ public void turnLightOff() { if (camera == null) { return; } Parameters parameters = camera.getParameters(); if (parameters == null) { return; } List<String> flashModes = parameters.getSupportedFlashModes(); String flashMode = parameters.getFlashMode(); // Check if camera flash exists if (flashModes == null) { return; } // 閃光燈打開(kāi)狀況 if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) { // Turn off the flash if (flashModes.contains(Parameters.FLASH_MODE_OFF)) { parameters.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(parameters); } else { Log.e(TAG, "FLASH_MODE_OFF not supported"); } } }3.生成二維碼,代碼添加在CaptureActivity.java類(lèi)中。
/*** * 獲到手機(jī)設(shè)備的串號(hào) * @param context * @return */ public static String getIMEI(Context context) { TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); // Get deviceId String deviceId = tm.getDeviceId(); // If running on an emulator if (deviceId == null || deviceId.trim().length() == 0 || deviceId.matches("0+")) { deviceId = (new StringBuilder("EMU")).append((new Random(System.currentTimeMillis())).nextLong()) .toString(); } return deviceId; } /** * 生成二維碼辦法 */ private Bitmap createQRCode() { int QR_WIDTH = 100; int QR_HEIGHT = 100; try { // 須要惹人core包 QRCodeWriter writer = new QRCodeWriter(); String mime= getIMEI(this); if (text == null || "".equals(text) || text.length() < 1) { return null; } // 把輸入的文本轉(zhuǎn)為二維碼 BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT); System.out.println("w:" + martix.getWidth() + "h:" + martix.getHeight()); Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints); int[] pixels = new int[QR_WIDTH * QR_HEIGHT]; for (int y = 0; y < QR_HEIGHT; y++) { for (int x = 0; x < QR_WIDTH; x++) { if (bitMatrix.get(x, y)) { pixels[y * QR_WIDTH + x] = 0xff000000; } else { pixels[y * QR_WIDTH + x] = 0xffffffff; } } } // cheng chen de er wei ma Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT); return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; }
初始工程的設(shè)備和對(duì)掃描框的修改都參考了這篇博客,大年夜家可以看下。http://blog.csdn.net/xiaanming/article/details/10163203.
項(xiàng)目源碼
相關(guān)案例查看更多
相關(guān)閱讀
- 專(zhuān)業(yè)網(wǎng)站建設(shè)公司
- 網(wǎng)站排名優(yōu)化
- 小程序密鑰
- 政府網(wǎng)站建設(shè)服務(wù)
- 微信分銷(xiāo)
- 百度小程序開(kāi)發(fā)公司
- 云南網(wǎng)站建設(shè)優(yōu)化
- 大理網(wǎng)站建設(shè)公司
- 小程序開(kāi)發(fā)
- 報(bào)廢車(chē)拆解軟件
- 網(wǎng)站維護(hù)
- 表單
- 昆明網(wǎng)站建設(shè)公司
- 小程序開(kāi)發(fā)費(fèi)用
- 昆明小程序開(kāi)發(fā)聯(lián)系方式
- 安家微信小程序
- 昆明網(wǎng)站制作
- 云南網(wǎng)站建設(shè) 網(wǎng)絡(luò)服務(wù)
- 網(wǎng)站搭建
- 云南網(wǎng)站建設(shè)
- 云南小程序開(kāi)發(fā)公司
- 報(bào)廢車(chē)回收
- 小程序定制
- 云南網(wǎng)站設(shè)計(jì)
- 云南小程序代建
- 快排推廣
- 云南網(wǎng)站建設(shè)公司
- 網(wǎng)站開(kāi)發(fā)
- 出入小程序
- 北京小程序制作