136 lines
4.6 KiB
Java
136 lines
4.6 KiB
Java
package example.zxing;
|
|
|
|
import android.content.Intent;
|
|
import android.hardware.Camera;
|
|
import android.os.Bundle;
|
|
import android.support.v4.app.Fragment;
|
|
import android.support.v7.app.ActionBarActivity;
|
|
import android.util.Log;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.Button;
|
|
import android.widget.Toast;
|
|
|
|
import com.google.zxing.integration.android.IntentIntegrator;
|
|
import com.google.zxing.integration.android.IntentResult;
|
|
|
|
|
|
public class MainActivity extends ActionBarActivity {
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_main);
|
|
}
|
|
|
|
public void scanBarcode(View view) {
|
|
new IntentIntegrator(this).initiateScan();
|
|
}
|
|
|
|
public void scanBarcodeCustomLayout(View view) {
|
|
IntentIntegrator integrator = new IntentIntegrator(this);
|
|
integrator.setCaptureActivity(AnyOrientationCaptureActivity.class);
|
|
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
|
|
integrator.setPrompt("Scan something");
|
|
integrator.setOrientationLocked(false);
|
|
integrator.setBeepEnabled(false);
|
|
integrator.initiateScan();
|
|
}
|
|
|
|
public void scanBarcodeFrontCamera(View view) {
|
|
IntentIntegrator integrator = new IntentIntegrator(this);
|
|
integrator.setCameraId(Camera.CameraInfo.CAMERA_FACING_FRONT);
|
|
integrator.initiateScan();
|
|
}
|
|
|
|
public void scanContinuous(View view) {
|
|
Intent intent = new Intent(this, ContinuousCaptureActivity.class);
|
|
startActivity(intent);
|
|
}
|
|
|
|
public void scanToolbar(View view) {
|
|
new IntentIntegrator(this).setCaptureActivity(ToolbarCaptureActivity.class).initiateScan();
|
|
}
|
|
|
|
public void scanCustomScanner(View view) {
|
|
new IntentIntegrator(this).setCaptureActivity(CustomScannerActivity.class).initiateScan();
|
|
}
|
|
|
|
@Override
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
|
|
if(result != null) {
|
|
if(result.getContents() == null) {
|
|
Log.d("MainActivity", "Cancelled scan");
|
|
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
|
|
} else {
|
|
Log.d("MainActivity", "Scanned");
|
|
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
|
|
}
|
|
} else {
|
|
Log.d("MainActivity", "Weird");
|
|
// This is important, otherwise the result will not be passed to the fragment
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Sample of scanning from a Fragment
|
|
*/
|
|
public static class ScanFragment extends Fragment {
|
|
private String toast;
|
|
|
|
public ScanFragment() {
|
|
}
|
|
|
|
@Override
|
|
public void onActivityCreated(Bundle savedInstanceState) {
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
displayToast();
|
|
}
|
|
|
|
@Override
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
Bundle savedInstanceState) {
|
|
View view = inflater.inflate(R.layout.fragment_scan, container, false);
|
|
Button scan = (Button) view.findViewById(R.id.scan_from_fragment);
|
|
scan.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
scanFromFragment();
|
|
}
|
|
});
|
|
return view;
|
|
}
|
|
|
|
public void scanFromFragment() {
|
|
IntentIntegrator.forSupportFragment(this).initiateScan();
|
|
}
|
|
|
|
private void displayToast() {
|
|
if(getActivity() != null && toast != null) {
|
|
Toast.makeText(getActivity(), toast, Toast.LENGTH_LONG).show();
|
|
toast = null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
|
|
if(result != null) {
|
|
if(result.getContents() == null) {
|
|
toast = "Cancelled from fragment";
|
|
} else {
|
|
toast = "Scanned from fragment: " + result.getContents();
|
|
}
|
|
|
|
// At this point we may or may not have a reference to the activity
|
|
displayToast();
|
|
}
|
|
}
|
|
}
|
|
}
|