• 使用Zxing及豆瓣API


    使用Zxing及豆瓣API | 疯狂的键盘

    使用Zxing及豆瓣API

    最近在做团队图书管理的一个Android端。因为需要通过手机扫描来输入图书信息(人工一条一条地输入,作为技术人员太受不了了),需要使用ZXing的API扫描图书ISBN,及使用豆瓣API来获取图书信息。

    由于时间关系,这里没有使用ZXing的jar包,而是下载并安装了它的开源项目——条码扫描器,然后调用里面的Activity扫描再获取结果。
    首先到市场下载Barcode Scaner(或搜索条码扫描器),下载安装。
    下面先贴上Activity的布局代码,因为是demo版,而且在内部使用,就没去好好做布局设计,只是把需要的控件都写上。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
     
        <Button
            android:id="@+id/home_scan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_scan" />
     
        <TextView
            android:id="@+id/home_result_scan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
     
        <TextView
            android:id="@+id/home_book_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
     
        <Button
            android:id="@+id/home_upload_result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_upload" />
     
        <TextView
            android:id="@+id/home_result_upload"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="false" />
     
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
     
            <Button
                android:id="@+id/home_borrow_book"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/borrow_book" />
     
            <Spinner
                android:id="@+id/home_users"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right" />
        </LinearLayout>
     
        <Button
            android:id="@+id/home_return_book"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/return_book" />
     
        <Button
            android:id="@+id/home_user_manager"
            android:layout_width="wrap_content"
            android:text="@string/manager_user"
            android:layout_height="wrap_content" />
     
    </LinearLayout>

    然后在我们的项目中,写一个Activity来调用Zxing的扫描功能,并通过它返回的结果访问互联网获取信息。

    下面是该Activity的代码。这里对于控件及事件的绑定,我使用了自己封装的一个工具(Androidkit)来简化这些代码,所以你们会看到许多类似@AndroidView的注解,这个工具可以在http://code.google.com/p/cfuture-androidkit/获取,或从https://github.com/msdx/androidkit上获得最新代码。

    里面的功能及流程大致如下:
    调用Zxinig的扫描条码的Activity,然后返回该扫描结果,显示扫描结果,然后调用豆瓣的API获取图书信息。接下来是要从我们的服务器上获得该图书的信息(如是否登记,有没有人借走等),但因为服务端还没搭建好,这一部分功能就没去实现。
    上面代码中,调用Zxing的过程比较简单,

    然后再在onActivityResult方法中对返回的结果进行处理就好了。
    然后从豆瓣API中获得的是xml格式的内容,这里我就使用了android自带的pull解析器。我需要获得的图书信息相对较少,Book的JavaBean如下:

    然后解析器的代码如下:

    然后是引用到的String资源,如下:

    最后的结果是这样子的:

  • 相关阅读:
    excel
    AWS学习之EC2
    约瑟夫问题
    centos7 系統vps安裝mysql5.6及設置本地遠程連接筆記
    搜索框的测试checklist
    产品把整个项目组拉走去创业,这是什么神操作
    python基础-python函数参数为print语句时的输出
    python基础学习笔记-切片难点
    session 、cookie、token的区别(转)
    robot framework python3环境下学习笔记(1)——安装robot framework
  • 原文地址:https://www.cnblogs.com/lexus/p/2878482.html
Copyright © 2020-2023  润新知