SHARE
TWEET

Untitled

a guest Jul 18th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package az.scanandplay.fragments;
  2.  
  3. import android.Manifest;
  4. import android.annotation.SuppressLint;
  5. import android.app.Dialog;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.content.IntentFilter;
  9. import android.content.pm.PackageManager;
  10. import android.hardware.Camera;
  11. import android.net.Uri;
  12. import android.os.Bundle;
  13. import android.support.annotation.NonNull;
  14. import android.support.annotation.Nullable;
  15. import android.support.design.widget.Snackbar;
  16. import android.support.v4.app.ActivityCompat;
  17. import android.support.v4.content.ContextCompat;
  18. import android.support.v7.widget.Toolbar;
  19. import android.util.Log;
  20. import android.view.GestureDetector;
  21. import android.view.LayoutInflater;
  22. import android.view.MenuItem;
  23. import android.view.MotionEvent;
  24. import android.view.ScaleGestureDetector;
  25. import android.view.View;
  26. import android.view.ViewGroup;
  27. import android.widget.Button;
  28. import android.widget.Toast;
  29.  
  30. import com.arellomobile.mvp.MvpAppCompatFragment;
  31. import com.arellomobile.mvp.presenter.InjectPresenter;
  32. import com.arellomobile.mvp.presenter.ProvidePresenter;
  33. import com.google.android.gms.common.ConnectionResult;
  34. import com.google.android.gms.common.GoogleApiAvailability;
  35. import com.google.android.gms.vision.CameraSource;
  36. import com.google.android.gms.vision.MultiProcessor;
  37. import com.google.android.gms.vision.barcode.Barcode;
  38. import com.google.android.gms.vision.barcode.BarcodeDetector;
  39.  
  40. import java.io.IOException;
  41.  
  42. import az.scanandplay.R;
  43. import az.scanandplay.presenters.QrScanPresenter;
  44. import az.scanandplay.presenters.QrScanView;
  45. import az.scanandplay.utils.BarcodeGraphic;
  46. import az.scanandplay.utils.BarcodeGraphicTracker;
  47. import az.scanandplay.utils.BarcodeTrackerFactory;
  48. import az.scanandplay.utils.ui.CameraSourcePreview;
  49. import az.scanandplay.utils.ui.GraphicOverlay;
  50.  
  51. import static android.support.constraint.Constraints.TAG;
  52. import static az.scanandplay.App.COMPANY_WEBSITE;
  53.  
  54. public class QrScanFragment extends MvpAppCompatFragment implements QrScanView, BarcodeGraphicTracker.BarcodeUpdateListener, Toolbar.OnMenuItemClickListener {
  55.  
  56.     private az.scanandplay.utils.ui.CameraSource mCameraSource;
  57.     private CameraSourcePreview mPreview;
  58.     private GraphicOverlay<BarcodeGraphic> mGraphicOverlay;
  59.     private PlayerBottomDialog dialog;
  60.     private Dialog aboutDialog;
  61.     private android.support.v7.widget.Toolbar toolbar;
  62.     private static final int REQUEST_CAMERA = 0;
  63.     ScaleGestureDetector scaleGestureDetector;
  64.     GestureDetector gestureDetector;
  65.  
  66.     @InjectPresenter
  67.     QrScanPresenter qrScanPresenter;
  68.  
  69.     @ProvidePresenter
  70.     QrScanPresenter qrScanPresenter() {
  71.         return new QrScanPresenter(getContext());
  72.     }
  73.  
  74.     @Override
  75.     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
  76.                              Bundle savedInstanceState) {
  77.         View view = inflater.inflate(R.layout.fragment_qr_scan, container, false);
  78.         toolbar = view.findViewById(R.id.qr_scanner_toolbar);
  79.         mPreview = view.findViewById(R.id.preview);
  80.         mGraphicOverlay = view.findViewById(R.id.graphicOverlay);
  81.         int rc = ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA);
  82.         if (rc == PackageManager.PERMISSION_GRANTED) {
  83.             createCameraSource();
  84.         } else {
  85.             requestCameraPermission();
  86.         }
  87.         gestureDetector = new GestureDetector(getContext(), new CaptureGestureListener());
  88.         scaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleListener());
  89.         return view;
  90.     }
  91.  
  92.  
  93.     private void requestCameraPermission() {
  94.         final String[] permissions = new String[]{Manifest.permission.CAMERA};
  95.  
  96.         if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
  97.                 Manifest.permission.CAMERA)) {
  98.             ActivityCompat.requestPermissions(getActivity(), permissions, 2);
  99.             return;
  100.         }
  101.     }
  102.  
  103.  
  104.     @Override
  105.     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  106.         toolbar.inflateMenu(R.menu.menu_toolbar);
  107.         toolbar.setOnMenuItemClickListener(this);
  108.     }
  109.  
  110.     @Override
  111.     public void onResume() {
  112.         super.onResume();
  113.         if (dialog != null && dialog.isVisible()) {
  114.             return;
  115.         }
  116.         if (aboutDialog != null) {
  117.             aboutDialog.dismiss();
  118.         }
  119.         startCameraSource();
  120.     }
  121.  
  122.  
  123.     private void createCameraSource() {
  124.         BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(getContext()).build();
  125.         BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay, getContext());
  126.         barcodeDetector.setProcessor(
  127.                 new MultiProcessor.Builder<>(barcodeFactory).build());
  128.         if (!barcodeDetector.isOperational()) {
  129.             Log.w(TAG, "Detector dependencies are not yet available.");
  130.         }
  131.         az.scanandplay.utils.ui.CameraSource.Builder builder = new az.scanandplay.utils.ui.CameraSource.Builder(getContext(), barcodeDetector)
  132.                 .setFacing(CameraSource.CAMERA_FACING_BACK)
  133.                 .setRequestedPreviewSize(1600, 1024)
  134.                 .setRequestedFps(15.0f);
  135.         mCameraSource = builder.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE).build();
  136.  
  137.     }
  138.  
  139.     private void startCameraSource() throws SecurityException {
  140.         int code = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(
  141.                 getContext());
  142.         if (code != ConnectionResult.SUCCESS) {
  143.  
  144.         }
  145.         if (mCameraSource != null) {
  146.             try {
  147.                 mPreview.start(mCameraSource, mGraphicOverlay);
  148.             } catch (IOException e) {
  149.                 mCameraSource.release();
  150.                 mCameraSource = null;
  151.             }
  152.         }
  153.     }
  154.  
  155.     @Override
  156.     public void showBottomDialog(String url) {
  157.         dialog = PlayerBottomDialog.newInstance(url);
  158.         dialog.setListener(new PlayerBottomDialog.OnDialogCloseListener() {
  159.             @Override
  160.             public void onDialogClosed() {
  161.                 dialog = null;
  162.             }
  163.         });
  164.         dialog.show(getChildFragmentManager(), null);
  165.     }
  166.  
  167.     @Override
  168.     public void showUnsupportedFormatError(String unsupportedTextFormat) {
  169.         Toast.makeText(getContext(), " ' " + unsupportedTextFormat + " ' " + "\n"
  170.                 + "isn't an .ogg file", Toast.LENGTH_SHORT).show();
  171.     }
  172.  
  173.     @Override
  174.     public void showConnectionError() {
  175.         Snackbar.make(getView(), "Connection error", Snackbar.LENGTH_INDEFINITE)
  176.                 .setAction("Retry", new View.OnClickListener() {
  177.                     @Override
  178.                     public void onClick(View v) {
  179.                     }
  180.                 }).show();
  181.     }
  182.  
  183.     @Override
  184.     public boolean onMenuItemClick(MenuItem item) {
  185.         switch (item.getItemId()) {
  186.             case R.id.contact_us_menu:
  187.                 Intent intent = new Intent(Intent.ACTION_VIEW);
  188.                 intent.setData(Uri.parse(COMPANY_WEBSITE));
  189.                 startActivity(intent);
  190.                 break;
  191.             case R.id.about_us_menu:
  192.                 showAboutDialog();
  193.                 break;
  194.         }
  195.         return false;
  196.     }
  197.  
  198.     public void showAboutDialog() {
  199.         ViewGroup viewGroup = getView().findViewById(android.R.id.content);
  200.         View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.about_us_dialog, viewGroup, false);
  201.         aboutDialog = new Dialog(getContext());
  202.         aboutDialog.setContentView(dialogView);
  203.         aboutDialog.show();
  204.  
  205.         aboutDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
  206.             @Override
  207.             public void onDismiss(DialogInterface dialog) {
  208.             }
  209.         });
  210.  
  211.         Button closeButton = aboutDialog.findViewById(R.id.close_button);
  212.         closeButton.setOnClickListener(new View.OnClickListener() {
  213.             @Override
  214.             public void onClick(View v) {
  215.                 aboutDialog.dismiss();
  216.             }
  217.         });
  218.     }
  219.  
  220.     @SuppressLint("MissingPermission")
  221.     @Override
  222.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  223.         if (requestCode == REQUEST_CAMERA && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  224.             //mScannerView.startCamera();
  225.             try {
  226.                 mCameraSource.start();
  227.             } catch (IOException e) {
  228.                 e.printStackTrace();
  229.             }
  230.         }
  231.     }
  232.  
  233.     @Override
  234.     public void onPause() {
  235.         super.onPause();
  236.         if (mPreview != null) {
  237.             mPreview.stop();
  238.         }
  239.     }
  240.  
  241.     @Override
  242.     public void onDestroyView() {
  243.         super.onDestroyView();
  244.         if (mPreview != null) {
  245.             mPreview.release();
  246.         }
  247.     }
  248.  
  249.     private boolean onTap(float rawX, float rawY) {
  250.         int[] location = new int[2];
  251.         mGraphicOverlay.getLocationOnScreen(location);
  252.         float x = (rawX - location[0]) / mGraphicOverlay.getWidthScaleFactor();
  253.         float y = (rawY - location[1]) / mGraphicOverlay.getHeightScaleFactor();
  254.  
  255.         Barcode best = null;
  256.         float bestDistance = Float.MAX_VALUE;
  257.         for (BarcodeGraphic graphic : mGraphicOverlay.getGraphics()) {
  258.             Barcode barcode = graphic.getBarcode();
  259.             if (barcode.getBoundingBox().contains((int) x, (int) y)) {
  260.                 // Exact hit, no need to keep looking.
  261.                 best = barcode;
  262.                 break;
  263.             }
  264.             float dx = x - barcode.getBoundingBox().centerX();
  265.             float dy = y - barcode.getBoundingBox().centerY();
  266.             float distance = (dx * dx) + (dy * dy);  // actually squared distance
  267.             if (distance < bestDistance) {
  268.                 best = barcode;
  269.                 bestDistance = distance;
  270.             }
  271.         }
  272.  
  273.         if (best != null) {
  274.             return true;
  275.         }
  276.         return false;
  277.     }
  278.  
  279.     @Override
  280.     public void onBarcodeDetected(Barcode barcode) {
  281.         qrScanPresenter.handleResult(barcode.displayValue);
  282.     }
  283.  
  284.  
  285.     private class CaptureGestureListener extends GestureDetector.SimpleOnGestureListener {
  286.         @Override
  287.         public boolean onSingleTapConfirmed(MotionEvent e) {
  288.             return onTap(e.getRawX(), e.getRawY()) || super.onSingleTapConfirmed(e);
  289.         }
  290.     }
  291.  
  292.     private class ScaleListener implements ScaleGestureDetector.OnScaleGestureListener {
  293.  
  294.         @Override
  295.         public boolean onScale(ScaleGestureDetector detector) {
  296.             return false;
  297.         }
  298.  
  299.         @Override
  300.         public boolean onScaleBegin(ScaleGestureDetector detector) {
  301.             return true;
  302.         }
  303.  
  304.         @Override
  305.         public void onScaleEnd(ScaleGestureDetector detector) {
  306.             mCameraSource.doZoom(detector.getScaleFactor());
  307.         }
  308.     }
  309.  
  310.  
  311. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top