2016-04-07 9 views
2

Ham bir görüntü verisini almak için iphone'un kamerasını kullanmak istiyorum ve daha sonra işlemek için dizüstü bilgisayardaki matlab veya opencv'i kullanıyorum. Bu yüzden bu ham görüntü verilerini iphone'dan dizüstü bilgisayara almam gerekiyor.Bir BGRA resmini iphone'a nasıl kaydederim ve daha sonra işlem sonrası işlem için bir dizüstü bilgisayara götürürüm

Şu anda, 32BGRA görüntüsünü almak için AVFoudnation kullanıyorum (bu ham görüntü verisi, değil mi?). Ve ben onu bir dosya olarak saklarım ama onu iphone'dan alamıyorum. Bazı alakalı kodlar aşağıda verilmiştir (? jailbreak olmadan orada herhangi çözümler mı)

:

if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) { 
     videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait 
     stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in 
      if (sampleBuffer != nil) {         
       // get the raw image data 
       let cameraFrame = CMSampleBufferGetImageBuffer(sampleBuffer) 

       CVPixelBufferLockBaseAddress(cameraFrame!, 0) 

       let rawImage = CVPixelBufferGetBaseAddress(cameraFrame!) 
       let bytesPerRow = CVPixelBufferGetBytesPerRow(cameraFrame!) 
       let rawImageData = NSData.init(bytes: rawImage, length: bytesPerRow*CVPixelBufferGetHeight(cameraFrame!)) 

       // process the rawImageData, we now save it 
       var paths = NSSearchPathForDirectoriesInDomains (.DocumentDirectory, .UserDomainMask, true); 
       let documentsDirectory = paths[0] 
       let fileName = documentsDirectory.stringByAppendingString("/rawImageData")      

       do { 
        try rawImageData.writeToFile(fileName, options: NSDataWritingOptions.DataWritingFileProtectionComplete) 
       } catch { 

       } 
       print("Raw image data write successfully.") 
       print("Path of raw image data: ", fileName)     
       CVPixelBufferUnlockBaseAddress(cameraFrame!, 0)    
      } 
     }) 
    } 

Ben eğer öyleyse, dışarı almak daha sonra fotoğraf albümüne BGRA verileri kaydetmek ve gerekirse, ben nasıl yapabilirim Bu (UIImage kullanın?)

Teşekkürler.

bir güncelleme:

Ben UIImage için BGRA görüntüyü dönüştürmek ve sonra mayıs mac görüntü dosyasını almak için hava yardımı kullanın fotoğraf albümü kaydetmek için UIImageWriteToSavedPhotosAlbum kullanın. Ancak, resmin JPEG formatı olduğunu buldum. Sorun nedir?

  if (sampleBuffer != nil) {     
       let rawImage:UIImage = self.imageFromSampleBuffer(sampleBuffer) 
       self.capturedImage.image = rawImage 
       UIImageWriteToSavedPhotosAlbum(rawImage, nil, nil, nil)      
       print("Raw Image write to photo album successfully") 
      } 

// Create a UIImage from sample buffer data 
func imageFromSampleBuffer(sampleBuffer : CMSampleBufferRef) -> UIImage 
{ 
// Get a CMSampleBuffer's Core Video image buffer for the media data 
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
// Lock the base address of the pixel buffer 
CVPixelBufferLockBaseAddress(imageBuffer!, 0); 


// Get the number of bytes per row for the pixel buffer 
let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer!); 

// Get the number of bytes per row for the pixel buffer 
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!); 
// Get the pixel buffer width and height 
let width = CVPixelBufferGetWidth(imageBuffer!); 
let height = CVPixelBufferGetHeight(imageBuffer!); 

// Create a device-dependent RGB color space 
let colorSpace = CGColorSpaceCreateDeviceRGB(); 

// Create a bitmap graphics context with the sample buffer data 
var bitmapInfo: UInt32 = CGBitmapInfo.ByteOrder32Big.rawValue 
bitmapInfo |= CGImageAlphaInfo.PremultipliedLast.rawValue & CGBitmapInfo.AlphaInfoMask.rawValue 
let context = CGBitmapContextCreate(baseAddress, width, height, 8, 
bytesPerRow, colorSpace, bitmapInfo); 
// Create a Quartz image from the pixel data in the bitmap graphics context 
let quartzImage = CGBitmapContextCreateImage(context); 
// Unlock the pixel buffer 
CVPixelBufferUnlockBaseAddress(imageBuffer!,0); 

// Create an image object from the Quartz image 
let image = UIImage.init(CGImage: quartzImage!); 

return (image); 
} 
+0

MATLAB için hangi formatta kullanmak istersiniz? MATLAB herhangi bir görüntüyü içe aktaramaz mı? – jtbandes

+0

Matlab, BGRA görüntüsünü içe aktaramaz, ham verileri iphone'da bmp olarak kaydedebilir ve dizüstü bilgisayara çıkarabilir miyim? –

cevap

1

ben bir çözüm bulmak: Aşağıdaki kodlardır UIImage için BGRA dönüştürmek ve PNG, sonra fotoğraf albümüne kaydedin. PNG kayıpsız bir sıkıştırma görüntü formatı olduğu için bmp görüntüsünü de aldığım anlamına gelir.

   let rawImage:UIImage = self.imageFromSampleBuffer(sampleBuffer)      
       let img = UIImagePNGRepresentation(rawImage) 
       let pngImg = UIImage.init(data: img!)          
       self.capturedImage.image = pngImg 
       UIImageWriteToSavedPhotosAlbum(pngImg!, nil, nil, nil)     
       print("PNG Image write to photo album successfully") 
İlgili konular