Xamarin.android 从xamarin c中的库图像读取条形码

Xamarin.android 从xamarin c中的库图像读取条形码,xamarin.android,xamarin.forms,Xamarin.android,Xamarin.forms,我需要使用xamarin从android应用程序中的gallery图像中读取条形码。您需要读取图像文件,然后对其进行解码 要解码它,你可以使用这个库。它有一个包含解码方法的条形码阅读器类 更新 我是这样做的: 使用上面的库,您需要在所有项目表单、Android和iOS中安装它 已创建类以保存窗体项目中的解码逻辑 using System.Collections.Generic; using ZXing; using Xamarin.Forms; namespace ReadBarcode {

我需要使用xamarin从android应用程序中的gallery图像中读取条形码。

您需要读取图像文件,然后对其进行解码

要解码它,你可以使用这个库。它有一个包含解码方法的条形码阅读器类

更新

我是这样做的:

使用上面的库,您需要在所有项目表单、Android和iOS中安装它

已创建类以保存窗体项目中的解码逻辑

using System.Collections.Generic;
using ZXing;
using Xamarin.Forms;

namespace ReadBarcode
{
    public class BarcodeDecoding
    {
        IImageHelper _imageHelper;

        public BarcodeDecoding()
        {
            _imageHelper = DependencyService.Get<IImageHelper>();
        }


        public Result Decode(string file, BarcodeFormat? format = null, KeyValuePair<DecodeHintType, object>[] aditionalHints = null)
        {
            var r = GetReader(format, aditionalHints);

            var image = GetImage(file);

            var result = r.decode(image);

            return result;
        }

        MultiFormatReader GetReader(BarcodeFormat? format, KeyValuePair<DecodeHintType, object>[] aditionalHints)
        {
            var reader = new MultiFormatReader();

            var hints = new Dictionary<DecodeHintType, object>();

            if (format.HasValue)
            {
                hints.Add(DecodeHintType.POSSIBLE_FORMATS, new List<BarcodeFormat>() { format.Value });
            }
            if (aditionalHints != null)
            {
                foreach (var ah in aditionalHints) 
                {
                    hints.Add(ah.Key, ah.Value);
                }
            }

            reader.Hints = hints;

            return reader;
        }

        BinaryBitmap GetImage(string fileName) 
        {
            // Get image file and pass in the bytes array
            // or pass in the image name and load the image from the platform implementation.

            var byteArray = GetBytesArraysSomeWhere(fileName);

            var binaryBitmap = _imageHelper.GetBinaryBitmap(byteArray);

            return binaryBitmap;
        }
    }
}
这样,您可以在Forms项目中读取图像并传入图像的字节数组表示形式,也可以传入图像名称并直接从特定于平台的实现中读取图像

下面是上面接口的Android实现。对于这个示例,我正在加载一个添加到Android项目资源中的文件。修改它以加载一个真实的图像不会那么困难,特别是如果您使用

注意:我使用的是BarcodeFormat.QR_码,因为这是我创建的条形码类型。如果您使用的是不同的东西,请从枚举中选择它

希望这有帮助

你可以找到一个完整的例子


从中使用的代码。

Zxing功能强大,我也是这么做的,但我要补充的是,我偶尔遇到了条形码阅读器。解码图像分辨率问题时,实际出现的条形码上的未遂事件返回空值

在摆弄了一些小提琴之后,我发现可以通过以下代码改变高度/宽度比,并且在解码之前返回空值的地方通常会得到很好的结果:

int i = -20;
while (barcode == null && i < 100)
{
    Bitmap resized = new Bitmap(pageImg, new Size(pageImg.Width + i, pageImg.Height + i));
    barcode = barcodeReader.Decode(resized);
    i++;
}

使用“从库中读取条形码”是指向用户显示条形码图像,还是指读取条形码的内容?您能提供更多信息吗?从包含条形码的库中所选图像中读取条形码,但在android中使用system.Drawing.Bitmap时,我收到一个错误system.TypeInitializationException:“Gdip”的类型初始值设定项引发了一个异常。帮我this@apineda,尝试添加代码以解释更多内容,stackoverflow支持的代码多于llinks。。thanks@shijinitsme刚刚更新了答案。添加了如何使其工作的示例代码。Mike感谢您的提示。@apineda感谢您的详细回答。QRcode可以正常工作。@apineda需要读取图像中的条形码而不是QRcode。尝试了以下代码,但失败了提示。AddDecodeHintType.Employment\u FORMATS,new List{BarcodeFormat.All\u 1D};
[assembly: Xamarin.Forms.Dependency(typeof(ImageHelper))]
namespace ReadBarcode.Droid
{
    public class ImageHelper : IImageHelper
    {
        Context context;

        public ImageHelper()
        {
            context = Xamarin.Forms.Forms.Context;
        }

        public BinaryBitmap GetBinaryBitmap(string imageName)
        {
            throw new NotImplementedException();
        }

        public BinaryBitmap GetBinaryBitmap(byte[] image)
        {
            //uncomment the line below to use the image that is passed instead of a raw image.
            //Bitmap bitmap = BitmapFactory.DecodeByteArray(image, 0, image.Length);

            Bitmap bitmap = BitmapFactory.DecodeStream(context.Resources.OpenRawResource(Resource.Raw.static_qr_code_without_logo));
            byte[] rgbBytes = GetRgbBytes(bitmap);
            var bin = new HybridBinarizer(new RGBLuminanceSource(rgbBytes, bitmap.Width, bitmap.Height));
            var i = new BinaryBitmap(bin);

            return i;
        }

        private byte[] GetRgbBytes(Bitmap image)
        {
            var rgbBytes = new List<byte>();
            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    var c = new Color(image.GetPixel(x, y));

                    rgbBytes.AddRange(new[] { c.R, c.G, c.B });
                }
            }
            return rgbBytes.ToArray();
        }
public partial class ReadBarcodePage : ContentPage
{
    BarcodeDecoding barcode;

    public ReadBarcodePage()
    {
        barcode = new BarcodeDecoding();

        InitializeComponent();
    }

    void Handle_Clicked(object sender, System.EventArgs e)
    {
        var aditionalHints = new KeyValuePair<DecodeHintType, object>(key: DecodeHintType.PURE_BARCODE, value: "TRUE");

        var result = barcode.Decode(file: "image_to_read", format: BarcodeFormat.QR_CODE, aditionalHints: new [] {aditionalHints } );

        //Label to show the text decoded
        QrResult.Text = result.Text;
    }
}
int i = -20;
while (barcode == null && i < 100)
{
    Bitmap resized = new Bitmap(pageImg, new Size(pageImg.Width + i, pageImg.Height + i));
    barcode = barcodeReader.Decode(resized);
    i++;
}