Android uses zbar QR code Scan-(to solve Chinese garbled and scan region definition)

Source: Internet
Author: User

Write at the top (this is an update to the previous blog post on "Android using zbar QR code Scan")

Project download Zbarlib compile project project download 0 points

Bug

In 2.3 of the system holder need to set type, otherwise it will be black screen (thanks to the Netizen "(Α⒎ to Sんа) 294439435")

Com.example.qu.MainActivity the 50th row of Mholder.settype (surfaceholder.surface_type_push_buffers);


Found today

There is a static shared library in com.example.qu.MainActivity that is not loaded in. Error in Android with low version number
Change method:
Add code on line 33rd of Com.example.qu.MainActivity:
static {
System.loadlibrary ("Iconv");
}



The day before yesterday morning landing csdn time a message: a netizen raised two points doubts

1. The scan box is just the illusion of doing it right now. is a full-screen image to parse

2. Chinese garbled phenomenon

A variety of search, today to repair: timely sharing to you netizens

Fill in the picture (there is a picture of the truth)




1. Scan box definition

/** * 2014-7-15 a.m. 11:14:21 * Created by Niexiaoqiang */package Com.example.qu;import Android.content.context;import an Droid.graphics.canvas;import Android.graphics.paint;import Android.graphics.rect;import Android.graphics.drawable.drawable;import android.util.attributeset;import android.view.view;/** * Find box * @author Niexiaoqiang */public class Finderview extends View {private static final long Animation_delay = 30;private Paint Finderma skpaint;private int measureedwidth;private int Measureedheight;public Finderview (context context) {super (context); Init (context);} Public Finderview (context context, AttributeSet Attrs) {Super (context, attrs); init (context);} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); Canvas.drawrect (Leftrect, findermaskpaint); Canvas.drawrect (Toprect, Findermaskpaint); Canvas.drawrect (Rightrect, Findermaskpaint); Canvas.drawRect (BottomRect , findermaskpaint);//Frame Zx_code_kuang.setbounds (Middlerect); Zx_code_kuang.draw (canvas); if (Linerect.bottom < Middlerect.bottom) {zx_code_line.setbounds (linerect); linerect.top = linerect.top + Lineheight/2;linerect.bottom = Linerect.bottom + LINEHEIGHT/2;} else {linerect.set (middlerect); linerect.bottom = Linerect.top + lineheight;zx_code_line.setbounds (lineRect);} Zx_code_line.draw (Canvas);p ostinvalidatedelayed (Animation_delay, Middlerect.left, Middlerect.top, Middlerect.right, Middlerect.bottom);} Private Rect toprect = new rect ();p rivate rect bottomrect = new rect ();p rivate rect rightrect = new rect ();p rivate rect le Ftrect = new rect ();p rivate rect middlerect = new rect ();p rivate rect linerect = new rect ();p rivate drawable Zx_code_kuang ;p rivate drawable zx_code_line;private int lineheight;private void init (context context) {int finder_mask = Context.getre Sources (). GetColor (r.color.finder_mask); findermaskpaint = new Paint (Paint.anti_alias_flag); Findermaskpaint.setcolor (finder_mask); Zx_code_kuang = Context.getresources (). Getdrawable (R.drawable.zx_code_ Kuang); zx_code_line = Context.getresourCES (). getdrawable (r.drawable.zx_code_line); lineheight = 30;} Add this method///////////////////////** * According to the picture size to find the rectangle in the picture location, Tip: Camera rotation 90 degrees later. The picture was shot sideways, and when all the parameters were passed, the interchange was made * @param w * @param h * @return */public rect getscanimagerect (int w, int h) {//First find the actual rectangle rect rect = New Rect (); rect.left = Middlerect.left;rect.right = Middlerect.right;float temp = h/(float) Measureedheight;rect.top = ( int) (Middlerect.top * temp); rect.bottom = (int) (Middlerect.bottom * temp); return rect; @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) { Super.onmeasure (Widthmeasurespec, heightmeasurespec); measureedwidth = Measurespec.getsize (WidthMeasureSpec); Measureedheight = Measurespec.getsize (heightmeasurespec); int borderWidth = MEASUREEDWIDTH/2 + 100;middlerect.set (( Measureedwidth-borderwidth)/2, (Measureedheight-borderwidth)/2, (Measureedwidth-borderwidth)/2 + borderWidth, (measureedheight-borderwidth)/2 + borderWidth); linerect.set(middlerect); linerect.bottom = linerect.top + lineheight;leftrect.set (0, Middlerect.top, Middlerect.left, Middlerect.bottom); Toprect.set (0, 0, measureedwidth, middlerect.top); Rightrect.set (Middlerect.right, Middlerect.top, Measureedwidth, Middlerect.bottom); Bottomrect.set (0, Middlerect.bottom, MeasureedWidth, measureedheight);}}

2. Scanning interface

Package Com.example.qu;import Net.sourceforge.zbar.config;import Net.sourceforge.zbar.image;import Net.sourceforge.zbar.imagescanner;import Net.sourceforge.zbar.symbol;import Net.sourceforge.zbar.SymbolSet; Import Android.app.activity;import Android.graphics.rect;import Android.hardware.camera;import Android.hardware.camera.autofocuscallback;import Android.hardware.camera.previewcallback;import Android.hardware.camera.size;import Android.os.asynctask;import Android.os.bundle;import Android.os.Handler; Import Android.util.log;import Android.view.surfaceholder;import Android.view.surfaceview;import Android.widget.textview;public class Mainactivity extends Activity implements Surfaceholder.callback {private static String TAG = "Xiaoqiang";p rivate Camera mcamera;private surfaceholder mholder;private surfaceview surface_view;private Imagescanner scanner;private Handler autofocushandler;private asyncdecode asyncdecode;private FinderView Finder_view; Private TextView TextView; @Overrideprotectedvoid OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.ac_zbar_ FINDER); init ();} private void Init () {Surface_view = (Surfaceview) Findviewbyid (r.id.surface_view); Finder_view = (Finderview) Findviewbyid (r.id.finder_view); TextView = (TextView) Findviewbyid (r.id.textview); mholder = Surface_view.getholder () ; Mholder.addcallback (this); scanner = new Imagescanner (); scanner.setconfig (0, config.x_density, 3); scanner.setconfig (0, Config.y_density, 3); Autofocushandler = new Handler (); asyncdecode = new Asyncdecode ();} @Overridepublic void Surfacechanged (surfaceholder holder, int format, int width, int height) {if (mholder.getsurface () = = NULL) {return;} try {Mcamera.stoppreview ();} catch (Exception e) {}try {mcamera.setdisplayorientation (n); Mcamera.setpreviewdisplay ( Mholder); Mcamera.setpreviewcallback (Previewcallback); Mcamera.startpreview (); Mcamera.autofocus ( Autofocuscallback);} catch (Exception e) {log.d ("DBG", "Error starting camera preview:" + e.getmeSsage ());}} /** * Preview Data */previewcallback previewcallback = new Previewcallback () {public void Onpreviewframe (byte[] data, Camera Camer A) {if (asyncdecode.isstoped ()) {//camera.parameters Parameters = Camera.getparameters ();//size Size = Parameters.getpreviewsize ();//image barcode = new Image (size.width, Size.Height, "Y800");//barcode.setdata (data);// Asyncdecode = new Asyncdecode ();//asyncdecode.execute (barcode); Camera.parameters Parameters = Camera.getparameters (); Size size = Parameters.getpreviewsize ();//The image is rotated by 90 degrees of image source = new Image (Size.width, size.height, "Y800"); Rect scanimagerect = Finder_view.getscanimagerect (Size.Height, size.width);//Picture rotated 90 degrees, The top of the scan box as the left clipping Source.setcrop (Scanimagerect.top, Scanimagerect.left, Scanimagerect.bottom, scanimagerect.right); Source.setdata (data); Asyncdecode = new Asyncdecode (); Asyncdecode.execute (source);}}; Private class Asyncdecode extends Asynctask<image, Void, void> {private Boolean stoped = true;private String str = " "; @Overrideprotected Void doinbackground (Image ... params) {stoped = false; StringBuilder sb = new StringBuilder (); Image barcode = Params[0];int result = scanner.scanimage (barcode); if (Result! = 0) {//mcamera.setpreviewcallback (null);//mcamera.stoppreview ();   Symbolset syms = Scanner.getresults (); for (Symbol sym:syms) {switch (Sym.gettype ())} {case SYMBOL.CODABAR:LOG.D (TAG, "barcode "+ sym.getdata ());//Barcode Sb.append (Sym.getdata () +" \ n "), break;case symbol.code128://128 encoded format QR code LOG.D (TAG," 128 encoded format QR code : "+ sym.getdata ()); Sb.append (Sym.getdata () +" \ n ") break;case symbol.qrcode://qr code QR Code log.d (TAG, QR code:" + sym.getd ATA ()), Sb.append (Sym.getdata () + "\ n"), break;case symbol.isbn10://isbn10 Book Query log.d (TAG, "ISBN10 book query:" + sym.getdata ( ); Sb.append (Sym.getdata () + "\ n"), break;case symbol.isbn13://isbn13 Book Query log.d (TAG, "ISBN13 book query:" + sym.getdata ()); SB . Append (Sym.getdata () + "\ n"), Break;case symbol.none:log.d (TAG, "Unknown:" + sym.getdata ()); Sb.append (Sym.getdata () + "\ n") ; BREAK;DEFAULT:LOG.D (TAG, "other: "+ sym.getdata ()); Sb.append (Sym.getdata () +" \ n "); str = sb.tostring (); return null;} @Overrideprotected void OnPostExecute (void result) {Super.onpostexecute (result); stoped = true;if (null = = str | | str.equa LS ("")) {} else {textview.settext (str);}} public Boolean isstoped () {return stoped;}} /** * Own active focus callback */autofocuscallback Autofocuscallback = new Autofocuscallback () {public void Onautofocus (Boolean success, C Amera camera) {autofocushandler.postdelayed (doautofocus, 1000);}};/  /own active focus private Runnable Doautofocus = new Runnable () {public void run () {if (null = = Mcamera | | null = = autofocuscallback) {return;} Mcamera.autofocus (Autofocuscallback);}}; @Overridepublic void surfacecreated (Surfaceholder holder) {try {Mcamera = Camera.open ()} catch (Exception e) {Mcamera = n ull;}} @Overridepublic void surfacedestroyed (Surfaceholder holder) {if (Mcamera! = null) {mcamera.setpreviewcallback (null); Mcamera.release (); mcamera = null;}} <pre name= "code" class= "HTML" >

3. Layout definition

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " >    <surfaceview        android:id= "@+id/surface_view"        android:layout_width= "Match_parent        " android:layout_height= "Match_parent"/>    <com.example.qu.finderview        android:id= "@+id/finder_view"        android:layout_width= "match_parent"        android:layout_height= "match_parent"/>    <textview        android:id= "@+id/textview"        android:layout_width= "match_parent"        android:layout_height= "Wrap_ Content "        android:background=" @android: Color/white "        android:gravity=" center_vertical "        android: Textcolor= "@color/rea"/></framelayout>

Some resource files please download the source code, assuming no points can QQ contact 312122330



About garbled resolution Zbarlib compiling project

1. I once again compiled the Zbar library with the NDK. and made a change to zbarlib\jni\zbar\qrcode\qrdectxt.c such as the following

/*this is the encoding of the standard says is the default.*/
/*********************************************/
/*********   Change here to support Chinese                                      **********/
/********************************* /
//latin1_cd=iconv_open ("UTF-8", "iso8859-1");
LATIN1_CD = Iconv_open ("UTF-8", "GB18030");


/*but This one is often used, as well.*/
//SJIS_CD = Iconv_open (" UTF-8 "," Sjis ");
SJIS_CD = Iconv_open ("UTF-8", "GB2312");
/*********************************************/

2.NDK compile this piece of friend with trouble. It is expected that we will study hard.

Too many things, no more blog



Android uses zbar QR code Scan-(to solve Chinese garbled and scan region definition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.