コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
namespace AppName.Services
{
//DependencyServiceから利用する
public interface IZxingService
{
string GetDecodedValue(byte[] image);
}
}
[assembly: Dependency(typeof(ZxingService))]
namespace AppName.Droid.Services
{
public class ZxingService : IZxingService
{
public string GetDecodedValue(byte[] image)
{
if (image == null || image.Length == 0)
{
return String.Empty;
}
ZXing.Result result = null;
try
{
// オプションでサイズを指定してBitmapオブジェクトを生成
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.InSampleSize = 2;
Bitmap bitmap = BitmapFactory.DecodeByteArray(image, 0, image.Length, opts);
//3次元配列で画像のRGBを取得する
byte[] rgbBytes = ImageService.GetRgbBytes(bitmap);
LuminanceSource source = new RGBLuminanceSource(rgbBytes, bitmap.Width, bitmap.Height);
BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(source));
//QRコードを読み取り
var reader = new MultiFormatReader();
IDictionary<DecodeHintType, object> hints = new Dictionary<DecodeHintType, object>();
hints.Add(ZXing.DecodeHintType.PURE_BARCODE, true);
//hints.Add(ZXing.DecodeHintType.POSSIBLE_FORMATS, new List<BarcodeFormat>() { BarcodeFormat.QR_CODE }); //無くても動作します。
hints.Add(ZXing.DecodeHintType.TRY_HARDER, true);
reader.Hints = hints;
result = reader.decode(bb);
}
catch (Exception ex)
{
// fall thru, it means there is no QR code in image
}
if (result != null)
{
return result.Text;
}
return String.Empty;
}
}
}
using UIKit;
using CoreGraphics;
using ZXing;
using Xamarin.Forms;
[assembly: Dependency(typeof(ZxingService))]
namespace AppName.iOS.Services
{
public class ZxingService : IZxingService
{
public string GetDecodedValue(byte[] image)
{
if (image == null || image.Length == 0)
{
return String.Empty;
}
ZXing.Result result = null;
try
{
//バイト配列からUIImageを取得
var data = NSData.FromArray(image);
var uiimage = UIImage.LoadFromData(data);
//3次元配列で画像のRGBを取得する
byte[] rgbBytes = ImageService.GetRgbBytes(uiimage);
LuminanceSource source = new RGBLuminanceSource(rgbBytes, (int)uiimage.Size.Width, (int)uiimage.Size.Height);
BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(source));
//QRコードを読み取り
var reader = new MultiFormatReader();
IDictionary<DecodeHintType, object> hints = new Dictionary<DecodeHintType, object>();
hints.Add(ZXing.DecodeHintType.PURE_BARCODE, true);
//hints.Add(ZXing.DecodeHintType.POSSIBLE_FORMATS, new List<BarcodeFormat>() { BarcodeFormat.QR_CODE }); //無くても動作します。
hints.Add(ZXing.DecodeHintType.TRY_HARDER, true);
reader.Hints = hints;
result = reader.decode(bb);
}
catch (Exception ex)
{
// fall thru, it means there is no QR code in image
}
if (result != null)
{
return result.Text;
}
return String.Empty;
}
}
}
using AppName.Services;
using Xamarin.Forms;
public class TestPage : ContentPage
{
void OnScanClick(object sender, EventArgs e)
{
byte[] byteArray = null;
if (this.imgPicture.Source.GetType() == typeof(StreamImageSource))
{
//ImageSourceからStreamを取得する
Stream stream = ImgConverter.GetStreamFromImageSource(this.imgPicture.Source);
//Streamをバイト配列に変換する
byteArray = ImgConverter.GetByteArrayFromStream(stream);
//画像からQRデータを読み込む
string result = DependencyService.Get<IZxingService>().GetDecodedValue(byteArray);
}
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。