业务场景
当实现二维码扫描的时候(用的扫码库是zbar),有时候需要悄悄的整个扫描的照片存储下来,(charles有部分变态业务就是这样)就是说给扫描的图片照个全景照片。
那么代码如何实现的呢?我去StackoverFlow里终于找到了实现办法!
protected PreviewCallback previewCb = new PreviewCallback() { public void onPreviewFrame(byte[] data, Camera camera) { try { Camera.Parameters parameters = camera.getParameters(); Size size = parameters.getPreviewSize(); Image barcode = new Image(size.width, size.height, "Y800"); barcode.setData(data); int result = scanner.scanImage(barcode); if (result != 0) { previewing = false; mCamera.setPreviewCallback(null); mCamera.stopPreview(); SymbolSet symbolSet = scanner.getResults(); for (Symbol sym : symbolSet) { try { // mediaPlayer.start(); } catch (Exception ex) { ex.printStackTrace(); } if (scanType == LIGHT_BAR_CODE && sym.getType() == Symbol.QRCODE) { SuperToastUtil.showToast(QrcodeCameraActivity.this, "只允许扫描条形码", Toast.LENGTH_LONG); finish(); return; } else if (scanType == LIGHT_QR_CODE && sym.getType() != Symbol.QRCODE) { SuperToastUtil.showToast(QrcodeCameraActivity.this, "只允许扫描二维码", Toast.LENGTH_LONG); finish(); return; } if (sym.getType() == Symbol.QRCODE) { codeType = LIGHT_QR_CODE; } else { codeType = LIGHT_BAR_CODE; } String code = sym.getData(); try { Size previewSize = camera.getParameters().getPreviewSize(); YuvImage yuvimage=new YuvImage(data, ImageFormat.NV21, previewSize.width, previewSize.height, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); yuvimage.compressToJpeg(new Rect(0, 0, previewSize.width, previewSize.height), 80, baos); byte[] jdata = baos.toByteArray(); Bitmap bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length); BitmapUtil.saveFile(QrcodeCameraActivity.this,bmp, getLogFileNameDateFormat() + ".jpg"); } catch (Exception e) { e.printStackTrace(); } handleDecodeInternally(code); } } } catch (Exception e) { e.printStackTrace(); } } };
主要用的是zbar 不过zbar这鸟东西 n久没有更新了。一直没有好的库给用,还有zxing,只有这两种扫码库可以用
最终于的代码在这里!:
try { Size previewSize = camera.getParameters().getPreviewSize(); YuvImage yuvimage=new YuvImage(data, ImageFormat.NV21, previewSize.width, previewSize.height, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); yuvimage.compressToJpeg(new Rect(0, 0, previewSize.width, previewSize.height), 80, baos); byte[] jdata = baos.toByteArray(); Bitmap bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length); BitmapUtil.saveFile(QrcodeCameraActivity.this,bmp, getLogFileNameDateFormat() + ".jpg"); } catch (Exception e) { e.printStackTrace(); }
时间: 2024-10-28 15:37:49