Android Support library for scanning and identifying barcodes Android supports scanning barcodes. But there's no real machine in hand, and I can't understand whether the feature is an application or an API-calling feature. But find a free Open-source Android library that handles barcodes on the Internet. See: Http://code.google.com/p/zxing Can be installed on Android as an application, see: http://code.google.com/p/zxing/wiki/GettingStarted Alternatively, use this feature in another program: Http://code.google.com/p/zxing/wiki/ScanningViaIntent Write the simplest Android scan barcode function There is already Java's Third-party open source Barcode Identification Library (zxing), in its own application scanning bar code in fact only need to invoke the implementation of the Third-party library. There is no G1 phone for the Android support library that scans and identifies barcodes, so you can try it now. In order to invoke zxing, you need to first install the barcode scanner in Google market: In order to invoke zxing, you need to first install the barcode scanner in Google market: The program is very simple, the first interface: The second interface, jumped to the Zxing library to provide the activity to do barcode scanning, which is scanned to generate QR two-dimensional bar code in the site URL. The QR code is instantly recognizable and then returned from the zxing activity to the program, displaying the contents of the entry: Writing code is simple. Java code: Package com.easymorse; Import android.app.Activity; Import android.content.Intent; Import Android.os.Bundle; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.Button; Import Android.widget.TextView; public class Showbarcodeactivity extends activity implements Onclicklistener { Private button button; Private TextView TextView; /** called the activity is a. */ @Override public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.main); This.button = (Button) This.findviewbyid (R.ID.BUTTON01); This.button.setOnClickListener (this); This.textview = (TextView) This.findviewbyid (R.id.hello); } @Override public void OnClick (view view) { Intent Intent = new Intent ("Com.google.zxing.client.android.SCAN"); Intent.putextra ("Scan_mode", "Qr_code_mode"); This.startactivityforresult (Intent, 0); } @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) { if (requestcode!= 0) { Return } This.textView.setText (Data.getstringextra ("Scan_result")); } } The main.xml below the layout directory: <?xml version= "1.0″encoding=" utf-8″?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:orientation= "Vertical" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" > <textview Android:id= "@+id/hello" Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:text= "@string/hello" /> <button android:text= "@string/button01″android:id=" @+id/button01″android:layout_width= "Wrap_content" Android: layout_height= "Wrap_content" ></Button> </LinearLayout> Strings.xml in the catalogue of values <?xml version= "1.0″encoding=" utf-8″?> <resources> <string name= "Hello" > currently has no scan results. </string> <string name= "app_name" > Shopping Small reference </string> <string name= "button01″> Click Start Scan ...</string> </resources> From this example, you can also see Android's powerful place to reuse services (service) or activity for other applications (reuse). Android Scan Commodity barcode Just write the simplest Android scan barcode function, can only scan QR two-dimensional code. If you want to scan the general merchandise bar code, you need to modify the program at a little bit. Changes to the interface: The effect of scanning barcodes: Show scan results (recognition speed is significantly lower than two-dimensional code): Changes to the code: Package com.easymorse; Import android.app.Activity; Import android.content.Intent; Import Android.os.Bundle; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.Button; Import Android.widget.TextView; public class Showbarcodeactivity extends activity { Private button button; Private Button button2; Private TextView TextView; /** called the activity is a. */ @Override public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.main); This.button = (Button) This.findviewbyid (R.ID.BUTTON01); This.button.setOnClickListener (New Onclicklistener () { @Override public void OnClick (View v) { Intent Intent = new Intent ( "Com.google.zxing.client.android.SCAN"); Intent.putextra ("Scan_mode", "Qr_code_mode"); Startactivityforresult (Intent, 0); } }); This.button2 = (Button) This.findviewbyid (R.ID.BUTTON02); This.button2.setOnClickListener (New Onclicklistener () { @Override public void OnClick (View v) { Intent Intent = new Intent ( "Com.google.zxing.client.android.SCAN"); Intent.putextra ("Scan_mode", "Ean_13″"); Startactivityforresult (Intent, 0); } }); This.textview = (TextView) This.findviewbyid (R.id.hello); } @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) { if (requestcode!= 0) { Return } This.textView.setText (Data.getstringextra ("Scan_result")); } } When you finish this example, you find that the implementation does not need to be written: Intent.putextra ("Scan_mode", "Qr_code_mode"); Zxing now the library can automatically identify which encoding is. If it is written, it is a specified type and cannot recognize other types of encoding. In fact, the above: Intent.putextra ("Scan_mode", "Ean_13″"); It's not written right. For a specific constant parameter, see: Http://zxing.org/w/docs/javadoc/constant-values.html The above content does not change, the correct source code share in the Google code of SVN, see: http://easymorse.googlecode.com/svn/tags/barcode.proto.0.1.0/ Example of identifying Book ISBN and outputting query results Wrote a technical prototype, through the phone to identify the book's ISBN barcode, and then through the Douban API, output ISBN corresponding book information. The example is very simple, come up with a button, click, enter the zxing activity, used to scan the book ISBN. Using the Zxing Scan feature, this time there are some problems. My Nexus One camera is dirty, causing the focus to be unrecognized, or very slow to recognize. Startled me, for there was no problem before. Replaced several versions of zxing, the problem remains, wiping the lens, the problem was immediately resolved. Nexus One's pixel and autofocus capabilities may also include CPU processing factors, slightly to the middle barcode, and immediately results. The previous G1 will be slower. This also brings the difficulty of capturing the screenshot above, as the instant scan is over. How to do it. I rubbed my hands on the camera a few more, and it was OK, haha. Tips to find books, through the Watercress API, access to the book's XML, here to say, watercress book information or relatively full, I used foreign books also found information. Because the use of Unicom WCDMA card, the whole process is very fast. Here are some of the key points in the code. How to scan barcodes Now do barcode scanning, Java general use zxing. Zxing has a variety of reuse methods, the simplest way is to install zxing software, through Google market. In its own application, the activity of the Zxing scan bar code is invoked via intent, and then the results of the scan are returned to their activity. The more complicated approach is to include the Zxing library in your own program, which is more appropriate for formal applications. The former is used in this article. Sample code Sample source code See: http://easymorse.googlecode.com/svn/tags/sou.book-0.2 There is also a simpler example that can be used to understand the basic process of invoking the zxing and watercress APIs, see: http://easymorse.googlecode.com/svn/tags/sou.book-0.1 to achieve activity jump In the 0.2 version, involving 3 activity, the first is to search for the activity of the book, see the top figure. Click on the button, into the zxing activity, visually, zxing to the ISBN, entered the third activity, showing the details of the book. In fact, from the Zxing get the ISBN, or return to the search book activity, but the activity immediately launched a new intent, into the new activity. In addition, this also shows the parameters that are shared between the activity through intent. The following code creates a new activity and sets the parameters: Intent Intent = new Intent (); Intent.setclass (this, searchbookactivity.class); Intent.putextra ("ISBN", Data.getextras (). getString ("Scan_result")); This.startactivity (Intent); The following are the parameters obtained from the intent: this.getintent (). Getextras (). getString ("ISBN"); for the use of zxing, you can refer to my other example, Android scanning commodity barcodes. Considerations for WebView using local pages In the activity that displays the results of a book query, use WebView, which shows the flexibility and simplicity that can be done. There are a number of ways to display a page, such as a page on a remote server, which requires server-side support. It seems to be a hassle, especially if you have an iphone, Android, and other heterogeneous terminals. Mobile phone developers can leave this part of the space, to the server side to solve. And it's a unified solution. However, in practice, there are two problems: server-side developers will be able to implement different styles for different mobile clients, and server-side developers are often not equipped with mobile devices, and it's hard to do it, but do you have an iphone, Android, and S60 phone for a matching server developer? Mobile terminal developers are not flexible enough to control the details of WebView, WebView (this refers to the Android environment, similar to the iphone environment) to support the use of JavaScript access to mobile phone services or objects, but this is platform-related, Android has its own code, The iphone also has its own code, which is hard to solve uniformly on the server side. In this prototype I tried to solve, that is, the HTML page on the phone side of the local, mobile phone and server interaction, but only to get the data, but not the display style (that is, the content of the HTML page), mobile phone through JavaScript, the resulting data injected into the local page display. The advantage of this approach is that the handset-side and server-side interaction is the acquisition of data, which is common to a variety of mobile phone systems, similar to Ajax, the demand for data traffic is reduced, and mobile-side developers can flexibly determine the style and interaction of the page. In this example, HTML and CSS are placed in the assets directory of the project, and, in addition, there is a picture in the directory that is used when the book picture is not loaded. Imagine that if you don't use webview, this requirement needs to be programmed, and you can see the processing in the pictures in the Android asynchronous loading ListView. The code to implement WebView is simple, and this is a simpler example, see Android writing Simple webview. This example related code: This.resultweb = (webview) This.findviewbyid (R.id.resultweb);
This.resultWeb.getSettings (). Setsupportzoom (false); This.resultWeb.getSettings (). setjavascriptcanopenwindowsautomatically ( true); This.resultWeb.getSettings (). Setjavascriptenabled (True); THIS.RESULTWEB.LOADURL (file:///android_asset/results.html); Note here that setjavascriptenabled enables JavaScript, otherwise JavaScript is not valid. JavaScript interoperability in Android Java and HTML pages The communication problem between JavaScript and Java, in this case, is only called Java through JavaScript, or rather simple. If Java calls JavaScript, you should start a separate thread to implement it. You need to create an interface object for a JavaScript call for the WebView object: This.resultWeb.addJavascriptInterface (New Object () {
Public String Getbookname () { return Bookinfo.getname (); } Public String getbooksummary () {
return Bookinfo.getsummary (); } Public String Getbookimageurl () {
return Bookinfo.getimageurl (); } Public String Getbookauthor () {
return Bookinfo.getauthor (); } }, "SearchResult"); The first parameter is the object that JavaScript calls, and the second argument is the name of the calling object. See how JavaScript calls this object: document.getElementById ("BookName"). Innerhtml=window.searchresult.getbookname (); You can see that the Window object has a SearchResult object property, and this searchresult is the second argument in the previous method. By that name, the inner class object created by the first argument above is called. Use the Watercress API Watercress API Official Website: http://www.douban.com/service/apidoc/ This only uses the function that obtains the book information XML according to the ISBN. Over here: http://www.douban.com/service/apidoc/reference/subject# Get book Information If you use this API anonymously, there are certain limitations: API calls are limited to requests not exceeding 10 times per minute. The number of calls here is per IP address. For mobile applications, more than 6 seconds to scan a book, should be enough. Parse the XML query result of watercress The results of the XML query for watercress were analyzed using the Xmlpull API from Android. There is a slight difference between this and Android parsing XML through Xmlpull, which is to get the attribute value of the XML, which is to get the text content in the node. Need: if (i = = Xmlpullparser.start_tag
&& parser.getname () equals ("attribute") && parser.getattributevalue (0). Equals ("title")) { Bookinfo.setname (Parser.nexttext ()); LOG.V ("Soubook", "title>>" + bookinfo.getname ()); Continue } obtained by the Parser.nexttext () method. Also, be aware that continue ends this cycle after the acquisition is complete. HTML and CSS This example shows the webview of the query results using a more unsightly gray background, mainly to demonstrate that external CSS can be used in conjunction with HTML. Similarly, you should be able to reference external JavaScript in an HTML page and plan to add jquery to the next version. |