Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Windows.Forms;
- using System.Windows.Forms.VisualStyles;
- using BarcodeLib.BarcodeReader;
- using DHL.Common;
- using DHL.ScanCode.MobileDataService;
- using DHL.WindowsBase;
- using ScanCode;
- using TwainDotNet;
- using TwainDotNet.TwainNative;
- using TwainDotNet.WinFroms;
- namespace DHL.ScanCode
- {
- public partial class ScanCodeScanAdd : FormBase
- {
- //private static AreaSettings AreaSettings = new AreaSettings(Units.Centimeters, 0.1f, 5.7f, 0.1F + 2.6f, 5.7f + 2.6f);
- private Twain _twain;
- ScanSettings _settings;
- //ფუნქციების კლასი
- Funcs _fun = new Funcs();
- //პროგრამის დირექტორია
- string _appPath = Path.GetDirectoryName(Application.ExecutablePath);
- public ScanCodeScanAdd()
- {
- InitializeComponent();
- //მზა სურათების წაკითხვა
- Readreadyimages();
- //დოკუმენტის ტიპის არჩევა
- cmb_doctype.SelectedIndex = 0;
- //სურათების დროებითი სახელების შესანახი მასივი
- var imagenames = new List<string>();
- var count = 0;
- //დროებითი დირექტორია
- var temp = _appPath+"\\temp";
- string tempath;
- try
- {
- _twain = new Twain(new WinFormsWindowMessageHook(this));
- _twain.TransferImage += delegate(Object sender, TransferImageEventArgs args)
- {
- if (args.Image == null) return;
- pictureBox1.Image = args.Image;
- if (!Directory.Exists(@temp))
- {
- Directory.CreateDirectory(@temp);
- }
- tempath = temp + "\\" + count + ".jpg";
- //suratis shenaxva
- //args.Image.Save( @tempath, ImageFormat.Jpeg);
- var parameters = new EncoderParameters(1);
- parameters.Param[0] = new EncoderParameter(Encoder.Quality, 50L);//50L
- var jgpEncoder = GetEncoder(ImageFormat.Jpeg);
- args.Image.Save(@tempath, jgpEncoder, parameters);
- //shevinaxot saxeli masivshi
- imagenames.Add(tempath);
- count++;
- // widthLabel.Text = "Width: " + pictureBox1.Image.Width;
- //heightLabel.Text = "Height: " + pictureBox1.Image.Height;
- };
- _twain.ScanningComplete += delegate
- {
- Enabled = true;
- //სკანირება მორჩა და დაიწყო პროცესი წაკითხვა დამუშავების
- Processimages(imagenames);
- imagenames.Clear();
- count = 0;
- Readreadyimages();
- this.Focus();
- };
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void selectSource_Click(object sender, EventArgs e)
- {
- if (_twain != null)
- {
- _twain.SelectSource();
- }
- else
- {
- ShowInformation("თქვენს სისტემაზე ვერ მოიძებნა სკანერი");
- }
- }
- private string _manualScanFolder;
- private void scan_Click(object sender, EventArgs e)
- {
- if (cmb_doctype.SelectedIndex == 3)
- {
- var fbdialog = new FolderBrowserDialog() { ShowNewFolderButton = true };
- if (fbdialog.ShowDialog() != DialogResult.OK || !Directory.Exists(fbdialog.SelectedPath))
- {
- return;
- }
- _manualScanFolder = fbdialog.SelectedPath;
- }
- var temp = Path.Combine(_appPath,"temp");
- _fun.Cleardir(temp);
- Skanireba();
- }
- //suratis decoderi
- private ImageCodecInfo GetEncoder(ImageFormat format)
- {
- var codecs = ImageCodecInfo.GetImageDecoders();
- return codecs.FirstOrDefault(codec => codec.FormatID == format.Guid);
- }
- //private void diagnostics_Click(object sender, EventArgs e)
- //{
- // var diagnostics = new Diagnostics(new WinFormsWindowMessageHook(this));
- //}
- private void m_close_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- //skanirebis dacyebis fuqncia
- public void Skanireba()
- {
- Enabled = false;
- //gverdis zoma
- var size = new PageSettings
- {
- Size=PageType.A4,
- };
- //rezoluciis setingebi
- var res = new ResolutionSettings
- {
- Dpi = 200,
- ColourSetting=ColourSetting.Colour,
- };
- _settings = new ScanSettings
- {
- //UseDocumentFeeder = useAdfCheckBox.Checked,
- ShowTwainUI = useUICheckBox.Checked,
- ShowProgressIndicatorUI = showProgressIndicatorUICheckBox.Checked,
- Resolution = res,
- Page=size,
- //UseDuplex = false,
- // blackAndWhiteCheckBox.Checked
- //? ResolutionSettings.Fax : ResolutionSettings.ColourPhotocopier,
- //? ResolutionSettings.Fax : ResolutionSettings.ColourPhotocopier,
- //Area = !checkBoxArea.Checked ? null : AreaSettings,
- // Rotation = new RotationSettings
- // {
- // AutomaticRotate = autoRotateCheckBox.Checked,
- // AutomaticBorderDetection = autoDetectBorderCheckBox.Checked
- // }
- };
- //MessageBox.Show(Convert.ToString(res.Dpi));
- try
- {
- _twain.StartScanning(_settings);
- }
- catch (TwainException ex)
- {
- MessageBox.Show(ex.Message);
- Enabled = true;
- }
- }
- private string Findbarcode(string filename)
- {
- try
- {
- using (var image = new Bitmap(filename))
- {
- var barcodes = BarcodeLib.BarcodeReader.BarcodeReader.read(image, BarcodeReader.CODE128);
- if (barcodes.Length > 0)
- {
- var bar = TrackNumber.Pharse(barcodes[0]);
- return bar.Isvalid ? bar.TrackingNumber : "";
- }
- }
- }
- catch (Exception exc)
- {
- return string.Empty;
- }
- return string.Empty;
- //var f = new Funcs();
- //var ret = f.RunCommandLine("\"" + filename + "\"");
- //var barcode = Billing.Win.TrackNumber.Pharse(f.Extractbarcode(ret));
- //return barcode.Isvalid ? barcode.TrackingNumber : string.Empty;
- }
- private void Processimages(List<string> imagenames)
- {
- Cursor = Cursors.WaitCursor;
- scan.Enabled = false;
- Application.DoEvents();
- //სკანირების ტიპის გარჩევა
- var dir = _appPath;
- var awb = false;
- var man = false;
- switch (cmb_doctype.SelectedIndex)
- {
- case 0:
- dir += "\\ready\\awb\\";
- break;
- case 1:
- dir += "\\ready\\awb\\";
- awb = true;
- break;
- case 2:
- dir += "\\ready\\delivery\\";
- break;
- case 3:
- dir = _manualScanFolder;
- man = true;
- break;
- }
- //ვნახოთ ყველა დასაკანერებული სურათი ცალცალკე
- var count = 1;
- var lastbar = "";
- progress.Maximum = imagenames.Count;
- progress.Value = 0;
- foreach(var img in imagenames)
- {
- progress.Value++;
- Application.DoEvents();
- if (cmb_doctype.SelectedIndex == 3)
- {
- Movimages(img, Path.GetFileNameWithoutExtension(img) + "_" + count, dir);
- count++;
- continue;
- }
- var barcode = Findbarcode(img);
- if (awb)
- {
- string combo;
- //ლოგიკა როცა ავბ + ინვოისია
- if (cmb_count.SelectedItem != null)
- {
- combo = cmb_count.SelectedItem.ToString();
- }
- else
- {
- combo = "0";
- }
- if (_fun.is_numeric(combo) && Convert.ToInt16(combo) > 0)
- {
- if (barcode != "")
- {
- barcode = TrackNumber.Pharse(barcode).TrackingNumber;
- if (Convert.ToInt16(combo) == count)
- {
- manual_input(img, dir);
- count = 1;
- }
- else
- {
- count = 1;
- Movimages(img, barcode + "_" + count, dir);
- lastbar = barcode;
- }
- }
- else
- {
- Movimages(img, lastbar + "_" + count, dir);
- }
- }
- else
- {
- if (barcode != "")
- {
- barcode = TrackNumber.Pharse(barcode).TrackingNumber;
- count = 1;
- Movimages(img, barcode + "_" + count, dir);
- lastbar = barcode;
- }
- else
- {
- Movimages(img, lastbar + "_" + count, dir);
- }
- }
- count++;
- }
- else
- {
- if (string.IsNullOrEmpty(barcode) && !man)
- {
- manual_input(img, dir);
- }
- else
- {
- if (string.IsNullOrEmpty(barcode))
- {
- barcode = Path.GetFileNameWithoutExtension(img);
- }
- Movimages(img, barcode + "_" + count, dir);
- count++;
- }
- }
- }
- //manual_input(img,dir);
- this.Cursor = Cursors.Default;
- scan.Enabled = true;
- Application.DoEvents();
- }
- //სურათის კოპირება
- string _folder = "";
- string _surati = "";
- string _lastbarcode = "";
- public void Movimages(string img, string barcode,string dir)
- {
- if ( barcode != "" )
- {
- _lastbarcode = barcode;
- }
- try
- {
- _folder = dir+"\\";
- _folder = _folder.Replace("\\\\", "\\");
- _surati = _lastbarcode + ".jpg";
- //თუ არ არსებობს დირექტორია რომელშიც უნდა ჩაიწეროს შევქმნათ
- if (!Directory.Exists(_folder))
- {
- Directory.CreateDirectory(_folder);
- }
- var rand = new Random();
- while (File.Exists(_folder + _surati) && barcode.Length > 0)
- {
- _surati = string.Format("{0}{1}.jpg", _lastbarcode, rand.Next(1, 999));
- }
- //თუ ასეთი ფაილი უკვე არსებობს ერრორზე გავიდეთ
- if (!File.Exists(_folder + _surati) && barcode.Length > 0)
- {
- File.Move(img, _folder + _surati);
- }
- else
- {
- if (barcode.Length > 0)
- {
- MessageBox.Show(@"დოკუმენტი ასეთი შტრიხკოდით უკვე არსებობს" + barcode);
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- //თუ შტრიხკოდი არ იკითხება ხელით კოპირება
- public void manual_input(string img,string dir)
- {
- var mForm = new Manual {picbox_1 = {ImageLocation = img}};
- if(mForm.ShowDialog(this) == DialogResult.OK)
- {
- var bc = TrackNumber.Pharse(mForm.txt_barcode.Text);
- Movimages(img, bc.TrackingNumber+"_1", dir);
- }
- }
- //გასაგზავნი ფაილების რაოდენობის ცვლადი
- public string[] Fileslist;
- public int Index = 0;
- public void Readreadyimages()
- {
- string dir = _appPath + ".\\ready\\";
- if (!Directory.Exists(@dir))
- {
- try
- {
- Directory.CreateDirectory(@dir);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- //ფაილების წაკითხვა ყველა სუბ დირექტორიიდანაც
- string[] lf = Directory.GetFiles(dir, "*.jpg", SearchOption.AllDirectories);
- //დირექტორიების წაკითხვა
- string[] dirs = Directory.GetDirectories(dir);
- files_count.Text = lf.Length.ToString();
- Fileslist = lf;
- }
- private void btn_viewfiles_Click(object sender, EventArgs e)
- {
- try
- {
- pictureBox1.ImageLocation = Fileslist[0];
- lbl_fnumber.Text = (1).ToString();
- }
- catch(Exception ex)
- {
- MessageBox.Show(@"დათვალიერება შეუძლებელია");
- }
- }
- private void btn_pre_Click(object sender, EventArgs e)
- {
- try
- {
- if (Index > 0)
- {
- pictureBox1.ImageLocation = Fileslist[Index - 1];
- Index--;
- lbl_fnumber.Text = (Index + 1).ToString();
- }
- else
- {
- MessageBox.Show(@"თქვენ უყურებთ საწყის ფაილს");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(@"დათვალიერება შეუძლებელია");
- }
- }
- private void btn_next_Click(object sender, EventArgs e)
- {
- if (Fileslist.Length > 0)
- {
- if (Index < (Fileslist.Length - 1))
- {
- pictureBox1.ImageLocation = Fileslist[Index + 1];
- Index++;
- lbl_fnumber.Text = (Index + 1).ToString();
- }
- else
- {
- MessageBox.Show(@"იწყება მეორე წრე");
- pictureBox1.ImageLocation = Fileslist[0];
- Index = 0;
- lbl_fnumber.Text = (Index + 1).ToString();
- }
- }
- }
- /// <summary>
- /// ფაილების გაგზავნა
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button2_Click(object sender, EventArgs e)
- {
- var dir = _appPath + "\\ready\\";
- //დირექტორიების წაკითხვა
- var dirs = Directory.GetDirectories(dir);
- // Use the 'client' variable to call operations on the service.
- var client = new MobileDataServiceClient();
- var erroCount = 0;
- //ყველა დირექტორია რაც არსებობს temp ში
- var err = "";
- foreach (var di in dirs)
- {
- var fileList = Directory.GetFiles(di, "*.jpg");
- progress.Maximum = fileList.Length;
- progress.Value = 0;
- foreach (var file in fileList)
- {
- var attr = File.GetAttributes(@file);
- if((attr & FileAttributes.Directory) == FileAttributes.Directory)
- {
- continue;
- }
- var fileinf = new FileInfo(@file);
- var image =
- File.ReadAllBytes(
- fileinf.FullName);
- try
- {
- if (fileinf.Directory == null) continue;
- var fileType = fileinf.Directory.Name.ToLower() == "awb" ? 1 : 2;
- if (client.SaveScannedImage(image, fileinf.Name, fileType, Convert.ToInt32(Profile.UserID)))
- {
- File.Delete(fileinf.FullName);
- progress.Value++;
- }
- else
- {
- erroCount++;
- err += ",UploadFalse";
- }
- }
- catch (Exception ex)
- {
- erroCount++;
- err += ex.Message;
- }
- }
- }
- // Always close the client.
- client.Close();
- progress.Value = 0;
- Readreadyimages();
- if (erroCount > 0)
- {
- MessageBox.Show(@"ფაილებს ნაწილი გაგზავნილია, ნაწილის გაგზავნისას მოხდა შეცდომა ცადეთ კიდევ ერთხელ" + err);
- }
- else
- {
- MessageBox.Show(@"ფაილები გაგზავნილია");
- }
- }
- private void cmb_doctype_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (cmb_doctype.SelectedIndex == 1)
- {
- cmb_count.SelectedIndex = 0;
- cmb_count.Enabled = true;
- lbl_cmb.Enabled = true;
- }
- else {
- cmb_count.Enabled = false;
- lbl_cmb.Enabled = false;
- }
- }
- private void button1_Click_1(object sender, EventArgs e)
- {
- openFileDialog1.ShowDialog();
- if (openFileDialog1.FileName.Length > 0)
- {
- Funcs ff = new Funcs();
- string result = ff.RunCommandLine("\"" + openFileDialog1.FileName + "\"");
- MessageBox.Show(result);
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- var f = new Funcs();
- var path = @"D:\Beqa\VSPRojects\DHLprojects\DHL\DHL.Scancode\bin\Debug\zbarcode\";
- var a = Findbarcode("\"" + path + "1.jpg\"");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement