Android uses the zbar QR code scan to resolve Chinese Garbled text and scan region definitions.
Preface (this is an update to the previous blog post [android uses zbar QR code scanning ])
Project download zbarLib compilation Project
Found today
There is a static shared library in com. example. qu. MainActivity that is not loaded in. An error is reported in earlier ANDROID versions.
Modification method:
Add code in line 33rd of com. example. qu. MainActivity:
Static {
System. loadLibrary ("iconv ");
}
One message was sent when I logged on to CSDN the day before yesterday: a netizen raised two questions
1. The scan box is only an illusion of full screen image resolution.
2. Chinese garbled characters
Various searches can be repaired today: Shared to users in a timely manner
Fill a picture (with a picture and a truth)
1. Scan box Definition
/*** 11:14:21 * Created By niexiaoqiang */package com. example. qu; import android. content. context; import android. graphics. canvas; import android. graphics. paint; import android. graphics. rect; import android. graphics. drawable. drawable; import android. util. attributeSet; import android. view. view;/*** search box * @ author niexiaoqiang */public class FinderView extends View {private static final long ANIMATION_DELAY = 30; private Paint finderMaskPaint; 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); // picture box 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); postInvalidateDelayed (ANIMATION_DELAY, middleRect. left, middleRect. top, middleRect. right, middleRect. bottom);} private Rect topRect = new Rect (); private Rect bottomRect = new Rect (); private Rect rightRect = new Rect (); private Rect leftRect = new Rect (); private Rect middleRect = new Rect (); private Rect lineRect = new Rect (); private Drawable zx_code_kuang; private Drawable zx_code_line; private int lineHeight; private void init (Context context) {int finder_mask = context. getResources (). 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 /////////////////////// * ** find the position of the rectangle frame in the Image Based on the image size, tip: After the camera is rotated 90 degrees, the image is taken horizontally. When all parameters are passed, * @ param w * @ param h * @ return */public Rect getScanImageRect (int w, int h) {// obtain the actual Rect 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. Scan Interface
Package com. example. qu; import net. sourceforge. zbar. config; import net. sourceforge. zbar. image; import net. sourceforge. zbar. imageworkflow; 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 Oid. 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"; private Camera mCamera; private SurfaceHolder mHolder; priv Ate SurfaceView surface_view; private image%scanner; private Handler autoFocusHandler; private AsyncDecode asyncDecode; private FinderView finder_view; private TextView textview; @ jsonvoid onCreate (Bundle savedInstanceState. 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); callback = new imageworkflow (); callback. setConfig (0, Config. x_DENSITY, 3); percent. setConfig (0, Config. y_DENSITY, 3); autoFocusHandler = new Handler (); asyncDecode = new AsyncDecode () ;}@ Overridepublic void surfaceChanged (SurfaceHol Der holder, int format, int width, int height) {if (mHolder. getSurface () = null) {return;} try {mCamera. stopPreview ();} catch (Exception e) {}try {mCamera. setDisplayOrientation (90); mCamera. setPreviewDisplay (mHolder); mCamera. setPreviewCallback (previewCallback); mCamera. startPreview (); mCamera. autoFocus (autoFocusCallback);} catch (Exception e) {Log. d ("DBG", "Error starting camera preview:" + e. getMes Sage () ;}}/*** Preview data */PreviewCallback previewCallback = new PreviewCallback () {public void onPreviewFrame (byte [] data, Camera camera) {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.exe c Ute (barcode); Camera. parameters parameters = camera. getParameters (); Size size = parameters. getPreviewSize (); // The Image is a 90-degree rotated Image source = new Image (size. width, size. height, "Y800"); Rect scanImageRect = finder_view.getScanImageRect (size. height, size. width); // The image is rotated 90 degrees, and the TOP of the scan box is used as the left cropping source. setCrop (scanImageRect. top, scanImageRect. left, scanImageRect. bottom, scanImageRect. right); source. setData (data); YncDecode = new AsyncDecode();asyncDecode.exe cute (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 = success. scanImage (barcode); if (result! = 0) {// mCamera. setPreviewCallback (null); // mCamera. stopPreview (); SymbolSet syms = runtime. 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 QR code Log. d (TAG, "128 QR code:" + sym. getData (); sb. append (sym. getData () + "\ n"); break; case Symbol. QRCODE: // QR code Log. d (TAG, "QR code:" + sym. getData (); 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"); break ;}}str = sb. toString (); return null ;}@ Overrideprotected void onPostExecute (Void result) {super. onPostExecute (result); stoped = true; if (null = str | str. equals ("") {} else {textview. setText (str) ;}} public boolean isStoped () {return stoped ;}/ *** Auto Focus callback */AutoFocusCallback autoFocusCallback = new AutoFocusCallback () {public void o NAutoFocus (boolean success, Camera camera) {autoFocusHandler. postDelayed (doAutoFocus, 1000) ;};// autofocus 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 = null ;}}@ Overr Idepublic 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>
Download the source code for some resource files. If you do not have any credits, contact QQ 312122330.
// Decode zbarLib compilation Project
1. I re-compiled the zbar library using NDK and made the following changes to zbarlib \ jni \ zbar \ qrcode \ qrdectxt. c.
/* This is the encoding the standard says is the default .*/
/*************************************** ******/
**********/
/*************************************** ******/
// 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. If you have trouble compiling NDK, you should study hard. Too many things, bad blog
When zbar is used for iphone development, zbar scans qr codes to confuse some Chinese characters. Is there a good solution?
# Callback method of The pragma mark camera
-(Void) imagePickerController: (UIImagePickerController *) reader
DidFinishPickingMediaWithInfo: (NSDictionary *) info
{
Id <NSFastEnumeration> results =
[Info objectForKey: ZBarReaderControllerResults];
ZBarSymbol * symbol = nil;
For (symbol in results)
{
[Reader. view addSubview: loadingProductByBarcode];
[LoadingProductByBarcode startAnimating];
CancelBtnInBarcode. enabled = NO;
ManualInputBtnBarcode. enabled = NO;
[NSThread detachNewThreadSelector: @ selector (toSearchByBarcode :) toTarget: self withObject: symbol. data];
Break;
}
}
Symbol. data returns an NSString. Is the output garbled?
/**
*
* This method generates a url encoded string (Chinese Character encoded in DOS state) by a UTF8 string.
* @ Return a url encoded string
*
*/
+ (NSString *) getUrlStringFromString :( NSString *) aStr {
NSString * outputStr = (NSString *)
CFURLCreateStringByAddingPercentEscapes (kCFAllocatorDefault,
(CFStringRef) aStr,
NULL,
(CFStringRef )@"! * '();: @ & = + $ ,/? % # [] ",
KCFStringEncodingUTF8 );
Return outputStr;
}
Try this similar method.
How to customize the scanning interface of the zBarSDK QR code open source Library
You can use the ZBarReaderView class to customize the interface. It has the start and stop methods to start and stop scanning.