漢信碼在iOS用戶端中的應用和遇到的坑

來源:互聯網
上載者:User

標籤:漢字編碼 ios opencv

先簡單介紹一下的 漢信碼,基本上和 QRCode 即二維碼 大差不差,可但是,二維碼 一般掃描出來是 非中文的字串(一般為連結),這就是漢信碼區別於二維碼的地方,漢信碼是涵蓋中文的,而且是國家自主研發非騙經費項目,雖然沒有推廣起來但是還是很好用的。其官網為:http://cscode.gs1cn.org/

簡約而不簡單的網站,大家可以看一下,在此提供一個範例:650) this.width=650;" src="http://cscode.gs1cn.org/UserFiles/Image/HXMaili/cscode1.gif" height="220" width="185" alt="cscode1.gif" />


其優點:漢字編碼能力超強、極強抗汙損、抗畸變識讀能力、識讀速度快、資訊密度高、錯誤修正能力強、圖形美觀等官方這麼說的。


然後,針對不同的平台 官方提供了不同的解決方案來方便整合,但是 所提供的繼承文檔內容 少之又少:如為 iOS用戶端即成 文檔 非常簡潔:

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" />

接下來 開始結合文檔 開始整合 漢信數位 識別


鑒於 文檔如此簡潔 ok 知道了 函數需要傳入一個圖片的 usinged char 類型資料

於是乎 第一步 轉化 灰階圖片

-(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;}

第二步 拿到灰階圖片 轉為 一維數組資料

+ (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, rect, 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;    }


在這裡需要注意的是 對選擇的圖片 要做選景框處理 也就是需要截取只需要解碼的部分圖片 並進行一定的體積壓縮 否則會出現溢出。

當時準備完畢之後 運行 ,掃描 漢信碼  一直報 9001 錯誤 具體什麼錯誤 官方文檔並沒有給出明確的解釋,問題懸而未決。。。。


折磨了數周之後 仍然 未解決 ,細細想了一下 是否是 因為  漢信碼  所提供 sdk 不支援 iOS CoreGraphics 架構的 資料輸出 識別。

於是抱著試試的態度,把目標轉向了 opencv 這個 跨平台的 圖片處理庫;

OpenCV是一個基於BSD許可(開源)發行的跨平台電腦視覺庫,可以運行在Linux、Windows、Android和Mac OS作業系統上。它輕量級而且高效——由一系列 C 函數和少量 C++ 類構成,同時提供了Python、Ruby、MATLAB等語言的介面,實現了影像處理和電腦視覺方面的很多通用演算法。


使用 opencv 整合

第一步 轉化 灰階圖片

 

CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);    CGFloat cols = image.size.width;    CGFloat rows = image.size.height;        cv::Mat cvMat(rows, cols, 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函數對matImage進行灰階處理    cv::cvtColor(cvMat, matGrey, CV_BGR2GRAY);// 轉換成灰色    //使用灰階後的IplImage形式映像,用OSTU演算法算閾值:threshold    IplImage grey = matGrey;

    第二步從 灰階圖片中 擷取到 一維數組

   

 unsigned char* dataImage = (unsigned char*)grey.imageData;


    第三步 調用 漢信碼 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(@"解碼漢信-------%@",str);                if (_blockHanxinResult) {                    _blockHanxinResult(str);                }                                一定要 注意 !!! 返回UI線程 停止掃描 否則會應用會奔潰                dispatch_async(dispatch_get_main_queue(), ^{                    [self stopScan];                });            }        }else{            self.srcimage = nil;            self.hximg = nil;        }            } catch (int i) {    }


iOS 整合 漢信碼 功能算是 告一段落。。。

本文出自 “我學DOTNET” 部落格,請務必保留此出處http://5xueweb.blog.51cto.com/2948706/1921646

漢信碼在iOS用戶端中的應用和遇到的坑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.