如何裁剪大图像以扫描条形码

问题描述 投票:0回答:2

我正在开发一个 WP8 应用程序来使用 ZXing 扫描条形码。如果图像仅包含ITF条码,则可以扫描条码。但是,如果条形码位于大图像内,则不起作用。

所以我想我必须将大图像裁剪成较小的图像才能应用扫描过程。我说得对吗?

所以,我的问题是: 是否有一些最佳实践可以做到这一点,或者我需要应用一些算法来随机选取大图像的部分?

以下是我的代码:

        StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");

        BitmapImage bitmapImage = await GetBitmapImage(folder, "LargeImage.png");

        WriteableBitmap btmMap = new WriteableBitmap(bitmapImage);
        var rgb = new BitmapLuminanceSource(btmMap);

        var hybrid = new HybridBinarizer(rgb);
        BinaryBitmap binBitmap = new BinaryBitmap(hybrid);

        Dictionary<DecodeHintType, object> zxingHints
            = new Dictionary<DecodeHintType, object>() { { DecodeHintType.TRY_HARDER, true } };

        Reader reader = new ZXing.OneD.MultiFormatOneDReader(zxingHints);

        try
        {
            Result result = reader.decode(binBitmap);
            if (result != null)
            {
                this.resultText = result.Text;
            }
        }
        catch (Exception ex)
        {
            this.resultText = ex.Message;
        }
.net windows-phone-8 barcode zxing
2个回答
3
投票

ZXing 有一个内置方法可以在解码之前裁剪图像。它发生在 LuminanceSource 步骤,即彩色图像根据每个像素的亮度变成灰度。这样做是为了减少需要处理的数据量。

示例:

var rgb = new BitmapLuminanceSource(New BitmapLuminanceSource(btmMap)
    .Crop(_frame.Left, _frame.Top, _frame.Width, _frame.Height));

其中

_frame
是描述您要搜索条形码的区域的矩形。这个矩形通常通过在 UI 中绘制一个视口来描述,以便用户将条形码居中。

这个答案主要适用于通过 Google 搜索最终到达这里的其他人,因为有很多原因导致您的代码无法正常工作,并且您现在可能已经离开该项目了。例如:为什么要从 PNG 文件而不是手机内置的网络摄像头加载图像?


2
投票

我建议使用 Lumia Imaging SDK(前诺基亚成像 SDK)为您提供硬件加速滤镜,例如从此页面获取的 CropFilter 示例:https://msdn.microsoft.com/en-us/library/lumia。成像.transforms.cropfilter.aspx

using (var filterEffect = new FilterEffect(source))
{
    // Initialize the filter and add the filter to the FilterEffect collection
    var filter = new CropFilter(new Windows.Foundation.Rect( 260, 210, 670, 446));

    filterEffect.Filters = new IFilter[] { filter };

    // Create a target where the filtered image will be rendered to
    var target = new WriteableBitmap(width, height);

    // Create a new renderer which outputs WriteableBitmaps
    using (var renderer = new WriteableBitmapRenderer(filterEffect, target))
    {
        // Render the image with the filter(s)
        await renderer.RenderAsync();

        // Set the output image to Image control as a source
        ImageControl.Source = target;
    }

    await SaveEffectAsync(filterEffect, "CropFilter.jpg", outputImageSize);
}
© www.soinside.com 2019 - 2024. All rights reserved.