本文原創,轉載請註明原文地址:http://maosidiaoxian.iteye.com/blog/1743296或我在sinaapp上的博客:http://msdxblog.sinaapp.com/?p=716。
最近在作團隊圖書管理的一個Android端。由於須要經過手機掃描來輸入圖書信息(人工一條一條地輸入,做爲技術人員太受不了了),須要使用ZXing的API掃描圖書ISBN,及使用豆瓣API來獲取圖書信息。
因爲時間關係,這裏沒有使用ZXing的jar包,而是下載並安裝了它的開源項目——條碼掃描器,而後調用裏面的Activity掃描再獲取結果。
首先到市場下載Barcode Scaner(或搜索條碼掃描器),下載安裝。
下面先貼上Activity的佈局代碼,由於是demo版,並且在內部使用,就沒去好好作佈局設計,只是把須要的控件都寫上。java
<?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上得到最新代碼。android
/* * @(#)HomeActivity.java Project:bookscan * Date:2012-12-3 * * Copyright (c) 2011 CFuture09, Institute of Software, * Guangdong Ocean University, Zhanjiang, GuangDong, China. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sinaapp.msdxblog.bookscan.activity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.SimpleCursorAdapter; import android.widget.Spinner; import android.widget.TextView; import com.sinaapp.msdxblog.androidkit.thread.HandlerFactory; import com.sinaapp.msdxblog.androidkit.ui.ResBindUtil; import com.sinaapp.msdxblog.androidkit.ui.UIBindUtil; import com.sinaapp.msdxblog.androidkit.ui.annotation.AndroidRes; import com.sinaapp.msdxblog.androidkit.ui.annotation.AndroidRes.ResType; import com.sinaapp.msdxblog.androidkit.ui.annotation.AndroidView; import com.sinaapp.msdxblog.androidkit.ui.annotation.OnClick; import com.sinaapp.msdxblog.bookscan.R; import com.sinaapp.msdxblog.bookscan.bean.Book; import com.sinaapp.msdxblog.bookscan.util.DB; import com.sinaapp.msdxblog.bookscan.util.XMLSax; /** * @author Geek_Soledad (66704238@51uc.com) */ public class HomeActivity extends Activity { private static final int HOME_ACTIVITY = 1990; private static final String DOUBAN_API = "http://api.douban.com/book/subject/isbn/"; @AndroidView(id = R.id.home_result_scan) private TextView mTextScan; @AndroidView(id = R.id.home_book_info) private TextView mTextBook; @AndroidView(id = R.id.home_result_upload) private TextView mTextUpload; @AndroidView(id = R.id.home_users) private Spinner mSpinnerUser; @AndroidRes(id = R.string.result_scan, type = ResType.STRING) private String mStringScan; @AndroidRes(id = R.string.result_getting, type = ResType.STRING) private String mStringGetting; @AndroidRes(id = R.string.book_info, type = ResType.STRING) private String mStringBookInfo; private Handler mGettingBook; private Book book; private DB mDb; private SimpleCursorAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); UIBindUtil.bind(this, R.layout.activity_home); ResBindUtil.bindAllRes(this); init(); } /** * 初始化參數。 */ private final void init() { mGettingBook = HandlerFactory.getNewHandlerInOtherThread("book"); mDb = new DB(this); Cursor users = mDb.getAllUser(); startManagingCursor(users); mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, users, new String[] { "username" }, new int[] { android.R.id.text1 }); mAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mSpinnerUser.setAdapter(mAdapter); } @OnClick(viewId = { R.id.home_scan, R.id.home_upload_result, R.id.home_borrow_book, R.id.home_return_book, R.id.home_user_manager }) public void onButtonClick(View v) { switch (v.getId()) { case R.id.home_scan: Intent intent = new Intent("com.google.zxing.client.android.SCAN"); this.startActivityForResult(intent, HOME_ACTIVITY); break; case R.id.home_upload_result: break; case R.id.home_borrow_book: break; case R.id.home_return_book: break; case R.id.home_user_manager: startActivity(new Intent(this, UserManagerActivity.class)); break; default: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode != HOME_ACTIVITY) { super.onActivityResult(requestCode, resultCode, data); return; } if (data == null) { return; } final String isbn = data.getStringExtra("SCAN_RESULT"); mTextScan.setText(String.format(mStringScan, isbn)); if (isbn != null) { mTextBook.setText(mStringGetting); mGettingBook.post(new Runnable() { @Override public void run() { DefaultHttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(DOUBAN_API + isbn); try { HttpResponse response = client.execute(request); book = XMLSax.sax(response.getEntity().getContent()); String summary = book.getSummary(); summary = summary.substring(0, summary.length() < 60 ? summary.length() : 60) .concat("..."); String string = String.format(mStringBookInfo, book.getName(), book.getAuthor(), book.getPublisher(), book.getIsbn13(), summary); updateBookInfoView(string); } catch (Exception e) { e.printStackTrace(); } } }); } } /** * 更新圖書信息 * * @param string */ private void updateBookInfoView(final String string) { runOnUiThread(new Runnable() { @Override public void run() { mTextBook.setText(string); } }); } @Override protected void onResume() { super.onResume(); mAdapter.notifyDataSetChanged(); } @Override protected void onDestroy() { super.onDestroy(); mDb.close(); } }