c#pda(wince)摄像头拍照程序
拍照时经常报内存溢出的错误,没有找到原因
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using Microsoft.Win32;
using System.Diagnostics;
namespace Camera300
{
public class Camera
{
//打开设备
[DllImport("CameraDll.dll", EntryPoint = "CIS_OpenDevice", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CIS_OpenDevice();
//关闭设备
[DllImport("CameraDll.dll", EntryPoint = "CIS_CloseDevice", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern void CIS_CloseDevice();
//设置自动调节
[DllImport("CameraDll.dll", EntryPoint = "CIS_SetAuto", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern void CIS_SetAuto(bool f);
//设置保存图片大小
[DllImport("CameraDll.dll", EntryPoint = "CIS_SetImageSize", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CIS_SetImageSize(byte bMode);
//得到保存图片大小
[DllImport("CameraDll.dll", EntryPoint = "CIS_GetImageSize", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int CIS_GetImageSize();
//得到是否全屏
[DllImport("CameraDll.dll", EntryPoint = "CIS_GetIsAuto", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CIS_GetIsAuto();
//增益增
[DllImport("CameraDll.dll", EntryPoint = "CIS_UpGainLight", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int CIS_UpGainLight(int nBit);
//增益减
[DllImport("CameraDll.dll", EntryPoint = "CIS_DownGainLight", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int CIS_DownGainLight(int nBit);
//设置增益
[DllImport("CameraDll.dll", EntryPoint = "CIS_SetGainLight", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int CIS_SetGainLight(int nBit);
//旋屏
[DllImport("CameraDll.dll", EntryPoint = "CIS_Rotate", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern void CIS_Rotate();
//暂停显示
[DllImport("CameraDll.dll", EntryPoint = "CIS_Pause", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern void CIS_Pause();
//开始
[DllImport("CameraDll.dll", EntryPoint = "CIS_Play", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern void CIS_Play();
//设置是否全屏显示
[DllImport("CameraDll.dll", EntryPoint = "CIS_SetFullScreen", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern void CIS_SetFullScreen(int nFull);
//拍照
[DllImport("CameraDll.dll", EntryPoint = "CIS_Photograph", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern void CIS_Photograph(IntPtr lpBuffer);
//设置显示位置
[DllImport("CameraDll.dll", EntryPoint = "CIS_SetPreview", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern void CIS_SetPreview(int nX, int nY, int nPreviewWidth, int nPreviewHeight);
//打开电源
[DllImport("CameraDll.dll", EntryPoint = "CIS_UP", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern void CIS_UP();
//断开电源
[DllImport("CameraDll.dll", EntryPoint = "CIS_DOWN", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern void CIS_DOWN();
[DllImport("coredll.dll")]
private static extern Int32 PlaySound(string strfile, uint hmod, playMode fdwSound);
//本来是想用C#里得函数来播放的,看看好像那个名空间在windowce上不存在,就用系统API吧,他们最终调用的韩式这个函数
enum playMode
{
SND_ALIAS = 0x00010000, // name is a WIN.INI [sounds] entry
SND_ASYNC = 0x00000001, // play asynchronously
SND_FILENAME = 0x00020000, // name is a file name
SND_LOOP = 0x00000008, // loop the sound until next sndPlaySound
SND_MEMORY = 0x00000004, // lpszSoundName points to a memory file
SND_NODEFAULT = 0x00000002, // silence not default, if sound not found
SND_NOSTOP = 0x00000010, // don't stop any currently playing sound
SND_NOWAIT = 0x00002000, // don't wait if the driver is busy
SND_RESOURCE = 0x00040004, // name is a resource name or atom
SND_SYNC = 0x00000000 // play synchronously (default)
}
static int nMode = 0, g_nWidth, g_nHeight;
// static int g_nX = 7, g_nY = 27, g_nPreviewWidth = 240, g_nPreviewHeight = 180;
static bool g_fIsFull = false, g_fIsAuto = true;
//static string g_strFileType = ".jpg";
static fileTYPE filetype = fileTYPE.JPG;
static string strforder = "\\ResidentFlash\\", g_strSoundPath = "";
//MenuItem g_menuNew = new MenuItem();
long m_totle = GC.GetTotalMemory(true);
public enum fileTYPE
{
BMP = 1,
GIF,
JPG,
PNG
};
public event System.EventHandler ShootEnd;
public int isOpen;
public bool AutoAdjust
{
get { g_fIsAuto = CIS_GetIsAuto();
return g_fIsAuto; }
set { g_fIsAuto = value;
CIS_SetAuto(value); }
}
public bool FullScreen
{
get { return g_fIsFull; }
set { g_fIsFull = value; if (value) { CIS_SetFullScreen(1); } else { CIS_SetFullScreen(0); } }
}
public int ImageMode
{
get { return nMode; }
set { nMode = value; GetSize(nMode);
CIS_SetImageSize((byte)nMode);
}
}
public string Folder
{
get { return strforder; }
set
{
strforder = value;
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey("Drivers\\BuiltIn\\Camera", true);
key.SetValue("Path", strforder);
key.Close();
}
catch
{
}
}
}
public fileTYPE FileType
{
get { return filetype; }
set { filetype = value; }
}
public int Gain
{
set { if ((!g_fIsAuto) && (value > -4) && (value < 7)) { CIS_SetGainLight(value + 3); } }
}
public bool Play
{
set
{
if (value)
{
CIS_Play();
isOpen = 1;
}
else
{
CIS_Pause();
isOpen = 0;
}
}
}
public Point LeftTop
{
set { CIS_SetPreview(value.X, value.Y, 0, 0); }
}
/// <summary>
/// 播放声音的位置
/// </summary>
public static string soundfile
{
set { g_strSoundPath = value; }
get { return g_strSoundPath; }
}
public Camera()
{
//if (!CIS_InitDevice())
//{
// MessageBox.Show("jj");
//}
}
public bool Open()
{
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey("Drivers\\BuiltIn\\Camera");
string str = (string)key.GetValue("Path");
if (str != null)
{
strforder = str;
}
else
{
try
{
Directory.CreateDirectory("\\ResidentFlash\\照片");
}
catch
{
}
strforder = "\\ResidentFlash\\照片\\";
}
key.Close();
}
catch
{
try
{
Directory.CreateDirectory("\\ResidentFlash\\照片");
}
catch
{
}
strforder = "\\ResidentFlash\\照片\\";
}
try
{
RegistryKey keyPath = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Apps\\FFT Camera");
g_strSoundPath = (string)keyPath.GetValue("InstallDir");
keyPath.Close();
if (g_strSoundPath != "")
{
g_strSoundPath += "\\shuttersound.wav";
}
}
catch
{
}
CIS_OpenDevice();
CIS_SetAuto(true);
nMode = CIS_GetImageSize();
nMode = GetSize(nMode);
CIS_SetImageSize((byte)nMode);
CIS_UP();
CIS_SetPreview(7, 33, 228, 171);
isOpen = 1;
return false;
}
public bool Open(int nX, int nY)
{
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey("Drivers\\BuiltIn\\Camera");
string str = (string)key.GetValue("Path");
if (str != null)
{
strforder = str;
}
else
{
try
{
Directory.CreateDirectory("\\ResidentFlash\\照片");
}
catch
{
}
strforder = "\\ResidentFlash\\照片\\";
}
key.Close();
}
catch
{
try
{
Directory.CreateDirectory("\\ResidentFlash\\照片");
}
catch
{
}
strforder = "\\ResidentFlash\\照片\\";
}
try
{
RegistryKey keyPath = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Apps\\FFT Camera");
g_strSoundPath = (string)keyPath.GetValue("InstallDir");
keyPath.Close();
if (g_strSoundPath != "")
{
g_strSoundPath += "\\shuttersound.wav";
}
}
catch
{
}
CIS_OpenDevice();
CIS_SetAuto(true);
nMode = CIS_GetImageSize();
nMode = GetSize(nMode);
CIS_SetImageSize((byte)nMode);
CIS_UP();
CIS_SetPreview(nX, nY, 228, 171);
isOpen = 1;
return false;
}
public void Pause()
{
CIS_Pause();
isOpen = 0;
}
public void Close()
{
CIS_DOWN();
CIS_Pause();
CIS_CloseDevice();
isOpen = 0;
}
public void Photo()
{
Thread th = new Thread(PhotoT);
th.Start();
}
void PhotoT()
{
try
{
Bitmap bm = new Bitmap(g_nWidth, g_nHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Rectangle rect = new Rectangle(0, 0, bm.Width, bm.Height);
BitmapData bmdata = bm.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
CIS_Photograph(bmdata.Scan0);
SaveFile(bm, filetype);
bm.Dispose();
CIS_Pause();
isOpen = 0;
// GC.Collect();
}
catch (OutOfMemoryException cw)
{
CIS_Pause();
isOpen = 0;
MessageBox.Show("没有足够内存运行程序\r\n"+cw.Message, "PhotoT");
}
catch (Exception cw)
{
CIS_Pause();
MessageBox.Show(cw.Message, "PhotoT");
//MessageBox.Show("Memory for failure, please expand the program can use memory!");
CIS_Play();
}
PlaySound(g_strSoundPath, 0, playMode.SND_ASYNC | playMode.SND_FILENAME);
if (this.ShootEnd != null)
this.ShootEnd(this, null);
}
public void Roate()
{
CIS_Rotate();
}
private string sfilename;
public string fileName
{
get { return sfilename; }
set { sfilename = value; }
}
private string gfilename;
/// <summary>
/// 保存时的文件名
/// </summary>
public string fileNmae1
{
set { gfilename = value; }
get { return gfilename; }
}
private void SaveFile(Bitmap bmpIn, fileTYPE type)
{
DateTime dt = DateTime.Now;
if (!Directory.Exists(strforder))
{
Directory.CreateDirectory(strforder);
}
try
{
string str, rname;
if (string.IsNullOrEmpty(gfilename) == true)
{
rname = string.Format("0}-{1}-{2}-{3}-{4}-{5}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
}
else
rname = gfilename;
switch (type)
{
case fileTYPE.BMP:
str = string.Format("{1}{0}.bmp", gfilename, strforder);
bmpIn.Save(str, ImageFormat.Bmp);
break;
case fileTYPE.GIF:
str = string.Format("{1}{0}.gif", gfilename, strforder);
bmpIn.Save(str, ImageFormat.Gif);
break;
case fileTYPE.JPG:
str = string.Format("{1}{0}.jpg", gfilename, strforder);
bmpIn.Save(str, ImageFormat.Jpeg);
break;
case fileTYPE.PNG:
str = string.Format("{1}{0}.png", gfilename, strforder);
bmpIn.Save(str, ImageFormat.Png);
break;
default:
str = string.Format("{1}{0}.jpg", gfilename, strforder);
bmpIn.Save(str, ImageFormat.Jpeg);
break;
}
sfilename = str;
//MessageBox.Show(str);
}
catch (OutOfMemoryException cw)
{
if (cw.InnerException != null)
MessageBox.Show("内存溢出错误!\r\n" + cw.InnerException.Message, "SaveFile");
else
MessageBox.Show("内存溢出错误!\r\n" + cw.Message, "SaveFile");
}
}
public int GetSize(int nMode)
{
switch (nMode)
{
case 0:
g_nWidth = 320;
g_nHeight = 240;
break;
case 1:
g_nWidth = 640;
g_nHeight = 480;
break;
case 2:
g_nWidth = 800;
g_nHeight = 600;
break;
case 3:
g_nWidth = 1024;
g_nHeight = 768;
break;
default:
g_nWidth = 640;
g_nHeight = 480;
nMode = 1;
break;
}
return nMode;
}
}
}
posted on 2010-07-02 07:58 gds通用软件开发系统 阅读(2427) 评论(0) 编辑 收藏 举报