修改扫码功能

This commit is contained in:
cebgcontract 2022-12-06 19:41:36 +08:00
parent 42dd1c98e3
commit 78a9a1ac38
19 changed files with 834 additions and 513 deletions

View File

@ -293,7 +293,8 @@ bool jsb_scanQRCode(se::State& s) {
NSString *nfunid = [NSString stringWithCString:funid.c_str() encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
[window.rootViewController scanQRCode:nfunid title:ntitle];
// [window.rootViewController scanQRCode:nfunid title:ntitle];
[window.rootViewController loadRestoreKey:nfunid oid: ntitle];
});
return true;
}

View File

@ -28,6 +28,7 @@
#import "QRCodeReaderDelegate.h"
#import "QRCodeReader.h"
typedef void (^QRReadCompletion)(NSString *_Nullable resultStr, NSError *_Nullable error);
/**
* Convenient controller to display a view to scan/read 1D or 2D bar codes like
* the QRCodes. It is based on the `AVFoundation` framework from Apple. It aims
@ -35,6 +36,7 @@
*/
@interface QRCodeReaderViewController : UIViewController
@property(nonatomic, nullable) QRReadCompletion completion;
#pragma mark - Creating and Inializing QRCodeReader Controllers
/** @name Creating and Inializing QRCode Reader Controllers */
@ -205,7 +207,7 @@
* is nil.
* @since 1.0.1
*/
- (void)setCompletionWithBlock:(nullable void (^) (NSString * __nullable resultAsString))completionBlock;
- (void)setCompletionWithBlock:(nullable QRReadCompletion)completion;
#pragma mark - Managing the Reader
/** @name Managing the Reader */

View File

