Zxing3.x的简单使用(二)

来源:互联网 发布:药品查询真伪软件 编辑:程序博客网 时间:2024/05/22 00:23

Zxing3.x的使用

  上一篇简单讲解了生成二维码,现在接着讲扫描二维码,扫描稍微复杂一些,需要自己用到SurfaceView,还好源码上来就用这个了,不用太封装,直接拿过来用即可,解析成功后用SurfaceHolder传递信息,源码实现比较复杂,调用起来不是很复杂。

 
cameraManager = new CameraManager(getApplication());
viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
viewfinderView.setCameraManager(cameraManager);

    resultView = findViewById(R.id.result_view);    handler = null;    lastResult = null;    handler = null;    lastResult = null;    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);    resetStatusView();    beepManager.updatePrefs();    ambientLightManager.start(cameraManager);    inactivityTimer.onResume();    Intent intent = getIntent();    copyToClipboard = prefs.getBoolean(PreferencesActivity.KEY_COPY_TO_CLIPBOARD, true)            && (intent == null || intent.getBooleanExtra(Intents.Scan.SAVE_HISTORY, true));    source = IntentSource.NONE;    sourceUrl = null;    scanFromWebPageManager = null;    decodeFormats = null;    characterSet = null;    if (intent != null) {        String action = intent.getAction();        String dataString = intent.getDataString();        if (Intents.Scan.ACTION.equals(action)) {            // Scan the formats the intent requested, and return the result to the calling activity.            source = IntentSource.NATIVE_APP_INTENT;            decodeFormats = DecodeFormatManager.parseDecodeFormats(intent);            decodeHints = DecodeHintManager.parseDecodeHints(intent);            if (intent.hasExtra(Intents.Scan.WIDTH) && intent.hasExtra(Intents.Scan.HEIGHT)) {                int width = intent.getIntExtra(Intents.Scan.WIDTH, 0);                int height = intent.getIntExtra(Intents.Scan.HEIGHT, 0);                if (width > 0 && height > 0) {                    cameraManager.setManualFramingRect(width, height);                }            }            if (intent.hasExtra(Intents.Scan.CAMERA_ID)) {                int cameraId = intent.getIntExtra(Intents.Scan.CAMERA_ID, -1);                if (cameraId >= 0) {                    cameraManager.setManualCameraId(cameraId);                }            }            String customPromptMessage = intent.getStringExtra(Intents.Scan.PROMPT_MESSAGE);            if (customPromptMessage != null) {            }        } else if (dataString != null &&                dataString.contains("http://www.google") &&                dataString.contains("/m/products/scan")) {            // Scan only products and send the result to mobile Product Search.            source = IntentSource.PRODUCT_SEARCH_LINK;            sourceUrl = dataString;            decodeFormats = DecodeFormatManager.PRODUCT_FORMATS;        } else if (isZXingURL(dataString)) {            // Scan formats requested in query string (all formats if none specified).            // If a return URL is specified, send the results there. Otherwise, handle it ourselves.            source = IntentSource.ZXING_LINK;            sourceUrl = dataString;            Uri inputUri = Uri.parse(dataString);            scanFromWebPageManager = new ScanFromWebPageManager(inputUri);            decodeFormats = DecodeFormatManager.parseDecodeFormats(inputUri);            // Allow a sub-set of the hints to be specified by the caller.            decodeHints = DecodeHintManager.parseDecodeHints(inputUri);        }        characterSet = intent.getStringExtra(Intents.Scan.CHARACTER_SET);    }    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);    SurfaceHolder surfaceHolder = surfaceView.getHolder();    if (hasSurface) {        // The activity was paused but not stopped, so the surface still exists. Therefore        // surfaceCreated() won't be called, so init the camera here.        initCamera(surfaceHolder);    } else {        // Install the callback and wait for surfaceCreated() to init the camera.        surfaceHolder.addCallback(this);    }

  核心代码就是这些,其他的也一起粘过来即可,官方写的有点乱,需要注意的是默认它会调用相机的调焦方法,让相机看东西更清楚,所以如果你正确的使用了该方法,仍然需要花费一点时间才能解码,那是正常的,只能是手机硬件的摄像头有待性能不行,跟代码的无关,android平台的手机,什么品质的都有,尤其是一些“垃圾”手机,摄像头做的烂的一X,扫码扫不出来正常。

  Zxing3.x版本修复了一些2.x版本的bug,比如横竖屏的问题,使用3.x版本直接正常使用即可,只是demo不是太友好而已。

0 0