Ejemplo n.º 1
0
Archivo: Core.cs Proyecto: manhg/malyst
        /// <summary>
        /// Cắt đôi hình theo một chiều nào đó.
        /// Sử dụng để chuyển hình quét A4 thành 2 hình A5
        ///
        /// </summary>
        /// <param name="direction">Hướng cắt</param>
        /// <returns>Kết quả được lưu trong mảng Cropped</returns>
        public void Crop(CropDirection direction)
        {
            #region Source##
            // Marshal ảnh điểm nguồn vào mảng src
            Rectangle  srcRect    = new Rectangle(0, 0, this.bmp.Width, this.bmp.Height);
            BitmapData srcBmpData =
                this.bmp.LockBits(srcRect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                  this.bmp.PixelFormat);
            // Get the address of the first line.
            IntPtr srcptr   = srcBmpData.Scan0;
            int    srcBytes = srcBmpData.Stride * srcBmpData.Height;
            int    destBytes;
            byte[] src = new byte[srcBytes];
            // Marshal
            System.Runtime.InteropServices.Marshal.Copy(srcptr, src, 0, srcBytes);
            this.bmp.UnlockBits(srcBmpData);
            #endregion
            Cropped    = new Acquired[2];
            Cropped[0] = new Acquired();
            Cropped[1] = new Acquired();
            switch (direction)
            {
            case CropDirection.Horizonal:

                destBytes                   = srcBmpData.Stride * (srcBmpData.Height / 2);
                Cropped[0].Height           = Cropped[1].Height = bmp.Height / 2;
                Cropped[0].Width            = Cropped[1].Width = bmp.Width;
                Cropped[0].ImagePixelFormat = Cropped[1].ImagePixelFormat = bmp.PixelFormat;
                Cropped[0].Stride           = Cropped[1].Stride = srcBmpData.Stride;
                Cropped[0].BytesPerPixel    = Cropped[1].BytesPerPixel = srcBmpData.Stride / srcBmpData.Width;
                Cropped[0].pixel            = new byte[destBytes];
                Cropped[1].pixel            = new byte[destBytes];
                Array.Copy(src, 0, Cropped[0].pixel, 0, destBytes);
                Array.Copy(src, destBytes, Cropped[1].pixel, 0, destBytes);
                break;

            case CropDirection.Vertical:
                int cropStride = srcBmpData.Stride / 2;
                destBytes                   = cropStride * srcBmpData.Height;
                Cropped[0].Height           = Cropped[1].Height = bmp.Height;
                Cropped[0].Width            = Cropped[1].Width = bmp.Width / 2;
                Cropped[0].ImagePixelFormat = Cropped[1].ImagePixelFormat = bmp.PixelFormat;
                Cropped[0].Stride           = Cropped[1].Stride = cropStride;
                Cropped[0].BytesPerPixel    = Cropped[1].BytesPerPixel = srcBmpData.Stride / srcBmpData.Width;
                Cropped[0].pixel            = new byte[destBytes];
                Cropped[1].pixel            = new byte[destBytes];
                for (int y = 0; y < srcBmpData.Height; y++)
                {
                    Array.Copy(src, y * srcBmpData.Stride, Cropped[0].pixel, cropStride * y, cropStride);
                    Array.Copy(src, y * srcBmpData.Stride + cropStride, Cropped[1].pixel, cropStride * y, cropStride);
                }
                break;

            default:
                Cropped = null;
                break;
            }
        }
Ejemplo n.º 2
0
Archivo: Core.cs Proyecto: manhg/malyst
 public Interpret(KeySuite keys, Acquired acquired, Anchor anchor, CoreMsgEventHandler handler)
 {
     this.result      = new Discover();
     this.result.file = acquired.FileName;
     this.CoreMsg    += handler;
     this.acq         = acquired;
     this.keys        = keys;
     place            = new Place(anchor, this.CoreMsgDelegates);
 }
