The application of Han code in iOS client and the pits encountered

Source: Internet
Author: User

A simple introduction to the Han letter code, basically and QRCode that is the two-dimensional code big difference is not bad, but, the QR code is generally scanned out of the non-Chinese string (generally linked), this is the Chinese letter code is different from the two-dimensional code place, the Chinese letter code is covered in Chinese, but also the national independent research and development of non Although not promoted, but it is very useful. Its official website is: http://cscode.gs1cn.org/

Simple and not simple website, you can look at, provide a sample: 650) this.width=650; "Src=" http://cscode.gs1cn.org/UserFiles/Image/HXMaili/ Cscode1.gif "height=" "width=" 185 "alt=" Cscode1.gif "/>


Its advantages: Chinese character coding ability is super strong, strong anti-fouling, anti-distortion reading ability, reading speed, high information density, error correction ability, graphic aesthetics and so on official said.


Then, there are different solutions for different platform authorities to facilitate integration, but there are few inherited document content available: For example, the iOS client is a very concise document:

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/93/3C/wKioL1kJhTvhohzSAAIWYSR-Wug216.png-wh_500x0-wm_ 3-wmp_4-s_3372516882.png "style=" Float:none; "title=" 1.png "alt=" Wkiol1kjhtvhohzsaaiwysr-wug216.png-wh_50 "/>

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/93/3E/wKiom1kJhTuyLQrpAAGuMSb10Oo905.png-wh_500x0-wm_ 3-wmp_4-s_140248441.png "style=" Float:none; "title=" 2.png "alt=" Wkiom1kjhtuylqrpaagumsb10oo905.png-wh_50 "/>

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/93/3E/wKiom1kJhTyAnxG4AACXbhl3uH8945.png-wh_500x0-wm_ 3-wmp_4-s_3274561983.png "style=" Float:none; "title=" 3.png "alt=" Wkiom1kjhtyanxg4aacxbhl3uh8945.png-wh_50 "/>

The next step is to start integrating the document with the Chinese letter digital recognition


Since the document is so concise OK know the function needs to pass in a picture of usinged char type data

So the first step is to convert the grayscale image

