LoginSignup
14
14

More than 5 years have passed since last update.

[iOS] QRコードを生成して画像に合成する

Last updated at Posted at 2014-03-27

写真を撮ってサーバーにアップロード

アップロード先のURLをQRコードにして元の写真に合成
みたいな処理を実装した時の自分用メモ。

iOS 7に限定できる場合

Core ImageCIQRCodeGeneratorフィルターを利用する。
参考:iOS 7でバーコード・QRコードを読み取る方法と生成する方法(おまけもあるよ)

CIQRCodeGeneratorフィルターで生成したQRコードの画像はものすごく小さいので注意

QR-Code-Encoder-for-Objective-C を利用する方法

Cocoapodsで導入可能なQR-Code-Encoder-for-Objective-Cを利用すると↓のような短いコードでQRコードのUIImageを生成できる。

sample.mm
//the qrcode is square. now we make it 250 pixels wide
int qrcodeImageDimension = 250;

//the string can be very long
NSString* aVeryLongURL = @"http://thelongestlistofthelongeststuffatthelongestdomainnameatlonglast.com/";

//first encode the string into a matrix of bools,
//TRUE for black dot and FALSE for white. Let the encoder decide the error correction level and version
DataMatrix* qrMatrix = [QREncoder encodeWithECLevel:QR_ECLEVEL_AUTO version:QR_VERSION_AUTO string:aVeryLongURL];

//then render the matrix
UIImage* qrcodeImage = [QREncoder renderDataMatrix:qrMatrix imageDimension:qrcodeImageDimension];

Cocoapods

platform :ios
pod 'QR-Code-Encoder-for-Objective-C'

pod install でインストール

利用方法

まずはじめに、このライブラリはC++のソースを利用しているので、#import "QREncoder.h" を書く実装ファイルの拡張子を.mmに変更する。

UIImagePickerControllerを使って撮影した写真をアップロードして、そのURLからQRコードを生成、元の写真に合成して保存するような場合こんな感じになる。
注意点として、+[QREncoder renderDataMatrix:imageDimension:]で生成されるQRコードのUIImageには余白がないので、単純に描画するとリーダーが反応してくれないことがある。(CIQRCodeGeneratorフィルターは必要な余白も含めた画像を生成してくれる)
白い背景でない画像に合成する時などは、QRコードの周りに適度な余白を描画する必要がある。

SomeViewController.mm
#import "QREncoder.h"

...

#pragma mark -
#pragma UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    CGSize size = [image size];
    UIGraphicsBeginImageContext(size);
    CGRect rect;
    rect.origin = CGPointZero;
    rect.size = size;
    [image drawInRect:rect];

    //upload image and get its URL
    NSString *imageUrl = @"http://xxxxxxxxxx";
    DataMatrix *qrMatrix = [QREncoder encodeWithECLevel:QR_ECLEVEL_AUTO version:QR_VERSION_AUTO string:imageUrl];
    int qrDimention = 640;
    UIImage *qrImage = [QREncoder renderDataMatrix:qrMatrix imageDimension:qrDimention];
    //At first, draw white background and then draw QR code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, CGRectMake(image.size.width - qrDimention - 140.0f,
                                          image.size.height - qrDimention - 140.0f,
                                          qrDimention + 80.0f,
                                          qrDimention + 80.0f));
    [qrImage drawAtPoint:CGPointMake(image.size.width - qrDimention - 100.0f,
                                     image.size.height - qrDimention - 100.0f)];

    UIImage *mergedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    image = mergedImage;
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeImageToSavedPhotosAlbum:image.CGImage
                              orientation:(ALAssetOrientation)image.imageOrientation
                          completionBlock:^(NSURL *assetURL, NSError *error) {
        /* Do something */
    }];
}
14
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14