@ -28,9 +28,14 @@
#import "QRCameraSwitchButton.h"
#import "QRCodeReaderView.h"
#import "QRToggleTorchButton.h"
#import "QRPhotoAlbumButton.h"
#import "LBXPermission.h"
#import "LBXPermissionSetting.h"
#import "LBXScanNative.h"
@interface QRCodeReaderViewController ()
@interface QRCodeReaderViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) QRCameraSwitchButton *switchCameraButton;
@property (strong, nonatomic) QRPhotoAlbumButton *photoAlbumButton;
@property (strong, nonatomic) QRToggleTorchButton *toggleTorchButton;
@property (strong, nonatomic) QRCodeReaderView *cameraView;
@property (strong, nonatomic) UIButton *cancelButton;
@ -39,8 +44,6 @@
@property (assign, nonatomic) BOOL showSwitchCameraButton;
@property (assign, nonatomic) BOOL showTorchButton;
@property (copy, nonatomic) void (^completionBlock) (NSString * __nullable);
@end
@implementation QRCodeReaderViewController
@ -107,13 +110,15 @@
__weak __typeof__(self) weakSelf = self;
[codeReader setCompletionWithBlock:^(NSString *resultAsString) {
if (weakSelf.completionBlock != nil) {
weakSelf.completionBlock(resultAsString);
if (weakSelf.completion) {
weakSelf.completion(resultAsString, nil);
}
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(reader:didScanResult:)]) {
[weakSelf.delegate reader:weakSelf didScanResult:resultAsString];
}
[weakSelf dismissViewControllerAnimated:YES completion:nil];
}];
}
return self;
@ -203,9 +208,9 @@
#pragma mark - Managing the Block
- (void)setCompletionWithBlock:(void (^) (NSString *resultAsString))completionBlock
- (void)setCompletionWithBlock:(nullable QRReadCompletion)completion
{
self.completionBlock = completionBlock;
self.completion = completion;
}
#pragma mark - Initializing the AV Components
@ -247,6 +252,14 @@
[_cancelButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[_cancelButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_cancelButton];
if (@available(iOS 8.0, *)) {
_photoAlbumButton = [[QRPhotoAlbumButton alloc] init];
[_photoAlbumButton setTranslatesAutoresizingMaskIntoConstraints:false];
[_photoAlbumButton addTarget:self action:@selector(showPhotoLibrary:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_photoAlbumButton];
}
}
- (void)setupAutoLayoutConstraints
@ -294,6 +307,15 @@
[_toggleTorchButton.widthAnchor constraintEqualToConstant:70]
]];
}
if (_photoAlbumButton) {
[NSLayoutConstraint activateConstraints:@[
[topLayoutAnchor constraintEqualToAnchor:_photoAlbumButton.topAnchor constant:-70],
[rightLayoutAnchor constraintEqualToAnchor:_photoAlbumButton.rightAnchor],
[_photoAlbumButton.heightAnchor constraintEqualToConstant:50],
[_photoAlbumButton.widthAnchor constraintEqualToConstant:70]
]];
}
}
- (void)switchDeviceInput
@ -306,10 +328,16 @@
- (void)cancelAction:(UIButton *)button
{
[_codeReader stopScanning];
if (_completionBlock) {
_completionBlock(nil);
if (self.completion) {
NSError *err = [NSError errorWithDomain:@"qrreader"
code:100
userInfo:@{
NSLocalizedDescriptionKey:@"cancel"
}];
self.completion(nil, err);
}
if (_delegate && [_delegate respondsToSelector:@selector(readerDidCancel:)]) {
[_delegate readerDidCancel:self];
@ -326,4 +354,69 @@
[_codeReader toggleTorch];
}
- (void)showPhotoLibrary:(UIButton *)button {
__weak __typeof(self) weakSelf = self;
[LBXPermission authorizeWithType:LBXPermissionType_Photos completion:^(BOOL granted, BOOL firstTime) {
if (granted) {
[weakSelf openLocalPhoto];
} else if (!firstTime ) {
[LBXPermissionSetting showAlertToDislayPrivacySettingWithTitle:@"Error" msg:@"The Photo Library is need to restore recovery key." cancel:@"Cancel" setting:@"Setting" completion:^{
// TODO:: cancel action
}];
}
}];
}
- (void)openLocalPhoto {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
// crash on some mobile
picker.allowsEditing = NO;
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark- - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[picker dismissViewControllerAnimated:YES completion:^{
[self handPhotoDidFinishPickingMediaWithInfo:info];
}];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController*)picker {
[picker dismissViewControllerAnimated:YES completion:^{
NSLog(@"imagePickerControllerDidCancel");
}];
}
- (void)handPhotoDidFinishPickingMediaWithInfo:(NSDictionary *)info {
__block UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage];
if (!image){
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
__weak __typeof(self) weakSelf = self;
[LBXScanNative recognizeImage:image success:^(NSArray<LBXScanResult *> *array) {
[weakSelf scanResultWithArray:array];
}];
}
- (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array {
if (!array || array.count < 1) {
NSLog(@"error scan photo");
return;
}
if (!array[0].strScanned || [array[0].strScanned isEqualToString:@""] ) {
NSLog(@"failed decoding QR code");
return;
}
LBXScanResult *scanResult = array[0];
if (self.completion) {
self.completion(scanResult.strScanned, nil);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end

View File

@ -0,0 +1,42 @@
//
// QRPhotoAlbumButton.h
// Unity-iPhone
//
// Created by zhl on 2022/12/6.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface QRPhotoAlbumButton : UIButton
/**
* @abstract The edge color of the drawing.
* @discussion The default color is the white.
* @since 2.0.0
*/
@property (nonatomic, strong) UIColor *edgeColor;
/**
* @abstract The fill color of the drawing.
* @discussion The default color is the darkgray.
* @since 2.0.0
*/
@property (nonatomic, strong) UIColor *fillColor;
/**
* @abstract The edge color of the drawing when the button is touched.
* @discussion The default color is the white.
* @since 2.0.0
*/
@property (nonatomic, strong) UIColor *edgeHighlightedColor;
/**
* @abstract The fill color of the drawing when the button is touched.
* @discussion The default color is the black.
* @since 2.0.0
*/
@property (nonatomic, strong) UIColor *fillHighlightedColor;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,125 @@
//
// QRPhotoAlbumButton.m
// Unity-iPhone
//
// Created by zhl on 2022/12/6.
//
#import "QRPhotoAlbumButton.h"
@implementation QRPhotoAlbumButton
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
_edgeColor = [UIColor whiteColor];
_fillColor = [UIColor darkGrayColor];
_edgeHighlightedColor = [UIColor whiteColor];
_fillHighlightedColor = [UIColor blackColor];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGFloat width = rect.size.width;
CGFloat height = rect.size.height;
CGFloat center = width / 2;
CGFloat middle = height / 2;
CGFloat strokeLineWidth = 2;
// Colors
UIColor *paintColor = (self.state != UIControlStateHighlighted) ? _fillColor : _fillHighlightedColor;
UIColor *strokeColor = (self.state != UIControlStateHighlighted) ? _edgeColor : _edgeHighlightedColor;
// box
CGFloat cameraWidth = width * 0.6;
CGFloat cameraHeight = cameraWidth * 0.618;
CGFloat cameraX = center - cameraWidth / 2;
CGFloat cameraY = middle - cameraHeight / 2;
CGFloat cameraRadius = cameraWidth / 80;
UIBezierPath *boxPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(cameraX, cameraY, cameraWidth, cameraHeight) cornerRadius:cameraRadius];
// Camera lens
CGFloat circleSize = cameraHeight * 0.2;
CGFloat cirlceX = cameraX + circleSize;
CGFloat circleY = cameraY + circleSize;
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(cirlceX, circleY, circleSize, circleSize)];
// triangle
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:CGPointMake(cameraX, cameraY + cameraHeight * 3 /4)];
[linePath addLineToPoint:CGPointMake(cirlceX, cameraY + cameraHeight / 2)];
[linePath addLineToPoint:CGPointMake(cameraX + cameraWidth * 2 / 5 , cameraY + cameraHeight * 4 / 5)];
[linePath addLineToPoint:CGPointMake(cameraX + cameraWidth * 7 / 10, cameraY + cameraHeight* 2 / 5)];
[linePath addLineToPoint:CGPointMake(cameraX + cameraWidth, cameraY + cameraHeight * 4 / 5)];
[linePath addLineToPoint:CGPointMake(cameraX + cameraWidth, cameraY + cameraHeight)];
[linePath addLineToPoint:CGPointMake(cameraX, cameraY + cameraHeight)];
[linePath closePath];
linePath.lineCapStyle = kCGLineCapRound;
linePath.lineJoinStyle = kCGLineJoinRound;
// drawing
[paintColor setFill];
// [boxPath fill];
[strokeColor setStroke];
boxPath.lineWidth = strokeLineWidth;
[boxPath stroke];
[paintColor setFill];
[circlePath fill];
[strokeColor setStroke];
circlePath.lineWidth = strokeLineWidth;
[circlePath stroke];
[paintColor setFill];
[linePath fill];
[strokeColor setStroke];
linePath.lineWidth = strokeLineWidth;
[linePath stroke];
}
// MARK: - UIResponder Methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[self setNeedsDisplay];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
[self setNeedsDisplay];
}
@end

