public static int GetExamID(Bitmap bitmap) { var results = QrCodeEncoderDecoder.DecodeMultiple(bitmap); if (results != null && results.Length > 0) { var res = results.FirstOrDefault(); if (int.TryParse(res.ToString().Split('/')?[0], out int result)) { return(result); } } return(0); }
public static Bitmap ResizeToStandard(Bitmap bitmap) { var qrCodesPositions = QrCodeEncoderDecoder.DecodeMultiple(bitmap).ToList().OrderBy(x => x.ResultPoints.FirstOrDefault().Y).Select(x => x.ResultPoints.FirstOrDefault()).ToList(); if (qrCodesPositions.Count() == 3 && (qrCodesPositions[2].Y - (qrCodesPositions[1].Y + qrCodesPositions[0].Y)) < 0) { bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone); } var qrCodesNewPositions = QrCodeEncoderDecoder.DecodeMultiple(bitmap).ToList().OrderBy(x => x.ResultPoints.FirstOrDefault().X).Select(x => x.ResultPoints).ToList(); var leftTopQRCode = qrCodesNewPositions.Take(2).OrderBy(x => x.FirstOrDefault().Y).FirstOrDefault().OrderBy(x => x.X + x.Y).FirstOrDefault(); var leftBottomQRCode = qrCodesNewPositions.Take(2).OrderByDescending(x => x.FirstOrDefault().Y).FirstOrDefault().OrderBy(x => x.X).FirstOrDefault(); var rightTopQRCode = qrCodesNewPositions[2].OrderByDescending(x => x.X).FirstOrDefault(); var source = new PointF[4]; source[0] = new PointF(leftTopQRCode.X, leftTopQRCode.Y); source[1] = new PointF(leftBottomQRCode.X, leftBottomQRCode.Y); source[2] = new PointF(rightTopQRCode.X, leftBottomQRCode.Y); source[3] = new PointF(rightTopQRCode.X, rightTopQRCode.Y); var topQRDistance = rightTopQRCode.X - leftBottomQRCode.X; var leftQRDistance = leftBottomQRCode.Y - leftTopQRCode.Y; int offsideX = (int)((topQRDistance / distanceBeetweenTopQRCodes) * standardLeftRightMarginPercentage); int offsideTop = (int)((leftQRDistance / distanceBeetweenLeftQRCodes) * standardTopMarginPercentage); int offsideBot = (int)((leftQRDistance / distanceBeetweenLeftQRCodes) * standardBottomMarginPercentage); source[0] = addOffsideToPoint(source[0], -offsideX, -offsideTop, bitmap); source[1] = addOffsideToPoint(source[1], -offsideX, offsideBot, bitmap); source[2] = addOffsideToPoint(source[2], offsideX, offsideBot, bitmap); source[3] = addOffsideToPoint(source[3], offsideX, -offsideTop, bitmap); var target = new PointF[] { new PointF(0, 0), new PointF(0, standardDocumentHeight), new PointF(standardDocumentWidth, standardDocumentHeight), new PointF(standardDocumentWidth, 0) }; using (var image = new Image <Gray, byte>(bitmap)) { var tran = CvInvoke.GetPerspectiveTransform(source, target); CvInvoke.WarpPerspective(image, image, tran, new Size((int)standardDocumentWidth, (int)standardDocumentHeight)); return(image.ToBitmap((int)standardDocumentWidth, (int)standardDocumentHeight)); } }