Commit 8c2a8ab9 authored by 張益誠's avatar 張益誠

點擊框取要讀條碼的部分

parent f79dff52
No preview for this file type
using System;
using ETC2019.Models.Barcode.Linear;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
......@@ -13,9 +16,47 @@ namespace ETC2019.Controllers
{
private string[] AllowExtension { get; set; }
private string[] Items { get; set; }
private int x { get; set; }
private int y { get; set; }
private int x_2 { get; set; }
private int y_2 { get; set; }
private float size_h { get; set; }
private float size_w { get; set; }
private BackgroundWorker Worker { get; set; }
public BarcodeController()
{
AllowExtension = new string[] { ".jpg", ".jpeg", ".gif" };
Worker = new BackgroundWorker() { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
Worker.DoWork += Worker_DoWork;
Worker.ProgressChanged += Worker_ProgressChanged;
Worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
}
private void Worker_DoWork(object sender, DoWorkEventArgs e) //你就當作我沒有寫吧
{
for (int i = 0; i < 4; i++)
{
Bitmap img = GetImage(i);
Rectangle_pic(x, y, x_2, y_2, size_w, size_h, img, out string result);
Console.WriteLine(x);
}
}
private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
throw new NotImplementedException();
}
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
public int AvailableWidth(int width, ToolStripItemCollection items)
......@@ -28,12 +69,91 @@ namespace ETC2019.Controllers
}
return width;
}
public Bitmap GetImage(int index)
{
FileStream file = new FileStream(Items[index], FileMode.Open);
MemoryStream memory = new MemoryStream();
file.CopyTo(memory);
public Bitmap Get(int index)
file.Close();
memory.Position = 0;
return new Bitmap(memory);
}
public bool RunWorker() //你新寫的
{
if (!Worker.IsBusy)
Worker.RunWorkerAsync(new object[] { x, y, x_2, y_2, Items });
else
return false;
return true;
}
public string Click_pic_x(MouseEventArgs e) //座標X
{
string x = Convert.ToString(e.X);
return x;
}
public string Click_pic_y(MouseEventArgs e) //座標Y
{
string y = Convert.ToString(e.Y);
return y;
}
public Image Rectangle_pic(int x , int y , int x_2,int y_2,float size_w, float size_h, Image image, out string result)
{
this.x = x;
this.y = y;
this.x_2 = x_2;
this.y_2 = y_2;
this.size_w = size_w;
this.size_h = size_h;
Pen pen = new Pen(Color.Red, 5);
if (x > x_2) //確保最左上點是x,變數對調
{
int change = x_2;
x_2 = x;
x = change;
}
if (y > y_2) //確保最左上點是y,變數對調
{
int change = y_2;
y_2 = y;
y = change;
}
//(1/ size_h)跟(1 / size_w)是還原比例
int h = (int)((y_2 - y)*(1/ size_h)); //算出"原圖比例的高"數值
int w = (int)((x_2 - x )* (1 / size_w)); //算出"原圖比例的寬"數值
y = (int)((y) * (1 / size_h)); //算出"原圖比例的左上角Y"數值
x = (int)((x) * (1 / size_w)); //算出"原圖比例的左上角X"數值
Bitmap bmp = new Bitmap(image);
result = "";
using (StreamReader sr = new StreamReader(Path.Combine("data", "code39.json")))
{
Code39 barcode = JsonConvert.DeserializeObject<Code39>(sr.ReadToEnd());
result = barcode.Decode(bmp.Clone(new Rectangle(x, y, w, h), bmp.PixelFormat));
}
Graphics g = Graphics.FromImage(image);
g.DrawRectangle(pen, x, y, w, h);
g.Save();
return image;
}
public bool LoadFolder(string path, out string[] items)
{
List<string> itemList = new List<string>(Items = FolderIterator(path, out string[] tempItems) ? tempItems : new string[0]);
......
......@@ -3,6 +3,7 @@ using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using ZXing;
// Program:
// 建檔資料模型。
......
......@@ -35,16 +35,23 @@
this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.toolStrip2 = new System.Windows.Forms.ToolStrip();
this.toolStripLabel2 = new System.Windows.Forms.ToolStripLabel();
this.toolStripTextBox2 = new System.Windows.Forms.ToolStripTextBox();
this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.toolStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.toolStrip2.SuspendLayout();
this.SuspendLayout();
//
......@@ -109,7 +116,11 @@
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.label4);
this.splitContainer1.Panel1.Controls.Add(this.label3);
this.splitContainer1.Panel1.Controls.Add(this.pictureBox1);
this.splitContainer1.Panel1.Controls.Add(this.toolStrip2);
this.splitContainer1.Panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.SplitContainer1_Panel1_Paint);
//
// splitContainer1.Panel2
//
......@@ -118,6 +129,19 @@
this.splitContainer1.SplitterDistance = 447;
this.splitContainer1.TabIndex = 1;
//
// pictureBox1
//
this.pictureBox1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(0, 30);
this.pictureBox1.Margin = new System.Windows.Forms.Padding(0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(447, 340);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseClick);
//
// toolStrip2
//
this.toolStrip2.AllowMerge = false;
......@@ -184,11 +208,61 @@
this.listBox1.TabIndex = 0;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.ListBox1_SelectedIndexChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(349, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 3;
this.label1.Text = "label1";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(349, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 2;
this.label2.Text = "label2";
this.label2.Click += new System.EventHandler(this.Label2_Click);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(349, 2);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 16);
this.label3.TabIndex = 4;
this.label3.Text = "label3";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(349, 18);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(56, 16);
this.label4.TabIndex = 5;
this.label4.Text = "label4";
//
// button1
//
this.button1.Location = new System.Drawing.Point(442, 4);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 4;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Button1_Click);
//
// BarcodePage
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(600, 400);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.splitContainer1);
this.Controls.Add(this.toolStrip1);
this.DoubleBuffered = true;
......@@ -205,6 +279,7 @@
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.toolStrip2.ResumeLayout(false);
this.toolStrip2.PerformLayout();
this.ResumeLayout(false);
......@@ -225,5 +300,11 @@
private System.Windows.Forms.ToolStripTextBox toolStripTextBox2;
private System.Windows.Forms.ToolStripButton toolStripButton2;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button1;
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
......@@ -14,6 +15,13 @@ namespace ETC2019.Views
public partial class BarcodePage : Form
{
private BarcodeController Controller { get; set; }
int switch_turn = 0;
int x_sec = 0;
int y_sec = 0;
int x_f = 0;
int y_f = 0;
float size_h = 0;
float size_w = 0;
public BarcodePage()
{
InitializeComponent();
......@@ -46,12 +54,68 @@ namespace ETC2019.Views
listBox1.Items.Clear();
listBox1.Items.AddRange(items);
}
}
private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Bitmap img = Controller.GetImage(listBox1.SelectedIndex);
int x = img.Width; //取得"原圖的寬"數值
int y = img.Height; //取得"原圖的高"數值
pictureBox1.Image = img;
int h = pictureBox1.Height; //取得"顯示的寬"數值
int w = pictureBox1.Width; //取得"顯示的高"數值
size_w = (float)w / x; //取得寬比例
size_h = (float)h / y; //取得高比例
label1.Text = "原圖"+ y + "顯示"+h+"比例:"+ size_h*100+"%"; //秀你好看
}
private void SplitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
{
}
private void PictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Bitmap img = Controller.GetImage(listBox1.SelectedIndex); //取圖片
if (switch_turn == 1) //第二次模式
{
x_sec = int.Parse(Controller.Click_pic_x(e)); //第二次X位置
y_sec = int.Parse(Controller.Click_pic_y(e)); //第二次Y位置
label3.Text = "滑鼠x:" + x_sec + " 滑鼠y:" + y_sec; //顯示結果
switch_turn = 0; //切換第一次跟第二次點擊的變數紀錄
}
else //第一次
{
x_f = int.Parse(Controller.Click_pic_x(e));
y_f = int.Parse(Controller.Click_pic_y(e));
label2.Text = "滑鼠x:" + x_f + " 滑鼠y:" + y_f;
switch_turn = 1;
}
if (switch_turn == 0) //第二次點擊完 switch_turn會轉為零 進來輸出條碼結果
{
pictureBox1.Image = Controller.Rectangle_pic(x_f, y_f, x_sec, y_sec, size_w, size_h, img, out string result);//(x1,y1,x2,y2,縮小比例寬,縮小比例高,圖檔,黑科技我不懂)
label4.Text = result; //黑科技輸出
}
}
private void Label2_Click(object sender, EventArgs e)
{
}
private void Button1_Click(object sender, EventArgs e)
{
}
}
}
......@@ -44,6 +44,7 @@
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.PictureBox1_Click);
//
// pictureBox2
//
......
......@@ -35,5 +35,10 @@ namespace ETC2019.Views
sr.Close();
}
private void PictureBox1_Click(object sender, EventArgs e)
{
}
}
}
62659db1c1bb9501e1025abaa03779a5bbb48a68
1385925a5ab41cde7802dc6594fb4612befea2d2
......@@ -47,3 +47,52 @@ C:\Users\ECOM\source\repos\ETCProject1\ETC2019\bin\Debug\zxing.presentation.pdb
C:\Users\ECOM\source\repos\ETCProject1\ETC2019\bin\Debug\zxing.presentation.xml
C:\Users\ECOM\source\repos\ETCProject1\ETC2019\obj\Debug\ETC2019.Views.Form1.resources
C:\Users\ECOM\source\repos\ETCProject1\ETC2019\obj\Debug\ETC2019.Views.BarcodePage.resources
C:\Users\admin\CShape_ETC\ETC2019\obj\Debug\ETC2019.csprojAssemblyReference.cache
C:\Users\admin\CShape_ETC\ETC2019\obj\Debug\ETC2019.Properties.Resources.resources
C:\Users\admin\CShape_ETC\ETC2019\obj\Debug\ETC2019.Views.BarcodePage.resources
C:\Users\admin\CShape_ETC\ETC2019\obj\Debug\ETC2019.Views.Form1.resources
C:\Users\admin\CShape_ETC\ETC2019\obj\Debug\ETC2019.csproj.GenerateResource.cache
C:\Users\admin\CShape_ETC\ETC2019\obj\Debug\ETC2019.csproj.CoreCompileInputs.cache
C:\Users\admin\CShape_ETC\ETC2019\obj\Debug\ETC2019.exe
C:\Users\admin\CShape_ETC\ETC2019\obj\Debug\ETC2019.pdb
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\x64\concrt140.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\x64\cvextern.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\x64\msvcp140.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\x64\opencv_ffmpeg410_64.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\x64\vcruntime140.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\x86\concrt140.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\x86\cvextern.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\x86\msvcp140.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\x86\opencv_ffmpeg410.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\x86\vcruntime140.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\ETC2019.exe.config
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\ETC2019.exe
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\ETC2019.pdb
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\Emgu.CV.UI.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\Emgu.CV.World.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\Newtonsoft.Json.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\ZedGraph.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\zxing.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\zxing.presentation.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\Emgu.CV.UI.xml
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\Emgu.CV.World.xml
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\Newtonsoft.Json.xml
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\ZedGraph.xml
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\zxing.pdb
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\zxing.xml
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\zxing.presentation.pdb
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\zxing.presentation.xml
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\de\ZedGraph.resources.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\es\ZedGraph.resources.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\fr\ZedGraph.resources.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\hu\ZedGraph.resources.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\it\ZedGraph.resources.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\ja\ZedGraph.resources.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\pt\ZedGraph.resources.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\ru\ZedGraph.resources.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\sk\ZedGraph.resources.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\sv\ZedGraph.resources.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\tr\ZedGraph.resources.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\zh-cn\ZedGraph.resources.dll
C:\Users\admin\CShape_ETC\ETC2019\bin\Debug\zh-tw\ZedGraph.resources.dll
C:\Users\admin\CShape_ETC\ETC2019\obj\Debug\ETC2019.csproj.CopyComplete
78ef1ab4af57fd26e7cb65937bd4f21712590f40
2b62da0e172d105f8e848a2d667359e35bfb2ba2
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment