标签:
问题:
公司项目考虑到跨平台一直都是用qt做,由于项目需求,项目上要增加一个二维码扫描功能,在安卓可以用QVideoProbe实现抓取摄像头视频帧,用QZxing解码图片,从而实现二维码扫描,但是在ios上,QVideProbe并不支持,所以只好选择其他抓取视频帧的方法,考虑使用OPencv实现抓取视频帧,但是在查看ios文档时,ios7 以上直接支持二维码扫描功能,所以放弃使用opencv抓取 + zxing解码的方法.从而采取ios官方提供的二维码解码功能.
实现:
由于我们项目ui一直是用qml实现,但是要实现扫描二维码功能,需要调用AVFoundation中的方法,同时要显示ios中的ui显示摄像头及返回qml 键.所以这里需要结合oc 和 qt编程.
直接上代码:
pro文件增加
ios {
OBJECTIVE_SOURCES += IOSView.mm \ # object c++ file
IOSCamera.mm
HEADER += IOSView.h \
IOSCamera.h \
IOSCameraViewProtocol.h
QMAKE_LFLAGS += -framework AVFoundation #add AVfoundation framework
QT += gui private
}
重新qmake生成xcode project
IOSView.#include <QQuickItem>
class IOSView : public QQuickItem { Q_OBJECT Q_PROPERTY(QString qrcodeText READ qrcodeText WRITE setQrcodeText NOTIFY qrcodeTextChanged) public: explicit IOSView(QQuickItem *parent = 0); QString qrcodeText() { return m_qrcodeText; } void setQrcodeText(QString text){ m_qrcodeText = text; emit qrcodeTextChanged(); } QString m_qrcodeText; public slots: void startScan(); //for open ios camera scan and ui private: void *m_delegate; //for communication with qt signals: void qrcodeTextChanged(); void stopCameraScan(); //show qml };
IOSView..mm
#include <UIKit/UIKit.h> #include <QtQuick> #include <QtGui> #include <QtGui/qpa/qplatformnativeinterface.h> #include "IOSView.h" #include "IOSCamera.h" @interface IOSCameraDelegate : NSObject <IOSCameraProtocol> { IOSView *m_iosView; } @end @implementation IOSCameraDelegate - (id) initWithIOSCamera:(IOSView *)iosView { self = [super init]; if (self) { m_iosView = iosView; } return self; } -(void) scanCancel{ emit m_iosView->stopCameraScan(); } -(void) scanResult :(NSString *) result{ m_iosView->setQrcodeText(QString::fromNSString(result)); } @end IOSView::IOSView(QQuickItem *parent) : QQuickItem(parent), m_delegate([[IOSCameraDelegate alloc] initWithIOSCamera:this]) { } void IOSView::startScan() { // Get the UIView that backs our QQuickWindow: UIView *view = static_cast<UIView *>(QGuiApplication::platformNativeInterface()->nativeResourceForWindow("uiview", window())); UIViewController *qtController = [[view window] rootViewController]; IOSCamera *iosCamera = [[[IOSCameraView alloc] init ]autorelease]; iosCamera.delegate = (id)m_delegate; // Tell the imagecontroller to animate on top: [qtController presentViewController:iosCamera animated:YES completion:nil]; [iosCamera startScan]; }
IOSCameraViewProtocol.h
#import <Foundation/Foundation.h> @protocol CameraScanViewProtocol <NSObject> @required -(void) scanCancel; -(void) scanResult :(NSString *) result; @end
IOSCamera.h
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import "CameraViewProtocol.h" @interface IOSCamera : UIViewController <AVCaptureMetadataOutputObjectsDelegate>{ id<CameraScanViewProtocol> delegate; } @property (retain, nonatomic) IBOutlet UIView *viewPreview; - (IBAction)backQtApp:(id)sender; -(void) startScan; @property (retain) id<CameraScanViewProtocol> delegate; @end
IOSCamera.cpp#import "IOSCamera.h"
@interface IOSCamera () @property (nonatomic,strong) AVCaptureSession * captureSession; @property (nonatomic,strong) AVCaptureVideoPreviewLayer * videoPreviewLayer;-(BOOL) startReading; -(void) stopReading; -(void) openQtLayer; @end @implementation CameraScanView @synthesize delegate; //Sync delegate for interactive with qt - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. // Initially make the captureSession object nil. _captureSession = nil; // Set the initial value of the flag to NO. _isReading = NO; // Begin loading the sound effect so to have it ready for playback when it‘s needed. [self loadBeepSound];
} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation - (void)dealloc { [_viewPreview release]; [super dealloc]; } - (void)viewDidUnload { [self setViewPreview:nil]; [super viewDidUnload]; }- (IBAction)backQtApp:(id)sender { [delegate scanCancel];
OK.大概流程就这些了,添加xib文件等就不介绍了.
标签:
原文地址:http://www.cnblogs.com/fuyanwen/p/4428599.html