Pull to refresh

Библиотека для сканирование разных типов кодов в Android

image

В этой статье я хотел бы рассказать про библиотеку для Android, которая упрощает процесс сканирования кодов. Библиотека использует модуль zbar для распознавания.


Сначала импортируем:
armeabi, armeabi-v7a, x86 and zbar.jar отсюда
— Так же не забудем android-support-v4.jar
— После чего импортируем саму библиотеку для сканирования, отсюда

Создадим разметку, основываясь на RelativeLayout, так как безопаснее всего будет показывать камеру на весь экран, а другие элементы накладывать поверх.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

    <fragment
        android:id="@+id/scannerFragment"
        class="garin.artemiy.simplescanner.library.fragments.SimpleScannerFragment"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"/>

</RelativeLayout>


В нужном классе находим фрагмент который мы объявили выше:

public class YourActivity extends FragmentActivity implements ScannerListener {

  @Override
  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        SimpleScannerFragment simpleScannerFragment = (SimpleScannerFragment)
            getSupportFragmentManager().findFragmentById(R.id.scannerFragment);
        simpleScannerFragment.setScannerListener(this);

    }

  @Override
    public void onDataReceive(String data, int barcodeType) {
    // при наведении на код, получаем данные и тип кода
  }

}


Больше ничего не требуется, всю рутинную работу взяла на себя библиотека. Ссылка на проект в гитхабе тут. Типы поддерживаемых кодов можно посмотреть в классе Symbol (zbar).
Tags:
Hubs:
You can’t comment this publication because its author is not yet a full member of the community. You will be able to contact the author only after he or she has been invited by someone in the community. Until then, author’s username will be hidden by an alias.