iOS - 如何使用 zxing 解码图像

发布于 2024-12-11 13:48:08 字数 240 浏览 0 评论 0原文

我需要使用 iOS 的 zxing 库解码 QR 图像。

我是 iOS 编程新手,并且一直在查看项目中包含的代码示例,但我不知道如何使用这个库来解码图像。

如果有人可以发布我如何解码图像的示例,我们将不胜感激。

到目前为止,我已经确定了 Decoder 类,它有一个名为“decodeImage”的方法,我已经加载了图像。但这个方法返回一个布尔值,而我需要的是一个包含二维码值的文本字符串。

提前致谢!

I need to decode an QR image using the zxing library for iOS.

I'm new to iOS programming, and have been looking through the code examples included in the project, but I can't figure out how to just decode an image using this library.

If anyone can please post examples of how I decode an image it would be much appreciated.

So far I've identified the Decoder class, which has a method called "decodeImage" which I've loaded with an image. But this method returns a boolean, and what I need is a text string containing the value of the QR code.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟酒忠诚 2024-12-18 13:48:08

这是最终解决我的问题的代码 - 感谢 smparkes

头文件

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "ApplicationConfiguration.h"
#import <ZXingWidgetController.h> 

@interface ScanViewController : UIViewController<DecoderDelegate>
{
    UIButton *scanButton;
}

@property (nonatomic, retain) IBOutlet UIButton *scanButton;
@property (nonatomic, retain ) NSSet *readers;

- (IBAction)doScanAction;
- (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)result;
- (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason;

@end

实现文件的帮助

#import "ScanViewController.h"
#import <ZXingWidgetController.h> 
#import <QRCodeReader.h> 
#import "TwoDDecoderResult.h"

@implementation ScanViewController

@synthesize scanButton;
@synthesize readers;

-(IBAction)doScanAction{
    QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
    self.readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];
    [qrcodeReader release];

    Decoder *d = [[Decoder alloc] init];
    [d setDelegate:self];
    [d setReaders:self.readers];
    [readers retain];

    BOOL decodeSuccess= [d decodeImage:[UIImage imageNamed:@"QRcode.png"]];
    NSLog(@"BOOL = %@\n", (decodeSuccess ? @"YES" : @"NO"));
}

- (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)result{
    [result retain];
    NSLog(@"Did Decode Image Result: %d",[result text]);
    [result release];
}

- (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason;
{
    [reason retain];
    NSLog(@"Failed Decode Image Result: %d",reason);
    [reason release];
}

@end

This was the code that finally solved my problem - thanks to the help from smparkes

HEADER FILE

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "ApplicationConfiguration.h"
#import <ZXingWidgetController.h> 

@interface ScanViewController : UIViewController<DecoderDelegate>
{
    UIButton *scanButton;
}

@property (nonatomic, retain) IBOutlet UIButton *scanButton;
@property (nonatomic, retain ) NSSet *readers;

- (IBAction)doScanAction;
- (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)result;
- (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason;

@end

IMPLEMENTATION FILE

#import "ScanViewController.h"
#import <ZXingWidgetController.h> 
#import <QRCodeReader.h> 
#import "TwoDDecoderResult.h"

@implementation ScanViewController

@synthesize scanButton;
@synthesize readers;

-(IBAction)doScanAction{
    QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
    self.readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];
    [qrcodeReader release];

    Decoder *d = [[Decoder alloc] init];
    [d setDelegate:self];
    [d setReaders:self.readers];
    [readers retain];

    BOOL decodeSuccess= [d decodeImage:[UIImage imageNamed:@"QRcode.png"]];
    NSLog(@"BOOL = %@\n", (decodeSuccess ? @"YES" : @"NO"));
}

- (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)result{
    [result retain];
    NSLog(@"Did Decode Image Result: %d",[result text]);
    [result release];
}

- (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason;
{
    [reason retain];
    NSLog(@"Failed Decode Image Result: %d",reason);
    [reason release];
}

@end
┾廆蒐ゝ 2024-12-18 13:48:08

您需要创建一个委托类/实例并设置解码器 delegate 属性。然后,当您调用 decodeImage 时,小部件将调用 didDecodeImagefailedToDecodeImage

You need to create a delegate class/instance and set the decoder delegate property. Then the widget will call didDecodeImage or failedToDecodeImage when you call decodeImage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文