我制作了一个应用程序,可以使用ARKit识别艺术品/绘画/纸张等。当它识别出图像时,它会切换到另一个视图控制器并告诉您有关它的信息。它可以识别一件艺术品。问题是我如何让应用程序识别多个图纸。这是代码。 AR 视图控制器:
import UIKit
import SpriteKit
import ARKit
import AVFoundation
struct ImageInformation {
let name: String
let description: String
let image: UIImage
}
class ViewController: UIViewController, ARSKViewDelegate {
@IBOutlet var sceneView: ARSKView!
@IBAction func options(){
let alert = UIAlertController(title: "Options", message: "Select one of the options below to continue.", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Enable Flashlight", style: .default, handler: { action in
//Enable Flashlight function
func toggleTorch(on: Bool) {
guard let device = AVCaptureDevice.default(for: AVMediaType.video),
device.hasTorch
else { return }
do {
try device.lockForConfiguration()
device.torchMode = on ? .on : .off
device.unlockForConfiguration()
} catch {
//Torch can not be used.
let alert = UIAlertController(title: "Flashlight Error", message: "We are unable to activate the flashlight. This could be because the flashlight is being used by another app", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dissmis", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Restart App", style: .destructive, handler: { action in
fatalError("The flashlight could not be used and user restarted app.")
}))
self.present(alert, animated: true)
}
}
toggleTorch(on: true)
}))
//Continue working on this function when you get back!!!
alert.addAction(UIAlertAction(title: "Disable Flashlight", style: .default, handler: { action in
//Enable flashlight function here
func toggleTorch(off: Bool) {
guard let device = AVCaptureDevice.default(for: AVMediaType.video),
device.hasTorch
else { return }
do {
try device.lockForConfiguration()
device.torchMode = off ? .on : .off
device.unlockForConfiguration()
} catch {
//Torch can not be used.
let alert = UIAlertController(title: "Flashlight Error", message: "We are unable to activate the flashlight. This could be because the flashlight is being used by another app", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dissmis", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Restart App", style: .destructive, handler: { action in
fatalError("The flashlight could not be used and user restarted app.")
}))
self.present(alert, animated: true)
}
}
toggleTorch(off: false)
}))
alert.addAction(UIAlertAction(title: "Clear Recently Visited Artworks", style: .destructive, handler: { action in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "action_done")
self.present(secondViewController, animated: false, completion: nil)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
var selectedImage : ImageInformation?
let images = ["flower" : ImageInformation(name: "Flower Drawing", description: "This is a drawing of a flower and was made by the developer of this app. It was intended to be a thank you card for a teacher on Teacher appreciation day. The Teacher enjoyed the project.", image: UIImage(named: "flower")!)]
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
sceneView.showsFPS = false
sceneView.showsNodeCount = false
if let scene = SKScene(fileNamed: "Scene") {
sceneView.presentScene(scene)
}
guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else {
let alert = UIAlertController(title: "Resources not Found", message: "The files needed for this application to work propely can not be found. What would you like to do about this?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Continue Anyway", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Restart App", style: .default, handler: { action in
fatalError("The Recources could not be found on the users device.")
}))
self.present(alert, animated: true)
return
}
let configuration = ARWorldTrackingConfiguration()
configuration.detectionImages = referenceImages
sceneView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
}
// MARK: - ARSKViewDelegate
func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {
if let imageAnchor = anchor as? ARImageAnchor,
let referenceImageName = imageAnchor.referenceImage.name,
let scannedImage = self.images[referenceImageName] {
self.selectedImage = scannedImage
self.performSegue(withIdentifier: "switch", sender: self)
return imageSeenMarker()
}
return nil
}
private func imageSeenMarker() -> SKLabelNode {
let labelNode = SKLabelNode(text: "✅")
labelNode.horizontalAlignmentMode = .center
labelNode.verticalAlignmentMode = .center
return labelNode
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "switch"{
if let imageInformationVC = segue.destination as? ImageInformationViewController,
let actualSelectedImage = selectedImage {
imageInformationVC.imageInformation = actualSelectedImage
}
}
}
}
信息视图控制器:
import Foundation
import UIKit
class ImageInformationViewController : UIViewController {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var descriptionText: UILabel!
var imageInformation : ImageInformation?
override func viewDidLoad() {
super.viewDidLoad()
if let actualImageInformation = imageInformation {
self.nameLabel.text = actualImageInformation.name
self.imageView.image = actualImageInformation.image
self.descriptionText.text = actualImageInformation.description
}
}
@IBAction func dismissView(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
}
我该怎么做? 谢谢!
该函数referenceImages(inGroup:bundle:)
从资产加载所有参考图像。只需将多个图像放入资产中的AR资源组中,并为所有这些图像分配一个唯一的名称即可。