0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ZXingを用いたQRコード変換

Posted at

VB.net初心者です。困っています。助けてください
PictureBoxコントロールに、内臓カメラで取得した画像を常時表示し、その画像(撮影しているQRコード)をボタンクリック時に文字列変換し、テキスト表示するアプリを作っています。

問題は以下の2つです。
1.変換処理でエラーが発生し、プロセスが死ぬ?
  ⇒Dim l_data As RGBLuminanceSource = New RGBLuminanceSource(b_data, source.Width, source.Height)
   この処理で毎回エラーが発生します。
   ※エラーの画像は添付しています。「インデックスの範囲が境界外です。」といった旨です。QR_error.PNG

2.上記1で該当処理が上手くいったとしても、QRコードから文字列への変換ができない。

言語はVB.netです。
ソースコードは以下になります。
ボタンを押してから、問題となっている「QRコード⇒文字列変換」の部分を載せておきます。
※念のため、インポートしている情報も載せておきます。
Imports ZXing
Imports ZXing.BinaryBitmap
Imports ZXing.Binarizer
Imports ZXing.LuminanceSource
Imports ZXing.Result
Imports ZXing.QrCode
Imports ZXing.QrCode.QRCodeReader

Imports UnityEngine
Imports UnityEngine.Color
Imports UnityEngine.Color32

'testボタン押下時
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click

    '表示中のImageをbitmapとして取得
    Dim source As Bitmap = New Bitmap(Me.PictureBox1.Image)

    'QrCodeReader
    Dim q_reader As ZXing.QrCode.QRCodeReader = New ZXing.QrCode.QRCodeReader()

    'Imageオブジェクトをバイト配列に変換
    Dim b_data As Byte() = ImageToByteArray(source)

    Dim l_data As RGBLuminanceSource = New RGBLuminanceSource(b_data, source.Width, source.Height)

    Dim binarizer As Common.HybridBinarizer = New Common.HybridBinarizer(l_data)
    Dim source2 As BinaryBitmap = New BinaryBitmap(binarizer)

    'QRCodeReader変換
    Dim q_result As ZXing.Result = q_reader.decode(source2)
    If (q_result IsNot Nothing) Then
        Dim q_string As String = q_result.ToString
        load_seizo_txt.Text = q_string.Substring(0, 5)
        load_seiko_txt.Text = q_string.Substring(6, 11)
        load_lot_txt.Text = q_string.Substring(12, 17)
    End If
End Sub


'Imageオブジェクトをバイト配列に変換
Public Shared Function ImageToByteArray(ByVal img As Image) As Byte()
    Dim b As Byte() = CType(imgconv.ConvertTo(img, GetType(Byte())), Byte())
    Return b
End Function

ちなみに撮影中のQR画像ではなく、ネットに転がっていたきれいなQRコードの画像を同様に変換処理してもエラーが発生します。
なので画像ではなく、ZXsingによる変換処理中の問題なのかと考えています。

以上です。
乱文で申し訳ございませんが、どなたかご教授お願い致します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?