void CaptureClick() { try { if (IsAutoModeCamera) { TimerCamaraCapture.Stop(); } else if (buttonFlash.Enabled) { buttonFlash.Enabled = false; buttonFlash.Visible = false; } // 一個image不能同時被兩個PictureBox使用。切換的時候,先把先前使用的PictureBox.Image = null pictureBoxPreview.Image = null; // pictureBoxPreview.Image.Dispose(); pictureBoxPreview2.Image = null; // pictureBoxPreview2.Image.Dispose(); if (mHotTabCamera == null) { checkTestStatus("HotTab Camera is NULL!"); return; } IntPtr mIntPtr = IntPtr.Zero; Cursor.Current = Cursors.WaitCursor; // Release any previous buffer if (mIntPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(mIntPtr); mIntPtr = IntPtr.Zero; } // 亂數產生Snapshot所要使用的初始PictureBox應該要是哪一個 SwitchSnapshotPreview = RandomGenerator.Next(MinValue, MaxValue); // Capture Image mIntPtr = mHotTabCamera.Click(); // Stride : 指定一條掃描線 (Scan Line) 的開頭和下一條之間的位元組位移 (Offset). 這通常是像素格式的位元組數目 (例如, 16 位元/像素為 2) 乘以點陣圖寬度. 必須是四的倍數 Bitmap CapturePreview = new Bitmap(mHotTabCamera.Width, mHotTabCamera.Height, mHotTabCamera.Stride, PixelFormat.Format24bppRgb, mIntPtr); // If the image is upsidedown CapturePreview.RotateFlip(RotateFlipType.RotateNoneFlipY); // Snapshot截圖會亂數顯示在兩個Preview的PictureBox之間 if (SwitchSnapshotPreview % 2 == 0) { pictureBoxPreview.Image = CapturePreview; Clipboard.SetDataObject(pictureBoxPreview.Image); } else { pictureBoxPreview2.Image = CapturePreview; Clipboard.SetDataObject(pictureBoxPreview2.Image); } Clipboard.GetImage().Save("Preview.bmp", ImageFormat.Bmp); if (IsAutoModeCamera) { buttonPASS.Enabled = false; buttonFAIL.Enabled = false; CaptureDecodeData = CaptureDecoder.decode(CapturePreview); if (IsDebugMode) { Trace.WriteLine("ConfirmTagContextBarcode : " + ConfirmTagContextBarcode); Trace.WriteLine("Capture Barcode Decode : " + CaptureDecodeData); } if (buttonFlash.Text.Equals("ON") && (CaptureDecodeData.Contains(ConfirmTagContextBarcode.Substring(0, ConfirmTagContextBarcode.Length - 3)))) { checkTestStatus("Capture can be decoded in FLASH ON!"); } else if (buttonFlash.Text.Equals("ON")) { TimerCamaraCapture.Start(); } else if (CaptureDecodeData.Contains(ConfirmTagContextBarcode.Substring(0, ConfirmTagContextBarcode.Length - 3))) { TestNextCamera(); } else { checkTestStatus("Capture Barcode Decode FAIL!"); } } Cursor.Current = Cursors.Default; // Release any previous buffer if (mIntPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(mIntPtr); mIntPtr = IntPtr.Zero; } // 手動測項防呆機制(不讓產線沒有測試就直接按PASS/FAIL) buttonPreviewTop.Enabled = true; buttonPreviewBottom.Enabled = true; } catch (Exception ex) { Trace.WriteLine(ex.ToString()); ExceptionEvent(ex); } }
void checkTestStatus(String testResult) { if (IsAutoModeCamera) { TimerCamaraCapture.Stop(); } if (HotTabCamera.GetCameraCount() > 0) { if (mHotTabCamera != null) { mHotTabCamera.Flash(0); } if (Is8MCameraUsed) { Flash_8M(0); } else { SetFlashOff(); } } labelTestCameraName.Text = "None"; pictureBoxPreview.Image = null; pictureBoxPreview2.Image = null; buttonFAIL.Enabled = false; buttonFlash.Enabled = false; buttonCapture.Enabled = false; buttonPreviewTop.Enabled = false; buttonPreviewBottom.Enabled = false; FreeResources(); if (testResult.Equals("PASS")) { labelResult.ForeColor = Color.Green; labelResult.Text = "PASS"; result["result"] = "PASS"; result["EIPLog"] = new JObject { { "Camera", "PASS" }, { "Camera_Info", "PASS" } }; } else { labelResult.ForeColor = Color.Red; labelResult.Text = "FAIL"; result["result"] = "FAIL"; result["EIPLog"] = new JObject { { "Camera", "FAIL" }, { "Camera_Info", testResult } }; } File.WriteAllText(GetFullPath("result.json"), result.ToString()); Thread.Sleep(200); File.Create(GetFullPath("completed")); if (!ShowWindow) { Exit(); } }
public void InitializeCameraListDevice(int iDeviceNum, PictureBox box) { lock (thisLock) { if (IsDebugMode) { Trace.WriteLine("Init No." + iDeviceNum + " Camara : " + CameraList[iDeviceNum] + " by Thread " + Thread.CurrentThread.ManagedThreadId + " on " + System.DateTime.Now); } groupBoxTestButton.Enabled = false; try { if (mHotTabCamera != null) { mHotTabCamera.Dispose(); mHotTabCamera = null; } this.Invoke((MethodInvoker) delegate() { if (IsDebugMode) { Trace.WriteLine("New HotTabCamera No." + iDeviceNum + " by Thread " + Thread.CurrentThread.ManagedThreadId + " on " + System.DateTime.Now); } mHotTabCamera = new HotTabCamera(iDeviceNum, VIDEOWIDTH, VIDEOHEIGHT, VIDEOBITSPERPIXEL, box); }); CameraResolution = HotTabCamera.CheckMaxResolution(iDeviceNum); // 2M Camera : X = 1600, Y = 1200 // 5M Camera : X = 2592, Y = 1944 // 6M Camera : X = 3264, Y = 1836 if (IsDebugMode) { Trace.WriteLine("CameraResolution, X : " + CameraResolution.X + " , Y : " + CameraResolution.Y); } // 1. 只有單個相機, 沒有閃光燈 // 2. 有前後相機, 後相機有閃光燈 // 像素不到500萬, 一定是前鏡頭 if ((CameraResolution.X < 1920) && (CameraResolution.Y < 1440)) { labelTestCameraName.Text = "Front Camera"; buttonFlash.Visible = false; buttonFlash.Enabled = false; buttonCapture.Enabled = true; } else // ID82, IB10X, DAP7, M9020B, ID8HH, ID90 只有單一相機, 有閃光燈 { labelTestCameraName.Text = "Rear Camera"; buttonFlash.Visible = true; buttonFlash.Enabled = true; buttonCapture.Enabled = false; mHotTabCamera.Flash(0); if (Is8MCameraUsed) { Flash_8M(0); } else { SetFlashOff(); } } if (IsAutoModeCamera) { buttonFlash.Visible = true; buttonFlash.Enabled = false; buttonCapture.Visible = true; buttonCapture.Enabled = false; TimerCamaraCapture.Interval = TimerIntervalCamaraCapture; TimerCamaraCapture.Start(); } } catch (Exception ex) { ExceptionEvent(ex); } groupBoxTestButton.Enabled = true; } }