replate-camera 0.1.63 → 0.1.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,8 @@ import ARKit
2
2
  import RealityKit
3
3
  import UIKit
4
4
  import AVFoundation
5
+ import ImageIO
6
+ import MobileCoreServices
5
7
 
6
8
  @objc(ReplateCameraViewManager)
7
9
  class ReplateCameraViewManager: RCTViewManager {
@@ -780,7 +782,7 @@ class ReplateCameraController: NSObject {
780
782
  let ambientIntensity = lightEstimate.ambientIntensity
781
783
  let ambientColorTemperature = lightEstimate.ambientColorTemperature
782
784
 
783
- if ambientIntensity < 600 {
785
+ if ambientIntensity < 1000 {
784
786
  safeRejecter("005", "[ReplateCameraController] Image too dark", NSError(domain: "ReplateCameraController", code: 005, userInfo: nil))
785
787
  return
786
788
  }
@@ -812,38 +814,75 @@ class ReplateCameraController: NSObject {
812
814
  return context.createCGImage(ciImage, from: ciImage.extent)
813
815
  }
814
816
 
817
+ static func getCameraTransformString(from session: ARSession) -> String? {
818
+ guard let currentFrame = session.currentFrame else {
819
+ print("No current frame available")
820
+ return nil
821
+ }
822
+
823
+ // Extract the camera transform matrix
824
+ let cameraTransform = currentFrame.camera.transform
825
+
826
+ // Serialize the transform matrix into a string
827
+ let transformString = """
828
+ \(cameraTransform.columns.0.x),\(cameraTransform.columns.0.y),\(cameraTransform.columns.0.z),\(cameraTransform.columns.0.w);
829
+ \(cameraTransform.columns.1.x),\(cameraTransform.columns.1.y),\(cameraTransform.columns.1.z),\(cameraTransform.columns.1.w);
830
+ \(cameraTransform.columns.2.x),\(cameraTransform.columns.2.y),\(cameraTransform.columns.2.z),\(cameraTransform.columns.2.w);
831
+ \(cameraTransform.columns.3.x),\(cameraTransform.columns.3.y),\(cameraTransform.columns.3.z),\(cameraTransform.columns.3.w)
832
+ """
833
+
834
+ return transformString
835
+ }
815
836
 
816
837
  static func saveImageAsJPEG(_ image: UIImage) -> URL? {
838
+ let cameraTransform = getCameraTransformString(from: ReplateCameraView.arView.session)
817
839
  // Convert UIImage to Data with JPEG representation
818
- guard let imageData = image.jpegData(compressionQuality: 0.8) else {
840
+ guard let imageData = image.jpegData(compressionQuality: 1) else {
819
841
  // Handle error if unable to convert to JPEG data
820
842
  print("Error converting UIImage to JPEG data")
821
843
  return nil
822
844
  }
823
-
824
- // UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
825
-
845
+
846
+ // Create a CGImageSource from the image data
847
+ guard let source = CGImageSourceCreateWithData(imageData as CFData, nil) else {
848
+ print("Error creating image source")
849
+ return nil
850
+ }
851
+
826
852
  // Get the temporary directory URL
827
853
  let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
828
-
829
- // Create a unique filename (you can use a UUID or any other method to generate a unique name)
830
854
  let uniqueFilename = "image_\(Date().timeIntervalSince1970).jpg"
831
-
832
- // Combine the temporary directory URL with the unique filename to get the full file URL
833
855
  let fileURL = temporaryDirectoryURL.appendingPathComponent(uniqueFilename)
834
-
835
- do {
836
- // Write the JPEG data to the file
837
- try imageData.write(to: fileURL, options: .atomic)
838
-
839
- // Print the file URL for reference
840
- print("Image saved at: \(fileURL.absoluteString)")
841
- return fileURL
842
- } catch {
843
- // Handle the error if unable to write to the file
844
- print("Error saving image: \(error.localizedDescription)")
856
+
857
+ // Create a mutable copy of the metadata
858
+ guard let imageProperties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [CFString: Any] else {
859
+ print("Error copying image properties")
845
860
  return nil
846
861
  }
862
+ var mutableMetadata = imageProperties
863
+
864
+ // Add the camera transform to the metadata
865
+ mutableMetadata[kCGImagePropertyExifDictionary] = [
866
+ kCGImagePropertyExifUserComment: cameraTransform
867
+ ]
868
+
869
+ // Create a CGImageDestination to write the image data with metadata
870
+ guard let destination = CGImageDestinationCreateWithURL(fileURL as CFURL, kUTTypeJPEG, 1, nil) else {
871
+ print("Error creating image destination")
872
+ return nil
873
+ }
874
+
875
+ // Add the image from the source to the destination with the updated metadata
876
+ CGImageDestinationAddImageFromSource(destination, source, 0, mutableMetadata as CFDictionary)
877
+
878
+ // Finalize the image destination
879
+ if !CGImageDestinationFinalize(destination) {
880
+ print("Error finalizing image destination")
881
+ return nil
882
+ }
883
+
884
+ print("Image saved at: \(fileURL.absoluteString)")
885
+ return fileURL
847
886
  }
848
887
 
849
888
  func updateSpheres(deviceTargetInFocus: Int, cameraTransform: simd_float4x4, completion: @escaping (Bool) -> Void) {
@@ -961,7 +1000,7 @@ class ReplateCameraController: NSObject {
961
1000
 
962
1001
  static func angleBetweenAnchorXAndCamera(anchor: AnchorEntity, cameraTransform: simd_float4x4) -> Float {
963
1002
  // Extract the position of the anchor and the camera from their transforms, ignoring the y-axis
964
- let anchorTransform = anchor.transformMatrix(relativeTo: nil)
1003
+ let anchorTransform = anchor.transform.matrix
965
1004
  let anchorPositionXZ = simd_float2(anchor.transform.translation.x, anchor.transform.translation.z)
966
1005
  let relativeCameraPositionXZ = simd_float2(cameraTransform.columns.3.x, cameraTransform.columns.3.z)
967
1006
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "replate-camera",
3
- "version": "0.1.63",
3
+ "version": "0.1.65",
4
4
  "description": "Camera component for Replate Manager",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",