Ejemplo n.º 3
0
Archivo: Core.cs Proyecto: manhg/malyst
        /// <summary>
        /// Crop (khoét) một vùng bất kỳ trong hình, tức lọc ra.
        /// </summary>
        /// <param name="sx"></param>
        /// <param name="sy"></param>
        /// <param name="ex"></param>
        /// <param name="ey"></param>
        public void Crop(int sx, int sy, int ex, int ey)
        {
            #region Source##
            // Marshal ảnh điểm nguồn vào mảng src
            Rectangle  srcRect    = new Rectangle(0, 0, this.bmp.Width, this.bmp.Height);
            BitmapData srcBmpData =
                this.bmp.LockBits(srcRect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                  this.bmp.PixelFormat);
            // Get the address of the first line.
            IntPtr srcptr   = srcBmpData.Scan0;
            int    srcBytes = srcBmpData.Stride * srcBmpData.Height;
            int    destBytes;
            byte[] src = new byte[srcBytes];
            // Marshal
            System.Runtime.InteropServices.Marshal.Copy(srcptr, src, 0, srcBytes);
            this.bmp.UnlockBits(srcBmpData);
            #endregion
            Acquired crop   = new Acquired(bmp, this.CoreMsgDelegates);
            int      width  = ex - sx;
            int      height = ey - sy;
            crop.Height = height;
            crop.Width  = width;
            destBytes   = srcBmpData.Stride * (srcBmpData.Height / 2);

            crop.ImagePixelFormat = bmp.PixelFormat;
            int stride = (int)Math.Ceiling(srcBmpData.Stride * width * 1.0 / bmp.Width);
            crop.Stride        = stride;
            crop.BytesPerPixel = srcBmpData.Stride / srcBmpData.Width;
            crop.pixel         = new byte[stride * height];
            for (int i = sy; i < ey; i++)
            {
                Array.Copy(src, i * srcBmpData.Stride + sx, crop.pixel, (i - sy) * stride, stride);                 // width * crop.BytesPerPixel);
            }

            // save to file
            //unsafe
            //{
            //    fixed (byte* bmpPtr = crop.pixel)
            //    {
            //        Bitmap name = new Bitmap(crop.Width, crop.Height,stride,
            //            crop.ImagePixelFormat, new IntPtr(bmpPtr));
            //        name.Save(@"D:\temp\" + DateTime.Today.ToFileTime());
            //    }
            //}
        }
Ejemplo n.º 4
0
Archivo: Core.cs Proyecto: manhg/malyst
        public Discover OneFile(string fileName, ref KeySuite keys)
        {
            if (!File.Exists(fileName))
            {
                Inform(this, "Tệp {0} không có thực!", fileName);
                return(null);
            }
            else
            {
                Inform(this, "Xử lý: '{0}'", fileName);
            }
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            Acquired   acq;

            try
            {
                Bitmap sourceBitmap = new Bitmap(fs);
                switch (sourceBitmap.PixelFormat)
                {
                case PixelFormat.Format24bppRgb:
                case PixelFormat.Format32bppArgb:
                case PixelFormat.Format8bppIndexed:
                    break;

                default:
                    Inform(this, "Hệ màu không hỗ trợ. Chỉnh lại cấu hình quét về màu RGB hoặc Grey 8-bit.");
                    return(null);
                }
                acq          = new Acquired(sourceBitmap, CoreMsgDelegates);
                acq.FileName = fileName;
            }
            catch (ArgumentException) { Inform(this, "Tệp'{0}' bị hỏng.", fileName); return(null); }
            finally { fs.Close(); }
            Anchor anchor = new Anchor(acq, this.CoreMsgDelegates);

            anchor.Detect(AnchorType.TopLeft);
            anchor.Detect(AnchorType.TopRight);
            anchor.Detect(AnchorType.BottomLeft);
            anchor.Detect(AnchorType.BottomRight);
            Inform(this, string.Format("Neo: TL{0}  TR{1}  BL{2}  BR{3}",
                                       anchor[AnchorType.TopLeft].ToString(),
                                       anchor[AnchorType.TopRight].ToString(),
                                       anchor[AnchorType.BottomLeft].ToString(),
                                       anchor[AnchorType.BottomRight].ToString()));
            if (anchor.Acceptable)
            {
                Interpret interpret = new Interpret(keys, acq, anchor, this.CoreMsgDelegates);
                interpret.FindAnswer();
                interpret.FindStudent();
                interpret.FindProblem();
                interpret.FindGroup();
                interpret.FindMark();
                interpret.Result.root    = anchor[AnchorType.TopLeft];
                interpret.Result.portion = keys.portion;
                return(interpret.Result);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 5
0
Archivo: Core.cs Proyecto: manhg/malyst
 public Anchor(Acquired acq, CoreMsgEventHandler handler)
 {
     this.CoreMsg       += handler;
     this.acq            = acq;
     SearchAnchorBoxSize = acq.Width / 4;
 }