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?

Android スマホでQRコードの内容の確認

0
Last updated at Posted at 2025-06-05

2次元の QR コード、最近はもうあちこちにありますね。
アプリでピッとするだけで、一々キーボードをポチポチしなくても良いのは実に楽なのですが、実際どんなデータが入っているのかは中々見えません。
特に、単に URL が入っているだけでは無く、データとして CSV がそのままコード化されていたりもするようで、それは是非直接見てみたいですね。

調べてみると、ちょいちょいで出来るようにまでお膳立てがされていました。しかも外部ライブラリで無く公式のようです。

手元の Android studio Ladybug Feature Drop | 2024.2.2 で、公式の説明に則って実装してみることにしました。

リポジトリは大丈夫そうだったので、 play-services-code-scanner を登録します。

libs.versions.toml に codescanner というのを追加して

[versions]
:
codescanner = "16.1.0"

[libraries]
:
codescanner = { group = "com.google.android.gms", name = "play-services-code-scanner", version.ref = "codescanner" }

build.gradle(:app) の dependencies に implementation 追加

dependencies {
    :
    implementation libs.codescanner
}

AndroidManifest.xml にダウンロードの定義も追加

  <application
    :
    <meta-data
        android:name="com.google.mlkit.vision.DEPENDENCIES"
        android:value="barcode_ui"/>
  </application>

こんなもんですね。

後はもう画面のボタンにスキャンするコードを入れて TextView に表示するだけです。
以下ざざっと… (って、当初は本当にボタンとテキストだけだったんですがHEX表示とか付けたら大きくなってしまいました。)

MainActivity.java

import android.os.Bundle;
import android.view.*;
import android.widget.*;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.google.mlkit.vision.barcode.common.Barcode;
import com.google.mlkit.vision.codescanner.*;

public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MainViewModel model = new ViewModelProvider(this).get(MainViewModel.class);

    Button scanButton = findViewById(R.id.scan_button);
    CheckBox appendCheck = findViewById(R.id.append_check);
    TextView contentText = findViewById(R.id.content_text);
    Spinner contentSpinner = findViewById(R.id.content_spinner);
    Button clearButton = findViewById(R.id.clear_button);

    model.getContent().observe(this, content -> {
      ContentDecoder contentDecoder = model.getContentDecoder().getValue();
      assert contentDecoder != null;
      contentText.setText(contentDecoder.getText(content));
    });
    model.getContentDecoder().observe(this, contentDecoder -> {
      byte[] content = model.getContent().getValue();
      contentText.setText(contentDecoder.getText(content));
    });

    GmsBarcodeScannerOptions options = new GmsBarcodeScannerOptions.Builder()
            .setBarcodeFormats(Barcode.FORMAT_QR_CODE)
            .enableAutoZoom()
            .build();
    GmsBarcodeScanner barcodeScanner = GmsBarcodeScanning.getClient(this, options);

    scanButton.setOnClickListener(v -> {
      barcodeScanner.startScan()
          .addOnSuccessListener(barcode -> {
            if(!appendCheck.isChecked()) model.clearContent();
            model.addContent(barcode.getRawBytes());
          })
          .addOnCanceledListener(() -> Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT).show())
          .addOnFailureListener(e -> {
            Toast.makeText(this, "throws Exception", Toast.LENGTH_LONG).show();
            e.printStackTrace();
          });
    });

    ContentSpinnerAdapter adapter = new ContentSpinnerAdapter();
    contentSpinner.setAdapter(adapter);
    contentSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        model.setContentDecoder((ContentDecoder)adapter.getItem(position));
      }
      @Override
      public void onNothingSelected(AdapterView<?> parent) { /*nothing*/ }
    });

    clearButton.setOnClickListener(v -> model.clearContent());
  }

  private static class ContentSpinnerAdapter extends BaseAdapter {
    @Override
    public int getCount() {
      return ContentDecoder.values().length;
    }

    @Override
    public Object getItem(int position) {
      return ContentDecoder.values()[position];
    }

    @Override
    public long getItemId(int position) {
      return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
      if(view == null) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.spinner_dropdown_item, parent, false);
        view.setTag(view.findViewById(R.id.dropdown_text));
      }
      TextView dropdownText = (TextView)view.getTag();
      dropdownText.setText(ContentDecoder.values()[position].toString());
      return view;
    }
  }
}

