我的代码:
IBarcodeReaderGeneric reader = new BarcodeReaderGeneric()
{
AutoRotate = true,
TryInverted = true,
Options =
{
PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.CODE_39 },
TryHarder = true,
ReturnCodabarStartEnd = true,
PureBarcode = false
}
};
获取条形码:
using (var img = RotateImage(newBitmap, -90, true, true, Color.White))
{
lum = new BitmapLuminanceSource(img);
barcode = reader.Decode(lum)?.ToString();
if (!string.IsNullOrEmpty(barcode))
{
return img;
}
}
旋转图像的方法:
public static Bitmap RotateImage(Image inputImage, float angleDegrees, bool upsizeOk,
bool clipOk, Color backgroundColor)
{
// Test for zero rotation and return a clone of the input image
if (angleDegrees == 0f)
return (Bitmap)inputImage.Clone();
// Set up old and new image dimensions, assuming upsizing not wanted and clipping OK
int oldWidth = inputImage.Width;
int oldHeight = inputImage.Height;
int newWidth = oldWidth;
int newHeight = oldHeight;
float scaleFactor = 1f;
// If upsizing wanted or clipping not OK calculate the size of the resulting bitmap
if (upsizeOk || !clipOk)
{
double angleRadians = angleDegrees * Math.PI / 180d;
double cos = Math.Abs(Math.Cos(angleRadians));
double sin = Math.Abs(Math.Sin(angleRadians));
newWidth = (int)Math.Round(oldWidth * cos + oldHeight * sin);
newHeight = (int)Math.Round(oldWidth * sin + oldHeight * cos);
}
// If upsizing not wanted and clipping not OK need a scaling factor
if (!upsizeOk && !clipOk)
{
scaleFactor = Math.Min((float)oldWidth / newWidth, (float)oldHeight / newHeight);
newWidth = oldWidth;
newHeight = oldHeight;
}
// Create the new bitmap object. If background color is transparent it must be 32-bit,
// otherwise 24-bit is good enough.
Bitmap newBitmap = new Bitmap(newWidth, newHeight, backgroundColor == Color.Transparent ?
PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb);
newBitmap.SetResolution(inputImage.HorizontalResolution, inputImage.VerticalResolution);
// Create the Graphics object that does the work
using (Graphics graphicsObject = Graphics.FromImage(newBitmap))
{
graphicsObject.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsObject.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphicsObject.SmoothingMode = SmoothingMode.HighQuality;
// Fill in the specified background color if necessary
if (backgroundColor != Color.Transparent)
graphicsObject.Clear(backgroundColor);
// Set up the built-in transformation matrix to do the rotation and maybe scaling
graphicsObject.TranslateTransform(newWidth / 2f, newHeight / 2f);
if (scaleFactor != 1f)
graphicsObject.ScaleTransform(scaleFactor, scaleFactor);
graphicsObject.RotateTransform(angleDegrees);
graphicsObject.TranslateTransform(-oldWidth / 2f, -oldHeight / 2f);
// Draw the result
graphicsObject.DrawImage(inputImage, 0, 0);
}
我试图从旋转90度的扫描图像中读取条形码。我使用标准代码旋转图像,代码从此图像中读取:
注意顶部和底部有哪些大空格,如果按以下方式调用方法,则可以获取它们: RotateImage(newBitmap,-90,false,false,Color.White) < / p>
但如果图像旋转正确,如图所示:
然后条形码无法读取!为什么会这样?毕竟,第二张图像正确旋转,与第一张图像不同。
答案 0 :(得分:0)
我想建议您使用Spire.Barcode,它也是免费的,并且可以更好地完成任务。有了她,我没有遇到任何问题。