Back to project page barcodescanner.
The source code is released under:
Apache License
If you think the Android project barcodescanner listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package me.dm7.barcodescanner.zbar.sample; /*from w ww. j av a 2s. co m*/ import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.view.MenuItemCompat; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; import java.util.List; import me.dm7.barcodescanner.zbar.BarcodeFormat; import me.dm7.barcodescanner.zbar.Result; import me.dm7.barcodescanner.zbar.ZBarScannerView; public class ScannerFragment extends Fragment implements MessageDialogFragment.MessageDialogListener, ZBarScannerView.ResultHandler, FormatSelectorDialogFragment.FormatSelectorDialogListener { private static final String FLASH_STATE = "FLASH_STATE"; private static final String AUTO_FOCUS_STATE = "AUTO_FOCUS_STATE"; private static final String SELECTED_FORMATS = "SELECTED_FORMATS"; private ZBarScannerView mScannerView; private boolean mFlash; private boolean mAutoFocus; private ArrayList<Integer> mSelectedIndices; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) { mScannerView = new ZBarScannerView(getActivity()); if(state != null) { mFlash = state.getBoolean(FLASH_STATE, false); mAutoFocus = state.getBoolean(AUTO_FOCUS_STATE, true); mSelectedIndices = state.getIntegerArrayList(SELECTED_FORMATS); } else { mFlash = false; mAutoFocus = true; mSelectedIndices = null; } setupFormats(); return mScannerView; } @Override public void onCreate(Bundle state) { super.onCreate(state); setHasOptionsMenu(true); } public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); MenuItem menuItem; if(mFlash) { menuItem = menu.add(Menu.NONE, R.id.menu_flash, 0, R.string.flash_on); } else { menuItem = menu.add(Menu.NONE, R.id.menu_flash, 0, R.string.flash_off); } MenuItemCompat.setShowAsAction(menuItem, MenuItem.SHOW_AS_ACTION_ALWAYS); if(mAutoFocus) { menuItem = menu.add(Menu.NONE, R.id.menu_auto_focus, 0, R.string.auto_focus_on); } else { menuItem = menu.add(Menu.NONE, R.id.menu_auto_focus, 0, R.string.auto_focus_off); } MenuItemCompat.setShowAsAction(menuItem, MenuItem.SHOW_AS_ACTION_ALWAYS); menuItem = menu.add(Menu.NONE, R.id.menu_formats, 0, R.string.formats); MenuItemCompat.setShowAsAction(menuItem, MenuItem.SHOW_AS_ACTION_ALWAYS); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items switch (item.getItemId()) { case R.id.menu_flash: mFlash = !mFlash; if(mFlash) { item.setTitle(R.string.flash_on); } else { item.setTitle(R.string.flash_off); } mScannerView.setFlash(mFlash); return true; case R.id.menu_auto_focus: mAutoFocus = !mAutoFocus; if(mAutoFocus) { item.setTitle(R.string.auto_focus_on); } else { item.setTitle(R.string.auto_focus_off); } mScannerView.setAutoFocus(mAutoFocus); return true; case R.id.menu_formats: DialogFragment fragment = FormatSelectorDialogFragment.newInstance(this, mSelectedIndices); fragment.show(getActivity().getSupportFragmentManager(), "format_selector"); default: return super.onOptionsItemSelected(item); } } @Override public void onResume() { super.onResume(); mScannerView.setResultHandler(this); mScannerView.startCamera(); mScannerView.setFlash(mFlash); mScannerView.setAutoFocus(mAutoFocus); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean(FLASH_STATE, mFlash); outState.putBoolean(AUTO_FOCUS_STATE, mAutoFocus); outState.putIntegerArrayList(SELECTED_FORMATS, mSelectedIndices); } @Override public void handleResult(Result rawResult) { try { Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone r = RingtoneManager.getRingtone(getActivity().getApplicationContext(), notification); r.play(); } catch (Exception e) {} showMessageDialog("Contents = " + rawResult.getContents() + ", Format = " + rawResult.getBarcodeFormat().getName()); } public void showMessageDialog(String message) { DialogFragment fragment = MessageDialogFragment.newInstance("Scan Results", message, this); fragment.show(getActivity().getSupportFragmentManager(), "scan_results"); } public void closeMessageDialog() { closeDialog("scan_results"); } public void closeFormatsDialog() { closeDialog("format_selector"); } public void closeDialog(String dialogName) { FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); DialogFragment fragment = (DialogFragment) fragmentManager.findFragmentByTag(dialogName); if(fragment != null) { fragment.dismiss(); } } @Override public void onDialogPositiveClick(DialogFragment dialog) { // Resume the camera mScannerView.startCamera(); mScannerView.setFlash(mFlash); mScannerView.setAutoFocus(mAutoFocus); } @Override public void onFormatsSaved(ArrayList<Integer> selectedIndices) { mSelectedIndices = selectedIndices; setupFormats(); } public void setupFormats() { List<BarcodeFormat> formats = new ArrayList<BarcodeFormat>(); if(mSelectedIndices == null || mSelectedIndices.isEmpty()) { mSelectedIndices = new ArrayList<Integer>(); for(int i = 0; i < BarcodeFormat.ALL_FORMATS.size(); i++) { mSelectedIndices.add(i); } } for(int index : mSelectedIndices) { formats.add(BarcodeFormat.ALL_FORMATS.get(index)); } if(mScannerView != null) { mScannerView.setFormats(formats); } } @Override public void onPause() { super.onPause(); mScannerView.stopCamera(); closeMessageDialog(); closeFormatsDialog(); } }