View File

@ -9,8 +9,8 @@
#import "QRCodeReaderViewController.h"
#import "QRCodeReader.h"
#import "QRCodeReaderDelegate.h"
#include "permission/LBXPermission.h"
#include "permission/LBXPermissionSetting.h"
#import "LBXPermission.h"
#import "LBXPermissionSetting.h"
#include "LBXScanNative.h"
#include "LBXScanTypes.h"
#include "QrCodeViewController.h"
@ -21,6 +21,8 @@
@interface UIViewController (QR)<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@end
static QRCodeReaderViewController *qrcodeReaderVC = nil;
@implementation UIViewController (QR)
-(void)showQRCode:(NSString *) content title:(NSString *) title oid:(NSString *) oid {
@ -45,30 +47,31 @@
-(void)loadRestoreKey:(NSString *)funid oid:(NSString *) oid{
NSLog(@"loadRestoreKey::funid: %@, oid:%@", funid, oid);
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Tips"
message:@"In order to restore recovery key, please Scan QRCode or Pick from Photo Library."
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler: ^(UIAlertAction *action){
[self nativeCb:funid hasErr:YES dataStr: @"user cancel"];
}];
UIAlertAction *scanAction = [UIAlertAction actionWithTitle:@"Scan"
style:UIAlertActionStyleDefault
handler: ^(UIAlertAction *action){
[self scanQRCode:funid title: @"" restore:YES ];
}];
UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"Photo"
style:UIAlertActionStyleDefault
handler: ^(UIAlertAction *action){
[self openLocalPhotoAlbum: funid];
}];
[alertController addAction:actionCancel];
[alertController addAction:scanAction];
[alertController addAction:photoAction];
[self presentViewController:alertController animated:YES completion:nil];
// UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Tips"
// message:@"In order to restore recovery key, please Scan QRCode or Pick from Photo Library."
// preferredStyle:UIAlertControllerStyleActionSheet];
// UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel"
// style:UIAlertActionStyleCancel
// handler: ^(UIAlertAction *action){
// [self nativeCb:funid hasErr:YES dataStr: @"user cancel"];
// }];
//
// UIAlertAction *scanAction = [UIAlertAction actionWithTitle:@"Scan"
// style:UIAlertActionStyleDefault
// handler: ^(UIAlertAction *action){
// [self scanQRCode:funid title: @"" restore:YES ];
// }];
//
// UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"Photo"
// style:UIAlertActionStyleDefault
// handler: ^(UIAlertAction *action){
// [self openLocalPhotoAlbum: funid];
// }];
// [alertController addAction:actionCancel];
// [alertController addAction:scanAction];
// [alertController addAction:photoAction];
// [self presentViewController:alertController animated:YES completion:nil];
[self scanQRCode:funid title: @"" restore:YES ];
});
}
@ -76,31 +79,25 @@
std::string sfunid = std::string([funid UTF8String], [funid lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
__weak __typeof(self) weakSelf = self;
if ([QRCodeReader supportsMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]) {
static QRCodeReaderViewController *vc = nil;
static dispatch_once_t onceToken;
dispatch_async(dispatch_get_main_queue(), ^{
// if we are active again, we don't need to do this anymore
if (vc == nil) {
if (qrcodeReaderVC == nil) {
QRCodeReader *reader = [QRCodeReader readerWithMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
vc = [QRCodeReaderViewController readerWithCancelButtonTitle:@"Cancel" codeReader:reader startScanningAtLoad:YES showSwitchCameraButton:YES showTorchButton:YES];
vc.modalPresentationStyle = UIModalPresentationFormSheet;
qrcodeReaderVC = [QRCodeReaderViewController readerWithCancelButtonTitle:@"Cancel" codeReader:reader startScanningAtLoad:YES showSwitchCameraButton:YES showTorchButton:YES];
qrcodeReaderVC.modalPresentationStyle = UIModalPresentationFormSheet;
}
[vc setCompletionWithBlock:^(NSString *resultAsString) {
NSLog(@"Completion with result: %@", resultAsString);
[self dismissViewControllerAnimated:YES completion:^{
NSString *result;
if (resultAsString.length > 0) {
[self nativeCb:funid hasErr:NO dataStr: resultAsString];
} else {
NSLog(@"user cancel scan");
[weakSelf loadRestoreKey:funid oid:@""];
}
}];
[qrcodeReaderVC setCompletionWithBlock:^(NSString *resultAsString, NSError *error){
if (error != nil) {
[self nativeCb:funid hasErr:YES dataStr: error.localizedDescription];
} else {
NSLog(@"scan result: %@", resultAsString);
[self nativeCb:funid hasErr:NO dataStr: resultAsString];
}
}];
[self presentViewController:vc animated:YES completion:NULL];
[self presentViewController:qrcodeReaderVC animated:YES completion:NULL];
});
}
}
@ -132,22 +129,17 @@
#pragma mark- - PhotoAlbum
- (void)openLocalPhotoAlbum:(NSString *)funid {
__weak __typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[LBXPermission authorizeWithType:LBXPermissionType_Photos completion:^(BOOL granted, BOOL firstTime) {
if (granted) {
[weakSelf openLocalPhoto:funid];
}
else if (!firstTime )
{
[LBXPermissionSetting showAlertToDislayPrivacySettingWithTitle:@"Error" msg:@"The Photo Library is need to restore recovery key." cancel:@"Cancel" setting:@"Setting" completion:^{
[weakSelf loadRestoreKey:funid oid:@""];
}];
}
}];
});
[LBXPermission authorizeWithType:LBXPermissionType_Photos completion:^(BOOL granted, BOOL firstTime) {
if (granted) {
[weakSelf openLocalPhoto:funid];
} else if (!firstTime ) {
[LBXPermissionSetting showAlertToDislayPrivacySettingWithTitle:@"Error" msg:@"The Photo Library is need to restore recovery key." cancel:@"Cancel" setting:@"Setting" completion:^{
[weakSelf loadRestoreKey:funid oid:@""];
}];
}
}];
}
/*!
* open local Photo Library
*/

File diff suppressed because it is too large Load Diff

View File

@ -144,8 +144,8 @@
endingColumnNumber = "9223372036854775807"
startingLineNumber = "255"
endingLineNumber = "255"
landmarkName = "UnityAppController"
landmarkType = "3">
landmarkName = "-application:openURL:sourceApplication:annotation:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
@ -196,5 +196,53 @@
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "B244721D-1CF8-4F01-BE11-03431BBC8C90"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Classes_cocos/UIViewController+QR.mm"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "133"
endingLineNumber = "133"
landmarkName = "-openLocalPhotoAlbum:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "8C42DE34-128B-44B3-AB19-1444B79360D3"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "QRCodeReaderViewController/QRCodeReaderViewController.m"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "255"
endingLineNumber = "255"
landmarkName = "-setupUIComponentsWithCancelButtonTitle:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "DBC54861-88C5-466E-82D9-61205446F6CC"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "QRCodeReaderViewController/QRPhotoAlbumButton.m"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "48"
endingLineNumber = "48"
landmarkName = "-drawRect:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>

View File

@ -192,23 +192,35 @@ function erc20Balance(funId, address, account) {
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
});
}
/**
* send ERC20 token to to
*/
function sendErc20(funId, address, to, amount) {
jc.wallet
.sendErc20(address, to, amount)
jc.wallet.sendErc20(address, to, amount)
.then((result) => {
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result }));
})
.catch((err) => {
jsb.jcCallback(funId,JSON.stringify({errcode: 1,errmsg: err}));
jsb.jcCallback(funId,JSON.stringify({errcode: 1, errmsg: err}));
});
}
function scanQRCode(funId, title) {
console.log('scanQRCode: ' + title)
jc.wallet.nativeSvr.scanQRCode(title)
.then((result)=> {
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result }));
})
.catch((err) => {
jsb.jcCallback(funId, JSON.stringify({errcode: 1, errmsg: err }));
});
}
function exportWalletSecKey(funId) {
try {
console.log('scanQRCode: ' + title)
jsb.scanQRCode(funId, title);
let key = jsb.walletSecKey(funId)
return JSON.stringify({ errcode: 0, data: key });
} catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err });
}