半農半エンジニアの記録

関西在住エンジニア。個人で勉強・開発したこと、その他趣味のことを書いてます。農業してます。

XamarinでQRコード表示

Xamarinで作成中のアプリで、QRコード表示をする。 「Xamarine QR」でググってみると、読み取りの情報はよくヒットしたけど、生成の方は少なかった。

ZXing

Zxing(Zebra Crossing)というライブラリを利用した。

良いネーミングだ。。

実装

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:qrSample"
             x:Class="qrSample.MainPage"
             xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
             xmlns:zxcm="clr-namespace:ZXing.Common;assembly=zxing.portable">
    <StackLayout>
        <!-- show QRcode -->
        <zxing:ZXingBarcodeImageView WidthRequest="300" HeightRequest="300"
                                     BarcodeValue="https://google.com"
                                     BarcodeFormat="QR_CODE">
            <zxing:ZXingBarcodeImageView.BarcodeOptions>
                <zxcm:EncodingOptions Width="300" Height="300" />
            </zxing:ZXingBarcodeImageView.BarcodeOptions>
        </zxing:ZXingBarcodeImageView>
    </StackLayout>
</ContentPage>

という感じ。

f:id:m24te28:20190606070940p:plain
qrcode_sample

BarcodeOptionsは無くても表示できるけど、なぜか少しぼやけたイメージが表示される。

  • BarcodeValue="{Binding foobar}"としてバインディング可能
  • WidthRequest、HeightRequestは必要