-(uiimage*) Getgrayimage: (uiimage*) sourceimage{    int width =  sourceimage.size.width;    int height = sourceimage.size.height;     cgcolorspaceref colorspace = cgcolorspacecreatedevicegray ();     CGContextRef context = CGBitmapContextCreate  (nil,                                                     width,                                                     height,                                                    8,       // bits per component                                                     0,                                                     colorspace,                                                    kcgimagealphanone);     cgcolorspacerelease (colorspace);    if  (context == null)  {         return nil;    }     Cgcontextdrawimage (context,                        cgrectmake (0, 0, width, height),  sourceimage.cgimage);    uiimage *grayimage = [uiimage  Imagewithcgimage:cgbitmapcontextcreateimage (context)];      cgcontextrelease ( context);     retuRn grayimage;} 

Second step to get grayscale image to one-dimensional array data

+  (unsigned char *)  convertuiimagetobitmaprgba8: (uiimage *)  image {         cgimageref imageref = image. Cgimage;        // create a bitmap context to  draw the uiimage into    cgcontextref context = [self  newbitmaprgba8contextfromimage:imageref];        if (!context)  {        return NULL;    }         size_t width = cgimagegetwidth (IMAGEREF);     size_t height = cgimagegetheight (IMAGEREF);         cgrect rect = cgrectmake (0, 0, width, height);         // draw image into the context to get the raw image data     Cgcontextdrawimage (CONTEXT,&NBSP;RECT,&NBSP;IMAGEREF);        //  get a pointer to the data        unsigned  char *bitmapData =  (unsigned char *) cgbitmapcontextgetdata (context);         // Copy the data and release the  memory  (return memory allocated with new)     size_t  Bytesperrow = cgbitmapcontextgetbytesperrow (context);    size_t  bufferlength = bytesperrow * height;         Unsigned char *newbitmap = null;        if ( BitmapData)  {        newbitmap =  (unsigned char *) malloc (sizeof (Unsigned char)  *  Bytesperrow * height);                 if (Newbitmap)  {    // Copy the data             for (int i = 0; i <  bufferlength; ++i)  {                 newBitmap[i] = bitmapData[i];             }        }                 free (BitmapData);             } else {         nslog (@ "error getting bitmap pixel data\n ");    }         cgcontextrelease (context);        return newbitmap;     }


It is important to note that the selection of the image to do the scene box processing is the need to intercept only the part of the image to be decoded and a certain volume compression or overflow will occur.

When ready to run, scan Han letter code has been reported 9001 error specific what error official documents did not give a clear explanation, the problem is outstanding ....


After a few weeks of torture still unresolved, carefully think about whether it is because the SDK provided by the Chinese Code does not support the IOS coregraphics framework for data output recognition.

So with a try attitude, the goal turned to OPENCV this cross-platform image processing library;

OpenCV is a BSD-licensed (open source) distributed, cross-platform computer Vision library that can run on Linux, Windows, Android, and Mac OS operating systems. It is lightweight and efficient-consisting of a series of C functions and a small number of C + + classes, and provides interfaces to Python, Ruby, Matlab, and many more general-purpose algorithms for image processing and computer vision.


Integration with OPENCV

The first step to convert grayscale images

Cgcolorspaceref colorspace = cgimagegetcolorspace (image. Cgimage);    cgfloat cols = image.size.width;     cgfloat rows = image.size.height;        cv::mat  Cvmat (ROWS,&NBSP;COLS,&NBSP;CV_8UC4);  // 8 bits per component, 4 channels         cgcontextref contextref = cgbitmapcontextcreate (cvmat.data,                  // Pointer to  data                                                       cols,                         Width of bitmap                                                       rows,                        // Height of bitmap                                                       8,                           //  bits per component                                                       cvMat.step[0],               // Bytes per row                                                       colorSpace,                  // colorspace                                                       kCGImageAlphaNoneSkipLast |                                                       kcgbitmapbyteorderdefault); // bitmap info flags         cgcontextdrawimage (Contextref, cgrectmake (0, 0, cols,  rows),  image. Cgimage);     cgcontextrelease (contextref);     cgcolorspacerelease ( ColorspaCE);     cv::mat matgrey;    //cvtcolor function matimage grayscale processing      cv::cvtcolor (Cvmat, matgrey, cv_bgr2gray);//  converted to gray     // Using the Iplimage form image after grayscale, the Ostu algorithm is used to calculate the threshold value:threshold    iplimage grey = matgrey;

The second step gets the one-dimensional array from the grayscale image

unsigned char* dataimage = (unsigned char*) grey.imagedata;


The third step calls the Hanshin Code SDK

byte vecnetmap[189*189];    try {         int versionsize = preprocessimg (dataimage, srcimage.size.width,  Srcimage.size.height, vecnetmap);        if  (versionSize  >= 23 && versionsize < 189)  {             Byte szInfo [7828];             int ret = decodecsbyte (vecnetmap, versionsize,  Szinfo);            if  (ret > 0)  {                 Nsstringencoding gbkencoding = cfstringconvertencodingtonsstringencoding (kCFStringEncodingGB_ 18030_2000);                 nsstring *str = [[nsstring  alloc] initwithbytes:szinfo length:ret encoding:gbkencoding];                 nslog (@ "decode Chinese letter-------%@", str);                 if  (_ Blockhanxinresult)  {                     _blockhanxinresult (str);                 }                                   Be sure to   note  !!!   Return to UI thread   stop scanning   Otherwise the app will crash &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;   dispatch_async (Dispatch_get_main_queue (), ^{                     [self stopScan];                 });             }         }else{            self.srcimage =  nil;            self.hximg = nil;         }             } catch  (Int i)  {    }


IOS Integrated Chinese code function is ...

This article is from the "I Learn dotnet" blog, please be sure to keep this source http://5xueweb.blog.51cto.com/2948706/1921646

The application of Han code in iOS client and the pits encountered

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.