using NSOTSController;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OTSSysMgrTools
{
public class ImageDispose
{
public ImageDispose()
{
}
#region 通过byte数组生成BMP图像文件
///
/// 将一个byte的数组转换为8bit灰度位图
///
/// 数组
/// 图像宽度
/// 图像高度
/// 位图
public static Bitmap ToGrayBitmap(byte[] data, int width, int height)
{
//// 申请目标位图的变量,并将其内存区域锁定
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
//// BitmapData这部分内容 需要 using System.Drawing.Imaging;
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
//// 获取图像参数
// 扫描线的宽度
int stride = bmpData.Stride;
// 显示宽度与扫描线宽度的间隙
int offset = stride - width;
// 获取bmpData的内存起始位置
IntPtr iptr = bmpData.Scan0;
// 用stride宽度,表示这是内存区域的大小
int scanBytes = stride * height;
//// 下面把原始的显示大小字节数组转换为内存中实际存放的字节数组
int posScan = 0;
int posReal = 0;// 分别设置两个位置指针,指向源数组和目标数组
byte[] pixelValues = new byte[scanBytes]; //为目标数组分配内存
//for (int x = height-1;x>=0 ; x--) data[startIndex+ y];//
for (int x = 0; x < height; x++)
{
int startIndex = x * width;
//// 下面的循环节是模拟行扫描
for (int y = 0; y < width; y++)
{
pixelValues[posScan++] = data[posReal++];
}
posScan += offset; //行扫描结束,要将目标位置指针移过那段“间隙”
}
//// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes);
bmp.UnlockBits(bmpData); // 解锁内存区域
//// 下面的代码是为了修改生成位图的索引表,从伪彩修改为灰度
ColorPalette tempPalette;
using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
{
tempPalette = tempBmp.Palette;
}
for (int i = 0; i < 256; i++)
{
tempPalette.Entries[i] = Color.FromArgb(i, i, i);
}
bmp.Palette = tempPalette;
//// 算法到此结束,返回结果
return bmp;
}
#endregion
#region 将文件转换成byte[] 数组
///
/// 将文件转换成byte[] 数组
///
/// 文件路径文件名称
/// byte[]
public static byte[] GetFileData(string fileUrl)
{
FileStream pFileStream = null;
byte[] pReadByte = new byte[0];
try
{
//读取文本文件
pFileStream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read);
//实例读取字节流对象
StreamReader sr = new StreamReader(pFileStream, System.Text.Encoding.Default);
string temp = sr.ReadToEnd();
//转换byte数组
byte[] byteArray = System.Text.Encoding.Default.GetBytes(temp);
//生成字符数组
string[] tempGroup = temp.Split(',');
//关闭读取对象
sr.Close();
string AppendStr = string.Empty;
for (int i = 0; i < tempGroup.Length; i++)
{
//十进制转十六进制
AppendStr += Convert.ToString(Convert.ToInt32(tempGroup[i]), 16) + " ";
}
//替换字符符号
AppendStr = AppendStr.Replace(" ", "");
if ((AppendStr.Length % 2) != 0)
{
AppendStr += " ";
}
byte[] returnBytes = new byte[AppendStr.Length / 2];
//转换为byte[]数组
for (int i = 0; i < returnBytes.Length; i++)
{
returnBytes[i] = Convert.ToByte(AppendStr.Substring(i * 2, 2), 16);
}
return returnBytes;
}
catch
{
return pReadByte;
}
finally
{
if (pFileStream != null)
pFileStream.Close();
}
}
#endregion
#region 根据分辨率获取图像
public static bool GetScanImage(string ImgWidth, string ImgHeight, string DwellTime, COTSControlFunExport cfun, ref byte[] ImageByte)
{
try
{
//设置图像分辨率
int width = 0;
int height = 0;
//获取宽度
width = Convert.ToInt32(ImgWidth);
height = Convert.ToInt32(ImgHeight);
int a_ExternalMode = 0;
//获取终止模式
a_ExternalMode = cfun.GetSemExternalMode();
//保存初始模式变量
int a_oldMode = 0;
//获取初始模式
if (!cfun.GetSemScanMode(ref a_oldMode))
{
return false;
}
//设置当前模式
if (!cfun.SetSemScanMode(a_ExternalMode))
{
return false;
}
#region BeamBlank
int a_nBeamBlank = 0;
//获取参数
if (!cfun.GetSemBeamBlank(ref a_nBeamBlank))
{
cfun.SetSemScanMode(a_oldMode);
return false;
}
//设置参数
if (!cfun.SetSemBeamBlank(0))
{
cfun.SetSemScanMode(a_oldMode);
return false;
}
#endregion
#region 获得放大倍数
//获得放大倍数
double a_dMagnification = 0;
//获取参数
if (!cfun.GetSemMagnification(ref a_dMagnification))
{
cfun.SetSemScanMode(a_oldMode);
return false;
}
#endregion
#region 获取 电镜 X、Y轴 与角度
//获取 电镜 X、Y轴 与角度
double PositionX = 0;
double PositionY = 0;
double PositionR = 0;
//获取参数
if (!cfun.GetSemPositionXY(ref PositionX, ref PositionY, ref PositionR))
{
cfun.SetSemScanMode(a_oldMode);
return false;
}
#endregion
#region 设置图像分辨率
//设置宽度
if (!cfun.SetImageSize(width, height))
{
cfun.SetSemScanMode(a_oldMode);
return false;
}
#endregion
#region 采集时间
//采集时间
int nDwellTime = 16;
nDwellTime = Convert.ToInt32(DwellTime);
//nDwellTime = Convert.ToInt32(tbCollectionTime.Text);
//设置采集时间
if (!cfun.SetDwellTime(nDwellTime))
{
cfun.SetSemScanMode(a_oldMode);
return false;
}
#endregion
#region MatrixSize
//获得放大倍数
int a_MatrixSize = 0;
Size size = new Size();
//获取参数
size = cfun.GetMatrixSize(a_MatrixSize);
#endregion
//获取图像数据
int resultCount = width * height;
int GetImgCount = cfun.AcquireBSEImage(0, 0, 0, ref ImageByte);
if (resultCount == GetImgCount)
{
//设置为原始 扫描模式
cfun.SetSemScanMode(a_oldMode);
}
else
{
cfun.SetSemScanMode(a_oldMode);
}
return true;
}
catch (Exception ex)
{
//记录日志
//NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
// log.Error(ex.Message.ToString());
return false;
}
}
#endregion
public static int GetScanImage(int iWidth, int iHeigh, string DwellTime, ref byte[] bImageData)
{
//电镜设置对象
COTSControlFunExport cfun = new COTSControlFunExport();
int GetImgCount = 0;
try
{
//连接电镜
bool IsConnec = cfun.ConncetSem();
if (!IsConnec)
{
return 0;
}
//实例电镜初始化
bool IsScan = cfun.ScanInit();
if (!IsScan)
{
return 0;
}
int a_ExternalMode = 0;
//获取终止模式
a_ExternalMode = cfun.GetSemExternalMode();
//保存初始模式变量
int a_oldMode = 0;
//获取初始模式
if (!cfun.GetSemScanMode(ref a_oldMode))
{
return 0;
}
//设置当前模式
if (!cfun.SetSemScanMode(a_ExternalMode))
{
return 0;
}
#region BeamBlank
int a_nBeamBlank = 0;
//获取参数
if (!cfun.GetSemBeamBlank(ref a_nBeamBlank))
{
cfun.SetSemScanMode(a_oldMode);
return 0;
}
//设置参数
if (!cfun.SetSemBeamBlank(0))
{
cfun.SetSemScanMode(a_oldMode);
return 0;
}
#endregion
#region 获得放大倍数
//获得放大倍数
double a_dMagnification = 0;
//m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification-----begin------");
//获取参数
if (!cfun.GetSemMagnification(ref a_dMagnification))
{
cfun.SetSemScanMode(a_oldMode);
return 0;
}
//m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification_a_dMagnification:" + a_dMagnification + "------");
//m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification-----end------");
#endregion
#region 获取 电镜 X、Y轴 与角度
//获取 电镜 X、Y轴 与角度
double PositionX = 0;
double PositionY = 0;
double PositionR = 0;
//获取参数
if (!cfun.GetSemPositionXY(ref PositionX, ref PositionY, ref PositionR))
{
cfun.SetSemScanMode(a_oldMode);
return 0;
}
#endregion
#region 设置图像分辨率
//设置宽度
if (!cfun.SetImageSize(iWidth, iHeigh))
{
cfun.SetSemScanMode(a_oldMode);
return 0;
}
#endregion
#region 采集时间
//采集时间
int nDwellTime = Convert.ToInt32(DwellTime);
//设置采集时间
if (!cfun.SetDwellTime(nDwellTime))
{
cfun.SetSemScanMode(a_oldMode);
return 0;
}
#endregion
#region MatrixSize
//获得放大倍数
int a_MatrixSize = 0;
Size size = new Size();
//获取参数
size = cfun.GetMatrixSize(a_MatrixSize);
#endregion
//获取图像数据
int resultCount = iWidth * iHeigh;
GetImgCount = cfun.AcquireBSEImage(0, 0, 0, ref bImageData);
//记录日志
if (resultCount == GetImgCount)
{
//设置为原始 扫描模式
cfun.SetSemScanMode(a_oldMode);
//Scan类结束
//初始化电镜参数
if (!cfun.SetAndStartScan())
{
return 0;
}
}
else
{
cfun.SetSemScanMode(a_oldMode);
//记录日志
//初始化电镜参数
if (!cfun.SetAndStartScan())
{
return 0;
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
cfun.ScanFinishedInstance();
cfun.DisConnectSem();
//cfun.FreeDll();
}
return GetImgCount;
}
///
/// 将byte数组转换为文件并保存到指定地址
///
/// byte数组
/// 保存地址
public static void BytesConvertToFile(byte[] buff, string savepath, string fileName)
{
try
{
//如果不存在就创建Enclosure文件夹
if (Directory.Exists(savepath + @"\Enclosure\") == false)
{
Directory.CreateDirectory(savepath + @"\Enclosure\");
}
if (System.IO.File.Exists(savepath + @"\Enclosure\" + fileName))
{
System.IO.File.Delete(savepath + @"\Enclosure\" + fileName);
}
Bitmap bitmap=ToGrayBitmap(buff, 1024, 768);
bitmap.Save(savepath + @"\Enclosure\" + fileName);
}
catch (Exception)
{
}
}
}
}