4

I am trying to get the colour in RGB when a user taps on a Point on the camera view. My approach is as follows: However, I only get 0,0,0,0 even when I tap on a brighter section of the camera feed view. The code where I use to print is print(color).

I think the colours are given in sRGB.

UPDATE : The output I get when I print

[ (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 0 0 0 0 )

How can I solve this?

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        if let point = touch?.location(in: view) {

            let color:CGColor = self.colorOfPoint(point: point)
            print(color)
        }
    }


        func colorOfPoint(point:CGPoint) -> CGColor {

            let pixel = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: 4)

            let colorSpace = CGColorSpaceCreateDeviceRGB()
            let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)

            let context = CGContext(data: pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)

            context!.translateBy(x: -point.x, y: -point.y)



            self.previewLayer.render(in: context!)

            let red: CGFloat   = CGFloat(pixel[0]) / 255.0
            let green: CGFloat = CGFloat(pixel[1]) / 255.0
            let blue: CGFloat  = CGFloat(pixel[2]) / 255.0
            let alpha: CGFloat = CGFloat(pixel[3]) / 255.0

            let color = UIColor(red:red, green: green, blue:blue, alpha:alpha)



            return color.cgColor
        }

UPDATE :2

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {

    let captureSession = AVCaptureSession()
    var previewLayer:CALayer!

    let dataOutput = AVCaptureVideoDataOutput()

    var captureDevice:AVCaptureDevice!

    var takePhoto = false

    override func viewDidLoad() {
        super.viewDidLoad()

        if let availableDevices = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .back).devices {
                captureDevice = availableDevices.first
                beginSession()
            }

do {
            let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice)

            captureSession.addInput(captureDeviceInput)

        }catch {
            print(error.localizedDescription)
        }


        if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) {
            self.previewLayer = previewLayer
            self.view.layer.addSublayer(self.previewLayer)
            self.previewLayer.frame = self.view.layer.frame
            captureSession.startRunning()


            dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString):NSNumber(value:kCVPixelFormatType_32BGRA)]

            dataOutput.alwaysDiscardsLateVideoFrames = true

            if captureSession.canAddOutput(dataOutput) {
                captureSession.addOutput(dataOutput)
            }

            captureSession.commitConfiguration()


            let queue = DispatchQueue(label: "com.brianadvent.captureQueue")
            dataOutput.setSampleBufferDelegate(self, queue: queue)



        }

     ...

    }
Illep
  • 16,375
  • 46
  • 171
  • 302
  • Do the `pixel` values coming out of `CGContext(data:...)` by any chance come back as numbers between 0 and 1? If so you shouldn't divide them by `255.0` before putting them in `UIColor`. – Paolo Aug 30 '17 at 19:12
  • Like how. Can you show me an example. – Illep Aug 30 '17 at 19:14
  • Try `let red: CGFloat = CGFloat(pixel[0])` instead of `let red: CGFloat = CGFloat(pixel[0]) / 255.0`. Likewise for the other components. – Paolo Aug 30 '17 at 19:15
  • Still its the same. I still get ` [ (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 0 0 0 0 ) ` – Illep Aug 30 '17 at 19:18
  • What about changing the `translateBy` values to positive? I'm not sure what gets translated with that function, the coordinates of the context or the contents of the context (after you do `render`). – Paolo Aug 30 '17 at 19:31
  • I tried removing the `-` sign but it still gives the same result. Help :( – Illep Aug 30 '17 at 19:37
  • what is this `previewLayer`? – Vini App Aug 30 '17 at 23:01
  • @ViniApp I have updated my code. see Update-2. `previewLayer` is a CALayer. – Illep Aug 31 '17 at 01:04
  • Please check this might be helpful https://stackoverflow.com/questions/32642262/most-efficient-realtime-way-to-get-pixel-values-from-ios-camera-feed-in-swift – Vini App Aug 31 '17 at 01:17
  • When I try with previewLayer its giving me 0 everytime. When I tried it on self.view.layer its giving me the correct values. – Vini App Aug 31 '17 at 01:18
  • @ViniApp I still get 0. Can you show me your code. – Illep Aug 31 '17 at 01:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153308/discussion-between-vini-app-and-illep). – Vini App Aug 31 '17 at 01:39

0 Answers0