add ios implementation and change readme
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// AppDelegate.h
|
||||
// lp
|
||||
//
|
||||
// Created by xiaojun on 2017/12/2.
|
||||
// Copyright © 2017年 xiaojun. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface AppDelegate : UIResponder <UIApplicationDelegate>
|
||||
|
||||
@property (strong, nonatomic) UIWindow *window;
|
||||
|
||||
|
||||
@end
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// AppDelegate.m
|
||||
// lp
|
||||
//
|
||||
// Created by xiaojun on 2017/12/2.
|
||||
// Copyright © 2017年 xiaojun. All rights reserved.
|
||||
//
|
||||
|
||||
#import "AppDelegate.h"
|
||||
|
||||
@interface AppDelegate ()
|
||||
|
||||
@end
|
||||
|
||||
@implementation AppDelegate
|
||||
|
||||
|
||||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
||||
// Override point for customization after application launch.
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
||||
- (void)applicationWillResignActive:(UIApplication *)application {
|
||||
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
|
||||
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
|
||||
}
|
||||
|
||||
|
||||
- (void)applicationDidEnterBackground:(UIApplication *)application {
|
||||
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
|
||||
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
|
||||
}
|
||||
|
||||
|
||||
- (void)applicationWillEnterForeground:(UIApplication *)application {
|
||||
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
|
||||
}
|
||||
|
||||
|
||||
- (void)applicationDidBecomeActive:(UIApplication *)application {
|
||||
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
|
||||
}
|
||||
|
||||
|
||||
- (void)applicationWillTerminate:(UIApplication *)application {
|
||||
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,98 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "20x20",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "20x20",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "29x29",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "29x29",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "40x40",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "40x40",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "60x60",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "60x60",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "20x20",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "20x20",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "29x29",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "29x29",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "40x40",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "40x40",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "76x76",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "76x76",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "83.5x83.5",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ios-marketing",
|
||||
"size" : "1024x1024",
|
||||
"scale" : "1x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" systemVersion="17A277" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<scenes>
|
||||
<!--View Controller-->
|
||||
<scene sceneID="EHf-IW-A2E">
|
||||
<objects>
|
||||
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
|
||||
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
|
||||
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
|
||||
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
|
||||
</view>
|
||||
</viewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
||||
</objects>
|
||||
<point key="canvasLocation" x="53" y="375"/>
|
||||
</scene>
|
||||
</scenes>
|
||||
</document>
|
||||
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
|
||||
<device id="retina4_0" orientation="portrait">
|
||||
<adaptation id="fullscreen"/>
|
||||
</device>
|
||||
<dependencies>
|
||||
<deployment identifier="iOS"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<scenes>
|
||||
<!--View Controller-->
|
||||
<scene sceneID="tne-QT-ifu">
|
||||
<objects>
|
||||
<viewController id="BYZ-38-t0r" customClass="ViewController" sceneMemberID="viewController">
|
||||
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
|
||||
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
|
||||
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
|
||||
</view>
|
||||
</viewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
|
||||
</objects>
|
||||
</scene>
|
||||
</scenes>
|
||||
</document>
|
||||
@@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
<true/>
|
||||
<key>UILaunchStoryboardName</key>
|
||||
<string>LaunchScreen</string>
|
||||
<key>UIMainStoryboardFile</key>
|
||||
<string>Main</string>
|
||||
<key>UIRequiredDeviceCapabilities</key>
|
||||
<array>
|
||||
<string>armv7</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations~ipad</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationPortraitUpsideDown</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// UIImageCVMatConverter.h
|
||||
// lp
|
||||
//
|
||||
// Created by xiaojun on 2017/12/2.
|
||||
// Copyright © 2017年 xiaojun. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef UIImageCVMatConverter_h
|
||||
#define UIImageCVMatConverter_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
#import <opencv2/opencv.hpp>
|
||||
#endif
|
||||
#ifdef __OBJC__
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <Foundation/Foundation.h>
|
||||
#endif
|
||||
using namespace cv;
|
||||
|
||||
@interface UIImageCVMatConverter : NSObject
|
||||
|
||||
+ (cv::Mat)cvMatFromUIImage:(UIImage *)image;
|
||||
+ (UIImage *)UIImageFromCVMat:(cv::Mat)image;
|
||||
+ (UIImage *)scaleAndRotateImageFrontCamera:(UIImage *)image;
|
||||
+ (UIImage *)scaleAndRotateImageBackCamera:(UIImage *)image;
|
||||
+(UIImage*) imageWithMat:(const cv::Mat&) image andImageOrientation: (UIImageOrientation) orientation;
|
||||
+(UIImage*) imageWithMat:(const cv::Mat&) image andDeviceOrientation: (UIDeviceOrientation) orientation;
|
||||
|
||||
@end
|
||||
|
||||
#endif /* UIImageCVMatConverter_h */
|
||||
@@ -0,0 +1,317 @@
|
||||
//
|
||||
// UIImageCVMatConverter.m
|
||||
// lp
|
||||
//
|
||||
// Created by xiaojun on 2017/12/2.
|
||||
// Copyright © 2017年 xiaojun. All rights reserved.
|
||||
//
|
||||
|
||||
|
||||
#include "UIImageCVMatConverter.h"
|
||||
@implementation UIImageCVMatConverter
|
||||
|
||||
+(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat{
|
||||
NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
|
||||
CGColorSpaceRef colorSpace;
|
||||
|
||||
if (cvMat.elemSize() == 1) {
|
||||
colorSpace = CGColorSpaceCreateDeviceGray();
|
||||
} else {
|
||||
colorSpace = CGColorSpaceCreateDeviceRGB();
|
||||
}
|
||||
|
||||
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
|
||||
|
||||
// Creating CGImage from cv::Mat
|
||||
CGImageRef imageRef = CGImageCreate(cvMat.cols, //width
|
||||
cvMat.rows, //height
|
||||
8, //bits per component
|
||||
8 * cvMat.elemSize(), //bits per pixel
|
||||
cvMat.step[0], //bytesPerRow
|
||||
colorSpace, //colorspace
|
||||
kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
|
||||
provider, //CGDataProviderRef
|
||||
NULL, //decode
|
||||
false, //should interpolate
|
||||
kCGRenderingIntentDefault //intent
|
||||
);
|
||||
|
||||
|
||||
// Getting UIImage from CGImage
|
||||
UIImage *finalImage = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:UIImageOrientationUp];
|
||||
CGImageRelease(imageRef);
|
||||
CGDataProviderRelease(provider);
|
||||
CGColorSpaceRelease(colorSpace);
|
||||
|
||||
return finalImage;
|
||||
}
|
||||
//缩放调整图片
|
||||
+ (UIImage *)scaleAndRotateImageBackCamera:(UIImage *)image
|
||||
{
|
||||
static int kMaxResolution = 480;
|
||||
CGImageRef imgRef = image.CGImage;
|
||||
CGFloat width = CGImageGetWidth(imgRef);
|
||||
CGFloat height = CGImageGetHeight(imgRef);
|
||||
|
||||
CGAffineTransform transform = CGAffineTransformIdentity;
|
||||
CGRect bounds = CGRectMake(0, 0, width, height);
|
||||
if (width > kMaxResolution || height > kMaxResolution) {
|
||||
CGFloat ratio = width/height;
|
||||
if (ratio > 1) {
|
||||
bounds.size.width = kMaxResolution;
|
||||
bounds.size.height = bounds.size.width / ratio;
|
||||
} else {
|
||||
bounds.size.height = kMaxResolution;
|
||||
bounds.size.width = bounds.size.height * ratio;
|
||||
}
|
||||
}
|
||||
CGFloat scaleRatio = bounds.size.width / width;
|
||||
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
|
||||
CGFloat boundHeight;
|
||||
UIImageOrientation orient = image.imageOrientation;
|
||||
switch(orient) {
|
||||
case UIImageOrientationUp:
|
||||
transform = CGAffineTransformIdentity;
|
||||
break;
|
||||
case UIImageOrientationUpMirrored:
|
||||
transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
|
||||
transform = CGAffineTransformScale(transform, -1.0, 1.0);
|
||||
break;
|
||||
case UIImageOrientationDown:
|
||||
transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
|
||||
transform = CGAffineTransformRotate(transform, M_PI);
|
||||
break;
|
||||
case UIImageOrientationDownMirrored:
|
||||
transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
|
||||
transform = CGAffineTransformScale(transform, 1.0, -1.0);
|
||||
break;
|
||||
case UIImageOrientationLeftMirrored:
|
||||
boundHeight = bounds.size.height;
|
||||
bounds.size.height = bounds.size.width;
|
||||
bounds.size.width = boundHeight;
|
||||
transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
|
||||
transform = CGAffineTransformScale(transform, -1.0, 1.0);
|
||||
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
|
||||
break;
|
||||
case UIImageOrientationLeft:
|
||||
boundHeight = bounds.size.height;
|
||||
bounds.size.height = bounds.size.width;
|
||||
bounds.size.width = boundHeight;
|
||||
transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
|
||||
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
|
||||
break;
|
||||
case UIImageOrientationRightMirrored:
|
||||
boundHeight = bounds.size.height;
|
||||
bounds.size.height = bounds.size.width;
|
||||
bounds.size.width = boundHeight;
|
||||
transform = CGAffineTransformMakeScale(-1.0, 1.0);
|
||||
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
|
||||
break;
|
||||
case UIImageOrientationRight:
|
||||
boundHeight = bounds.size.height;
|
||||
bounds.size.height = bounds.size.width;
|
||||
bounds.size.width = boundHeight;
|
||||
transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
|
||||
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
|
||||
break;
|
||||
default:
|
||||
[NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
|
||||
}
|
||||
|
||||
UIGraphicsBeginImageContext(bounds.size);
|
||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||
if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
|
||||
CGContextScaleCTM(context, -scaleRatio, scaleRatio);
|
||||
CGContextTranslateCTM(context, -height, 0);
|
||||
} else {
|
||||
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
|
||||
CGContextTranslateCTM(context, 0, -height);
|
||||
}
|
||||
CGContextConcatCTM(context, transform);
|
||||
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
|
||||
UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
|
||||
UIGraphicsEndImageContext();
|
||||
NSLog(@"resize w%f,H%f",returnImage.size.width,returnImage.size.height);
|
||||
return returnImage;
|
||||
}
|
||||
|
||||
+(UIImage*) imageWithMat:(const cv::Mat&) image andDeviceOrientation: (UIDeviceOrientation) orientation
|
||||
{
|
||||
UIImageOrientation imgOrientation = UIImageOrientationUp;
|
||||
|
||||
switch (orientation)
|
||||
{
|
||||
case UIDeviceOrientationLandscapeLeft:
|
||||
imgOrientation =UIImageOrientationLeftMirrored; break;
|
||||
|
||||
case UIDeviceOrientationLandscapeRight:
|
||||
imgOrientation = UIImageOrientationDown; break;
|
||||
|
||||
case UIDeviceOrientationPortraitUpsideDown:
|
||||
imgOrientation = UIImageOrientationRightMirrored; break;
|
||||
case UIDeviceOrientationFaceUp:
|
||||
imgOrientation = UIImageOrientationRightMirrored; break;
|
||||
|
||||
default:
|
||||
case UIDeviceOrientationPortrait:
|
||||
imgOrientation = UIImageOrientationRight; break;
|
||||
};
|
||||
|
||||
return [UIImageCVMatConverter imageWithMat:image andImageOrientation:imgOrientation];
|
||||
}
|
||||
|
||||
+(UIImage*) imageWithMat:(const cv::Mat&) image andImageOrientation: (UIImageOrientation) orientation;
|
||||
{
|
||||
cv::Mat rgbaView;
|
||||
|
||||
if (image.channels() == 3)
|
||||
{
|
||||
cv::cvtColor(image, rgbaView, COLOR_BGR2BGRA);
|
||||
}
|
||||
else if (image.channels() == 4)
|
||||
{
|
||||
cv::cvtColor(image, rgbaView, COLOR_BGR2BGRA);
|
||||
}
|
||||
else if (image.channels() == 1)
|
||||
{
|
||||
cv::cvtColor(image, rgbaView, COLOR_GRAY2RGBA);
|
||||
}
|
||||
|
||||
NSData *data = [NSData dataWithBytes:rgbaView.data length:rgbaView.elemSize() * rgbaView.total()];
|
||||
|
||||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
||||
|
||||
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
|
||||
|
||||
CGBitmapInfo bmInfo = kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big;
|
||||
|
||||
// Creating CGImage from cv::Mat
|
||||
CGImageRef imageRef = CGImageCreate(rgbaView.cols, //width
|
||||
rgbaView.rows, //height
|
||||
8, //bits per component
|
||||
8 * rgbaView.elemSize(), //bits per pixel
|
||||
rgbaView.step.p[0], //bytesPerRow
|
||||
colorSpace, //colorspace
|
||||
bmInfo,// bitmap info
|
||||
provider, //CGDataProviderRef
|
||||
NULL, //decode
|
||||
false, //should interpolate
|
||||
kCGRenderingIntentDefault //intent
|
||||
);
|
||||
|
||||
// Getting UIImage from CGImage
|
||||
UIImage *finalImage = [UIImage imageWithCGImage:imageRef scale:1 orientation:orientation];
|
||||
CGImageRelease(imageRef);
|
||||
CGDataProviderRelease(provider);
|
||||
CGColorSpaceRelease(colorSpace);
|
||||
|
||||
return finalImage;
|
||||
}
|
||||
|
||||
|
||||
+ (cv::Mat)cvMatFromUIImage:(UIImage *)image
|
||||
{
|
||||
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 cvMat3(rows, cols, CV_8UC3); // 8 bits per component, 4 channels
|
||||
cv::cvtColor(cvMat, cvMat3,COLOR_RGBA2RGB);
|
||||
|
||||
return cvMat3;
|
||||
}
|
||||
+ (UIImage *)scaleAndRotateImageFrontCamera:(UIImage *)image
|
||||
{
|
||||
static int kMaxResolution = 640;
|
||||
CGImageRef imgRef = image.CGImage;
|
||||
CGFloat width = CGImageGetWidth(imgRef);
|
||||
CGFloat height = CGImageGetHeight(imgRef);
|
||||
CGAffineTransform transform = CGAffineTransformIdentity;
|
||||
CGRect bounds = CGRectMake( 0, 0, width, height);
|
||||
if (width > kMaxResolution || height > kMaxResolution) {
|
||||
CGFloat ratio = width/height;
|
||||
if (ratio > 1) {
|
||||
bounds.size.width = kMaxResolution;
|
||||
bounds.size.height = bounds.size.width / ratio;
|
||||
} else {
|
||||
bounds.size.height = kMaxResolution;
|
||||
bounds.size.width = bounds.size.height * ratio;
|
||||
}
|
||||
}
|
||||
|
||||
CGFloat scaleRatio = bounds.size.width / width;
|
||||
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
|
||||
CGFloat boundHeight;
|
||||
UIImageOrientation orient = image.imageOrientation;
|
||||
switch(orient) {
|
||||
case UIImageOrientationUp:
|
||||
transform = CGAffineTransformIdentity;
|
||||
break;
|
||||
case UIImageOrientationUpMirrored:
|
||||
transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
|
||||
transform = CGAffineTransformScale(transform, -1.0, 1.0);
|
||||
break;
|
||||
case UIImageOrientationDown:
|
||||
transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
|
||||
transform = CGAffineTransformRotate(transform, M_PI);
|
||||
break;
|
||||
case UIImageOrientationDownMirrored:
|
||||
transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
|
||||
transform = CGAffineTransformScale(transform, 1.0, -1.0);
|
||||
break;
|
||||
case UIImageOrientationLeftMirrored:
|
||||
boundHeight = bounds.size.height;
|
||||
bounds.size.height = bounds.size.width;
|
||||
bounds.size.width = boundHeight;
|
||||
transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
|
||||
transform = CGAffineTransformScale(transform, -1.0, 1.0);
|
||||
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
|
||||
break;
|
||||
case UIImageOrientationLeft:
|
||||
boundHeight = bounds.size.height;
|
||||
bounds.size.height = bounds.size.width;
|
||||
bounds.size.width = boundHeight;
|
||||
transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
|
||||
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
|
||||
break;
|
||||
case UIImageOrientationRight:
|
||||
case UIImageOrientationRightMirrored:
|
||||
boundHeight = bounds.size.height;
|
||||
bounds.size.height = bounds.size.width;
|
||||
bounds.size.width = boundHeight;
|
||||
transform = CGAffineTransformMakeScale(-1.0, 1.0);
|
||||
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
|
||||
break;
|
||||
default:
|
||||
[NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
|
||||
}
|
||||
UIGraphicsBeginImageContext( bounds.size );
|
||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||
if ( orient == UIImageOrientationRight || orient == UIImageOrientationLeft ) {
|
||||
CGContextScaleCTM(context, -scaleRatio, scaleRatio);
|
||||
CGContextTranslateCTM(context, -height, 0);
|
||||
}
|
||||
else {
|
||||
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
|
||||
CGContextTranslateCTM(context, 0, -height);
|
||||
}
|
||||
CGContextConcatCTM( context, transform );
|
||||
CGContextDrawImage( UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef );
|
||||
UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
|
||||
UIGraphicsEndImageContext();
|
||||
return returnImage;
|
||||
}@end
|
||||
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// ViewController.h
|
||||
// lp
|
||||
//
|
||||
// Created by xiaojun on 2017/12/2.
|
||||
// Copyright © 2017年 xiaojun. All rights reserved.
|
||||
//
|
||||
|
||||
//#import <UIKit/UIKit.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
#import <opencv2/opencv.hpp>
|
||||
|
||||
//#import <opencv2/imgproc/types_c.h>
|
||||
#import <opencv2/imgcodecs/ios.h>
|
||||
#endif
|
||||
|
||||
#ifdef __OBJC__
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
#endif
|
||||
|
||||
@interface ViewController : UIViewController<UIImagePickerControllerDelegate,
|
||||
UINavigationControllerDelegate,UIToolbarDelegate> {
|
||||
|
||||
cv::Mat source_image;
|
||||
}
|
||||
|
||||
@property (nonatomic, retain) UILabel *textLabel;
|
||||
@property (nonatomic, retain) UIImageView *imageView;
|
||||
@property (nonatomic, retain) UIImageView *textView;
|
||||
@property (nonatomic, retain) UIToolbar *toolbar;
|
||||
|
||||
-(void)loadButtonPressed:(id)sender;
|
||||
-(void)simpleRecognition:(cv::Mat&)src;
|
||||
-(NSString *)getPath:(NSString *)fileName;
|
||||
|
||||
@end
|
||||
|
||||
@@ -0,0 +1,216 @@
|
||||
//
|
||||
// ViewController.m
|
||||
// lp
|
||||
//
|
||||
// Created by xiaojun on 2017/12/2.
|
||||
// Copyright © 2017年 xiaojun. All rights reserved.
|
||||
//
|
||||
|
||||
|
||||
|
||||
#import "ViewController.h"
|
||||
#import "UIImageCVMatConverter.h"
|
||||
|
||||
#import "Pipeline.h"
|
||||
|
||||
using namespace pr;
|
||||
|
||||
|
||||
@interface ViewController ()
|
||||
|
||||
@end
|
||||
|
||||
@implementation ViewController
|
||||
|
||||
@synthesize imageView;
|
||||
@synthesize textView;
|
||||
@synthesize toolbar;
|
||||
@synthesize textLabel;
|
||||
|
||||
-(NSString *)getPath:(NSString*)fileName{
|
||||
NSString *bundlePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"model.bundle"];
|
||||
NSString *path = [bundlePath stringByAppendingPathComponent:fileName];
|
||||
return path;
|
||||
}
|
||||
|
||||
-(void)simpleRecognition:(cv::Mat&)src{
|
||||
|
||||
NSString *path_1 = [self getPath:@"cascade.xml"];
|
||||
NSString *path_7 = [self getPath:@"CharacterRecognization.caffemodel"];
|
||||
NSString *path_6 = [self getPath:@"CharacterRecognization.prototxt"];
|
||||
NSString *path_3 = [self getPath:@"HorizonalFinemapping.caffemodel"];
|
||||
NSString *path_2 = [self getPath:@"HorizonalFinemapping.prototxt"];
|
||||
NSString *path_5 = [self getPath:@"Segmentation.caffemodel"];
|
||||
NSString *path_4 = [self getPath:@"Segmentation.prototxt"];
|
||||
|
||||
std::string *cpath_1 = new std::string([path_1 UTF8String]);
|
||||
std::string *cpath_2 = new std::string([path_2 UTF8String]);
|
||||
std::string *cpath_3 = new std::string([path_3 UTF8String]);
|
||||
std::string *cpath_4 = new std::string([path_4 UTF8String]);
|
||||
std::string *cpath_5 = new std::string([path_5 UTF8String]);
|
||||
std::string *cpath_6 = new std::string([path_6 UTF8String]);
|
||||
std::string *cpath_7 = new std::string([path_7 UTF8String]);
|
||||
|
||||
|
||||
PipelinePR pr2 = PipelinePR::PipelinePR(*cpath_1,*cpath_2,*cpath_3,*cpath_4,*cpath_5,*cpath_6,*cpath_7);
|
||||
|
||||
std::vector<pr::PlateInfo> list_res = pr2.RunPiplineAsImage(src);
|
||||
std::string concat_results="";
|
||||
for(auto one:list_res){
|
||||
if(one.confidence>0.7){
|
||||
concat_results += one.getPlateName()+",";
|
||||
//std::cout<<"1-----"+one.getPlateName()+"----1";
|
||||
}
|
||||
}
|
||||
//std::cout<<"2--"+concat_results+"--2";
|
||||
|
||||
NSString *str=[NSString stringWithCString:concat_results.c_str() encoding:NSUTF8StringEncoding];
|
||||
if(str.length > 0){
|
||||
str = [str substringToIndex:str.length-1];
|
||||
str = [NSString stringWithFormat:@"result:%@",str];
|
||||
}else{
|
||||
str = [NSString stringWithFormat:@"result:null"];
|
||||
}
|
||||
|
||||
[self.textLabel setText:str];
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
|
||||
{
|
||||
[picker dismissViewControllerAnimated:YES completion:nil];
|
||||
|
||||
UIImage* temp = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
|
||||
|
||||
|
||||
|
||||
|
||||
UIImage *temp_image=[UIImageCVMatConverter scaleAndRotateImageBackCamera:temp];
|
||||
source_image=[UIImageCVMatConverter cvMatFromUIImage:temp_image];
|
||||
[self simpleRecognition:source_image];
|
||||
imageView.image = temp;
|
||||
|
||||
|
||||
|
||||
|
||||
//cv::Mat cvImage,cv2;
|
||||
//UIImageToMat(temp, cvImage);
|
||||
// if(!cvImage.empty()){
|
||||
// cv::Mat gray;
|
||||
// // 将图像转换为灰度显示
|
||||
// cv::cvtColor(cvImage,gray,CV_RGB2GRAY);
|
||||
// // 应用高斯滤波器去除小的边缘
|
||||
// cv::GaussianBlur(gray, gray, cv::Size(5,5), 1.2,1.2);
|
||||
// // 计算与画布边缘
|
||||
// cv::Mat edges;
|
||||
// cv::Canny(gray, edges, 0, 50);
|
||||
// // 使用白色填充
|
||||
// cvImage.setTo(cv::Scalar::all(225));
|
||||
// // 修改边缘颜色
|
||||
// cvImage.setTo(cv::Scalar(0,128,255,255),edges);
|
||||
// // 将Mat转换为Xcode的UIImageView显示
|
||||
// self.imageView.image = MatToUIImage(cvImage);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
|
||||
{
|
||||
[picker dismissViewControllerAnimated:YES completion:nil];
|
||||
}
|
||||
|
||||
|
||||
- (void)loadButtonPressed:(id)sender
|
||||
{
|
||||
|
||||
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
|
||||
picker.delegate = self;
|
||||
|
||||
if (![UIImagePickerController isSourceTypeAvailable:
|
||||
UIImagePickerControllerSourceTypePhotoLibrary])
|
||||
return;
|
||||
|
||||
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
|
||||
|
||||
[self presentViewController:picker animated:YES completion:nil];
|
||||
|
||||
}
|
||||
|
||||
- (void)viewDidLoad {
|
||||
[super viewDidLoad];
|
||||
// Do any additional setup after loading the view, typically from a nib.
|
||||
|
||||
|
||||
|
||||
CGRect bounds = [UIScreen mainScreen].bounds;
|
||||
imageView = [[UIImageView alloc] init];
|
||||
imageView.frame = CGRectMake(0, 160, bounds.size.width, bounds.size.height-210);
|
||||
imageView.contentMode=UIViewContentModeScaleAspectFit;
|
||||
imageView.backgroundColor = [UIColor clearColor];
|
||||
[self.view addSubview:imageView];
|
||||
|
||||
|
||||
/* Add the fps Label */
|
||||
UILabel *fps = [[UILabel alloc] initWithFrame:CGRectMake(20, 120, 180, 20)];
|
||||
fps.font=[UIFont fontWithName:@"华文细黑" size:14.0f];
|
||||
fps.backgroundColor=[UIColor clearColor];
|
||||
fps.textColor=[UIColor redColor];
|
||||
fps.textAlignment=NSTextAlignmentLeft;
|
||||
// fps.transform = CGAffineTransformMakeRotation(90);
|
||||
fps.text=@"result";
|
||||
self.textLabel = fps;
|
||||
[self.view addSubview:self.textLabel];
|
||||
[self.view bringSubviewToFront:self.textLabel];
|
||||
|
||||
toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, bounds.size.height- 44, bounds.size.width, 44)];
|
||||
[toolbar setBackgroundColor:[UIColor clearColor]];
|
||||
// toolbar.barStyle=UIBarStyleDefault;
|
||||
toolbar.tintColor=[UIColor blackColor];
|
||||
toolbar.translucent=YES;
|
||||
// [toolbar setTranslucent:YES];
|
||||
[self.toolbar setBackgroundImage:[UIImage new]
|
||||
forToolbarPosition:UIBarPositionAny
|
||||
barMetrics:UIBarMetricsDefault];
|
||||
toolbar.delegate=self;
|
||||
|
||||
|
||||
UIBarButtonItem*flexitem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
|
||||
|
||||
UIBarButtonItem*albumitem=[[UIBarButtonItem alloc]
|
||||
|
||||
initWithTitle:@"相册"
|
||||
style:UIBarButtonItemStylePlain
|
||||
|
||||
target:self
|
||||
|
||||
action:@selector(loadButtonPressed:)];
|
||||
|
||||
|
||||
|
||||
|
||||
[toolbar setItems:[NSArray arrayWithObjects:albumitem,flexitem,nil]];
|
||||
[self.view addSubview:toolbar];
|
||||
|
||||
// Do any additional setup after loading the view, typically from a nib
|
||||
toolbar.autoresizingMask = UIViewAutoresizingNone;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
- (void)didReceiveMemoryWarning {
|
||||
[super didReceiveMemoryWarning];
|
||||
// Dispose of any resources that can be recreated.
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// main.m
|
||||
// lp
|
||||
//
|
||||
// Created by xiaojun on 2017/12/2.
|
||||
// Copyright © 2017年 xiaojun. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "AppDelegate.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
@autoreleasepool {
|
||||
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user