res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

  <Button
      android:id="@+id/scan_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Scan"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />

  <CheckBox
      android:id="@+id/append_check"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="10dp"
      android:text="Append"
      app:layout_constraintBottom_toBottomOf="@id/scan_button"
      app:layout_constraintStart_toEndOf="@id/scan_button" />

  <TextView
      android:id="@+id/content_text"
      android:layout_width="0dp"
      android:layout_height="0dp"
      app:layout_constraintBottom_toTopOf="@id/clear_button"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@id/scan_button" />

  <TextView
      android:id="@+id/content_label"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="content:"
      android:textSize="24sp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintStart_toStartOf="parent" />

  <Spinner
      android:id="@+id/content_spinner"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:layout_constraintBaseline_toBaselineOf="@id/content_label"
      app:layout_constraintStart_toEndOf="@id/content_label" />

  <Button
      android:id="@+id/clear_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Clear"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

res/layout/spinner_dropdown_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dropdown_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="?android:attr/spinnerItemStyle"
    android:textSize="24sp" />

MainViewModel.java

import androidx.annotation.*;
import androidx.lifecycle.*;

import java.util.*;

public class MainViewModel extends ViewModel {
  private final MutableLiveData<byte[]> contentLiveData = new MutableLiveData<>(new byte[0]);
  @NonNull LiveData<byte[]> getContent() { return contentLiveData; }

  void addContent(@Nullable byte[] appends) {
    if(appends == null) return;
    byte[] old = contentLiveData.getValue();
    assert old != null;
    byte[] content = Arrays.copyOf(old, old.length+appends.length);
    System.arraycopy(appends, 0, content, old.length, appends.length);
    contentLiveData.setValue(content);
  }
  void clearContent() {
    contentLiveData.setValue(new byte[0]);
  }

  private final MutableLiveData<ContentDecoder> contentDecoderLiveData = new MutableLiveData<>(ContentDecoder.HEX);
  LiveData<ContentDecoder> getContentDecoder() { return contentDecoderLiveData; }

  void setContentDecoder(@NonNull ContentDecoder contentDecoder) {
    contentDecoderLiveData.setValue(contentDecoder);
  }
}

ContentDecoder.java

import androidx.annotation.NonNull;

import java.nio.charset.*;

enum ContentDecoder {
  HEX {
    @NonNull String getText(byte[] content) {
      StringBuilder sb = new StringBuilder();
      int i = 0;
      for(byte b : content) {
        if(++i > 16) { sb.append("\n"); i = 1; }
        if(b < 16) sb.append('0');
        sb.append(Integer.toHexString(b));
      }
      return sb.toString();
    }

    public @NonNull String toString() {
      return "Hex";
    }
  },
  SHIFT_JIS {
    @NonNull String getText(byte[] content) {
      return new String(content, Charset.forName("Shift_JIS"));
    }

    public @NonNull String toString() {
      return "ShiftJIS";
    }
  },
  UTF_8 {
    @NonNull String getText(byte[] content) {
      return new String(content, StandardCharsets.UTF_8);
    }

    public @NonNull String toString() {
      return "UTF-8";
    }
  };

  abstract @NonNull String getText(byte[] content);
}

最初に起動したときに必要なライブラリが無いとダウンロードするのですが、それが終わらないうちに Scan ボタンを押すと例外が発生するようです。
本来ならダウンロードが終わるまで Scan ボタンを押せないようにして終わったら押せるようにするとかをやったほうが良いですが、出来るのか調べていません。
とりあえず Xperia XZ3 (Android10) に入れてその辺に散らかしていた紙を漁って見つけた QR コードを読ませてみましたが、まぁなんとか読めています。

最新の AndroidStudio プロジェクトは GitHub にて
https://github.com/Jimbe-github/QRCodeReader

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?