1

我需要通过从 dll Aygshell.dll 中调用 SHCameraCapture 来在紧凑的框架 3.7 应用程序中显示相机捕捉对话框。由于我正在使用的技术的限制,我无法使用托管对象 CameraCaptureDialog。相反,我需要通过 Pinvoking 来访问它。

有关该功能的文档,请参阅http://msdn.microsoft.com/en-us/library/aa454995.aspx。该函数接受一个定义对话框参数的结构。例如在哪里保存文件,使用什么分辨率。

我会想象我必须在 C# 中定义结构的副本并使用属性 StructLayout 装饰结构。我还想象代码将涉及 [DllImport("aygshell.dll")]。任何有关如何调用它的示例代码将不胜感激。

4

3 回答 3

2

此代码有效....

#region Enumerations

public enum CAMERACAPTURE_STILLQUALITY
{
    CAMERACAPTURE_STILLQUALITY_DEFAULT = 0,
    CAMERACAPTURE_STILLQUALITY_LOW = 1,
    CAMERACAPTURE_STILLQUALITY_NORMAL = 2,
    CAMERACAPTURE_STILLQUALITY_HIGH = 3
}
public enum CAMERACAPTURE_VIDEOTYPES
{
    CAMERACAPTURE_VIDEOTYPE_ALL = 0xFFFF,
    CAMERACAPTURE_VIDEOTYPE_STANDARD = 1,
    CAMERACAPTURE_VIDEOTYPE_MESSAGING = 2
}

public enum CAMERACAPTURE_MODE
{
    CAMERACAPTURE_MODE_STILL = 0,
    CAMERACAPTURE_MODE_VIDEOONLY = 1,
    CAMERACAPTURE_MODE_VIDEOWITHAUDIO = 2
}

#endregion //Enumerations

#region Structures

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public struct struSHCAMERACAPTURE
{
    public uint cbSize;
    public IntPtr hwndOwner;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
    public String szFile;
    [MarshalAs(UnmanagedType.LPTStr)]
    public String pszInitialDir; //LPCTSTR
    [MarshalAs(UnmanagedType.LPTStr)]
    public String pszDefaultFileName; //LPCTSTR
    [MarshalAs(UnmanagedType.LPTStr)]
    public String pszTitle;  //LPCTSTR
    public CAMERACAPTURE_STILLQUALITY StillQuality;
    public CAMERACAPTURE_VIDEOTYPES VideoTypes;
    public uint nResolutionWidth;
    public uint nResolutionHeight;
    public uint nVideoTimeLimit;
    public CAMERACAPTURE_MODE Mode;
}
#endregion //Structures

#region API
[DllImport("Aygshell.dll", SetLastError = true,CharSet=CharSet.Unicode)]
public static extern int SHCameraCapture
(
    ref struSHCAMERACAPTURE pshCamCapture
);

private string StartImager(String strImgDir, String strImgFile, uint uintImgHeight,
                            uint uintImgWidth)

try
{
  struSHCAMERACAPTURE shCamCapture = new struSHCAMERACAPTURE();

  shCamCapture.cbSize = (uint)Marshal.SizeOf(shCamCapture);
  shCamCapture.hwndOwner = IntPtr.Zero;
  shCamCapture.szFile = "\\" + strImgFile;  //strImgDir + "\\" + strImgFile;
  shCamCapture.pszInitialDir = "\\"; // strImgDir;
  shCamCapture.pszDefaultFileName = strImgFile;
  shCamCapture.pszTitle = "PTT Image Capture";
  shCamCapture.StillQuality = 0; // CAMERACAPTURE_STILLQUALITY.CAMERACAPTURE_STILLQUALITY_NORMAL;
  shCamCapture.VideoTypes = CAMERACAPTURE_VIDEOTYPES.CAMERACAPTURE_VIDEOTYPE_STANDARD;
  shCamCapture.nResolutionHeight = 0; // uintImgHeight;
  shCamCapture.nResolutionWidth = 0; // uintImgWidth;
  shCamCapture.nVideoTimeLimit = 10;
  shCamCapture.Mode = 0; // CAMERACAPTURE_MODE.CAMERACAPTURE_MODE_STILL;

  //IntPtr intptrCamCaptr = IntPtr.Zero;

  //Marshal.StructureToPtr(shCamCapture, intptrCamCaptr, true);

  int intResult = SHCameraCapture(ref shCamCapture);
  if (intResult != 0)
  {
      Win32Exception Win32 = new Win32Exception(intResult);
      MessageBox.Show("Error: " + Win32.Message);
  }             

  return strCaptrErr;
}

catch (Exception ex)
{
    MessageBox.Show("Error StartImager : " + ex.ToString() + " - " + strCaptrErr
                    , "Nomad Imager Test");
    return "";
}
于 2009-03-06T17:48:01.793 回答
1

Groky,这是一个好的开始...感谢您开始。我尝试将其连接起来,但得到了 NotSupportedException。

我已经粘贴了下面我的测试应用程序中的文本。请注意,我尝试使用 [StructLayout(LayoutKind.Sequential)] 装饰结构。我还公开了所有成员,以消除对象可访问性的任何问题。

public partial class Form1 : Form
{
    [DllImport("aygshell.dll")]
    static extern int SHCameraCapture(ref SHCAMERACAPTURE pshcc);

    [StructLayout(LayoutKind.Sequential)]
    struct SHCAMERACAPTURE
    {
        public Int32 cbSize;
        public IntPtr hwndOwner;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szFile;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pszInitialDir;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pszDefaultFileName;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pszTitle;
        public Int32 StillQuality;
        public Int32 VideoTypes;
        public Int32 nResolutionWidth;
        public Int32 nResolutionHeight;
        public Int32 nVideoTimeLimit;
        public Int32 Mode;
    }
    private void ShowCamera()
    {
        SHCAMERACAPTURE captureData = new SHCAMERACAPTURE
                                           {
                                               cbSize = sizeof (Int64),
                                               hwndOwner = (IntPtr)0,
                                               szFile = "\\My Documents",
                                               pszDefaultFileName = "picture.jpg",
                                               pszTitle = "Camera Demo",
                                               StillQuality = 0,
                                               VideoTypes = 1,
                                               nResolutionWidth = 480,
                                               nResolutionHeight = 640,
                                               nVideoTimeLimit = 0,
                                               Mode = 0
                                           };
        SHCameraCapture(ref captureData);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        ShowCamera();
    }
于 2008-09-26T15:35:52.493 回答
0

我无法对此进行测试,但您的结构/函数应该如下所示:

struct SHCAMERACAPTURE {
  public Int32 cbSize;
  public IntPtr hwndOwner;

  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]  
  public string szFile;

  [MarshalAs(UnmanagedType.LPStr)] 
  string pszInitialDir;

  [MarshalAs(UnmanagedType.LPStr)] 
  string pszDefaultFileName;

  [MarshalAs(UnmanagedType.LPStr)] 
  string pszTitle;

  Int32 StillQuality;
  Int32 VideoTypes;
  Int32 nResolutionWidth;
  Int32 nResolutionHeight;
  Int32 nVideoTimeLimit;
  Int32 Mode;
}

[DllImport("aygshell.dll")]
static extern int SHCameraCapture(ref SHCAMERACAPTURE pshcc);

AFAIK,您不需要在 SHCAMERCAPTURE 上显式设置 StructLayout,因为它的布局没有什么不寻常的。

一旦你得到这个工作,你可能想将你的发现发布到 pinvoke.net 以供其他人使用!

于 2008-09-26T09:08:11.263 回答