2013-01-01 3 views
0

Kinect가 이미지를 가져 와서 사용자가 지정한 위치에 저장하는 프로그램이 있습니다. 나는 그것이 다른 종류의 이미지를 저장하기 위해 더 많은 폴더를 생성하기 때문에 프로그램이 올바른 폴더를 찾는다는 것과 그 폴더가 생성된다는 것을 안다. 이미지를 저장하기위한 내 현재 코드 (아래)는 다른 프로그램에서도 작동하므로, 인식하지 못하는 부분이 멈추고 있습니까? 미리 감사드립니다. 이미지 경로내 프로그램이 이미지를 저장하지 않는 이유는 무엇입니까?

결정

using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) 
{ 
    if (colorFrame == null) 
    { 
     return; 
    } 

    byte[] pixels = new byte[sensor.ColorStream.FramePixelDataLength]; 

    //WriteableBitmap image = new WriteableBitmap(
    //  sensor.ColorStream.FrameWidth, 
    //  sensor.ColorStream.FrameHeight, 96, 96, 
    //  PixelFormats.Bgra32, null); 

    colorFrame.CopyPixelDataTo(pixels); 

    colorImage.WritePixels(new Int32Rect(0, 0, colorImage.PixelWidth, 
             colorImage.PixelHeight), 
          pixels, colorImage.PixelWidth * 4, 0); 
    //BitmapSource image = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 
    //           96, 96, PixelFormats.Bgr32, null, 
    //           pixels, colorFrame.Width * 4); 

    //image.WritePixels(new Int32Rect(0, 0, image.PixelWidth, image.PixelHeight), 
    //     pixels, image.PixelWidth * sizeof(int), 0); 

    //video.Source = image; 

    totalFrames++; 
    BitmapEncoder encoder = new JpegBitmapEncoder(); 

    encoder.Frames.Add(BitmapFrame.Create(colorImage)); 

    //path = System.IO.Path.Combine("C:/", "Kinected", "Images"); 

    if (PersonDetected == true) 
    { 
     if (totalFrames % 10 == 0) 
     { 
      if (file_name != null && colorImage != null) 
      { 
       try 
       { 
        using (FileStream fs = new FileStream(colorPath + 
          @"\Kinected Image " + time + ".jpg", FileMode.Create)) 
        { 
         encoder.Save(fs); 
        } 
       } 
       catch (IOException) 
       { 
        System.Windows.MessageBox.Show("Save Failed"); 
       } 
      } 
     } 

     skeletonDeLbl.Content = "Skeleton Detected!"; 
    } 

    if (PersonDetected == false) skeletonDeLbl.Content = "No Skeleton Detected."; 
} 

저장

FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "Select which folder you want Kinected to keep all of its information/images in."; DialogResult result = dialog.ShowDialog(); colorPath = dialog.SelectedPath + @"\Color Images"; depthPath = dialog.SelectedPath + @"\Depth Images"; facePath = dialog.SelectedPath + @"\Face Data"; if (!Directory.Exists(colorPath)) Directory.CreateDirectory(colorPath); if (!Directory.Exists(depthPath)) Directory.CreateDirectory(depthPath); if (!Directory.Exists(facePath)) Directory.CreateDirectory(facePath); System.Windows.MessageBox.Show(colorPath); 

편집

file_name 그냥 null입니다 밝혀,하지만 지금은 오류 때 무엇입니까 그것은 line using (FileStream fs = new FilesStream(file_name, FileMode.Create)) 그 내용 :

An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll 

Additional information: The given path's format is not supported. 

왜 이런 일이 발생합니까? Microsoft의 데모와 똑같은 코드를 사용하고 있습니다. 감사.

+4

"저장하지 않음"은 무엇을 의미합니까? 오류가 있습니까? 모든 파일 끝에는 파일이 없지만 잘 작동하는 것 같습니까? 파이 먹을 때 케이크 먹을거야? –

+1

'PersonDetected && totalFrames % 10 == 0 && file_name! = null && colorImage! = null' 조건을 모두 만족합니까? 저장 코드가 전혀 실행되지 않습니까? 디버거를 사용하여 문제를 추적하십시오. –

+0

@lc. 끝 부분에 파일이 없습니다. – Kinected

답변

4

당신은

colorPath = System.IO.Path.Combine(dialog.SelectedPath, "Color Images"); 

Combine 방법은 추가하거나 필요한 경우 백 슬래시를 제거하는 돌봐 경로로 문자열을 결합하는 다음 코드를 사용합니다.


디버거를 사용하는 것을 잊지 마십시오. 중단 점을 설정하고 변수를 검사하고 더 많은 일을 할 수 있습니다.

디버거가 가장 친한 친구입니다!


UPDATE는

은 또한 파일 이름에 잘못된 문자를 사용하고 있습니다. 이 방법은 유효하지 않은 문자를 대체하고 파일 이름에 다른 수정 사항을 적용합니다.

/// <summary> 
/// Replaces invalid characters in a file name by " ". Apply only to the filename.ext 
/// part, not to the path part. 
/// </summary> 
/// <param name="fileName">A file name (not containing the path part) possibly 
/// containing invalid characters.</param> 
/// <returns>A valid file name.</returns> 
public static string GetValidFileName(string fileName) 
{ 
    string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars())); 
    string s = Regex.Replace(fileName, "[" + invalidChars + "]", " "); 
    s = Regex.Replace(s, @"\s\s+", " "); // Replace multiple spaces by one space. 
    string fil = Path.GetFileNameWithoutExtension(s).Trim().Trim('.'); 
    string ext = Path.GetExtension(s).Trim().Trim('.'); 
    if (ext != "") { 
     fil += "." + ext; 
    } 
    fil = fil.Replace(" .", "."); 
    return fil == "." ? "" : fil; 
} 
+0

같은 줄에 여전히 같은 오류가 있습니다. – Kinected

+0

경로 ('file_name')가 정확히 어떻게 보이나요? –

+0

C : \\ 사용자 \\ Liam \\ 그림 \ Kinect-Ed \\ 컬러 이미지 \\ Kinected Image @ 1/1/2013 4:00:30 – Kinected

관련 문제