-
-
Save ZNickq/49605d299dbabb321c28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Fragment; | |
import android.media.Ringtone; | |
import android.media.RingtoneManager; | |
import android.net.Uri; | |
import android.os.Bundle; | |
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 android.widget.Toast; | |
import com.google.zxing.BarcodeFormat; | |
import com.google.zxing.Result; | |
import java.util.ArrayList; | |
import java.util.List; | |
import me.dm7.barcodescanner.zxing.ZXingScannerView; | |
public class CameraFragment extends Fragment implements ZXingScannerView.ResultHandler { | |
private static final String FLASH_STATE = "FLASH_STATE"; | |
private static final String SELECTED_FORMATS = "SELECTED_FORMATS"; | |
private ZXingScannerView mScannerView; | |
private boolean mFlash; | |
private ArrayList<Integer> mSelectedIndices; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) { | |
mScannerView = new ZXingScannerView(getActivity()); | |
if(state != null) { | |
mFlash = state.getBoolean(FLASH_STATE, false); | |
mSelectedIndices = state.getIntegerArrayList(SELECTED_FORMATS); | |
} else { | |
mFlash = false; | |
mSelectedIndices = null; | |
} | |
setupFormats(); | |
return mScannerView; | |
} | |
@Override | |
public void onCreate(Bundle state) { | |
super.onCreate(state); | |
} | |
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) { | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
return false; | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
mScannerView.setResultHandler(this); | |
mScannerView.startCamera(); | |
mScannerView.setFlash(mFlash); | |
mScannerView.setAutoFocus(true); | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
outState.putBoolean(FLASH_STATE, mFlash); | |
outState.putIntegerArrayList(SELECTED_FORMATS, mSelectedIndices); | |
} | |
@Override | |
public void handleResult(Result rawResult) { | |
Toast.makeText(getActivity(), "Contents = " + rawResult.getText() + ", Format = " + rawResult.getBarcodeFormat().toString(), Toast.LENGTH_LONG).show(); | |
} | |
public void setupFormats() { | |
List<BarcodeFormat> formats = new ArrayList<BarcodeFormat>(); | |
if(mSelectedIndices == null || mSelectedIndices.isEmpty()) { | |
mSelectedIndices = new ArrayList<Integer>(); | |
for(int i = 0; i < ZXingScannerView.ALL_FORMATS.size(); i++) { | |
mSelectedIndices.add(i); | |
} | |
} | |
for(int index : mSelectedIndices) { | |
formats.add(ZXingScannerView.ALL_FORMATS.get(index)); | |
} | |
if(mScannerView != null) { | |
mScannerView.setFormats(formats); | |
} | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
mScannerView.stopCamera(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment