您的位置:首页 > 移动开发 > Android开发

在Android上使用ZXing识别条码 二次开发笔记 (2)

2014-09-26 20:33 375 查看
1、在CaptureActivityHandler类里方法:

 

public void handleMessage(Message message)

 

接收对图片解码后的结果,如果界面成功则进入下面分支

case R.id.decode_succeeded:

        Log.d(TAG, "Got decode succeeded message");

        state = State.SUCCESS;

        Bundle bundle = message.getData();

        Bitmap barcode = bundle == null ? null :

            (Bitmap) bundle.getParcelable(DecodeThread.BARCODE_BITMAP);

        activity.handleDecode((Result) message.obj, barcode);

 

 

2、调用CaptureActivity类handleDecode->   private void handleDecodeInternally(Result rawResult, Bitmap barcode)

 

    //格式

    TextView formatTextView = (TextView) findViewById(R.id.format_text_view);

    formatTextView.setText(rawResult.getBarcodeFormat().toString());

    //类型

    ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(this, rawResult);

    TextView typeTextView = (TextView) findViewById(R.id.type_text_view);

    typeTextView.setText(resultHandler.getType().toString());

   //时间  这个就无所谓了

    DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);

    String formattedTime = formatter.format(new Date(rawResult.getTimestamp()));

    TextView timeTextView = (TextView) findViewById(R.id.time_text_view);

    timeTextView.setText(formattedTime);

 

    那条形码的值如何获得呢?  rawResult.getText();即可获得。

 

进行结果解码结果解析,解析结果例如:

 

图片+ 条形码的值:978771151622121

格式 EAN_13

类型 ISBN

时间 2011

 

其中条形码值和类型,格式这三个数据就是使用zxing图片解码最后得到的数据。

 

3、总结

 

zxing条形码扫描的工作流程:

1)启动相机,在间隔很短的时间内连续拍照

2)调用图片解码把拍到在图片进行解码

3)当解码出结果时,解码器返回成功结果和数据

4)在界面上显示解码后的数据
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android