Advertisement
Guest User

Activity.java

a guest
Mar 12th, 2016
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. package com.rakamahesa.opencvtestbed;
  2.  
  3. import android.Manifest;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7.  
  8. import org.opencv.android.BaseLoaderCallback;
  9. import org.opencv.android.CameraBridgeViewBase;
  10. import org.opencv.android.LoaderCallbackInterface;
  11. import org.opencv.android.OpenCVLoader;
  12. import org.opencv.core.Mat;
  13. import org.opencv.imgproc.Imgproc;
  14.  
  15. public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2 {
  16.     //Data
  17.     protected CameraBridgeViewBase mCamera;
  18.  
  19.     //Create load callback
  20.     protected LoaderCallbackInterface mCallback = new BaseLoaderCallback(this) {
  21.         @Override
  22.         public void onManagerConnected(int status) {
  23.             //If not success, call base method
  24.             if (status != LoaderCallbackInterface.SUCCESS) super.onManagerConnected(status);
  25.             else {
  26.                 //Enable camera if connected to library
  27.                 if (mCamera != null) mCamera.enableView();
  28.             }
  29.         }
  30.     };
  31.  
  32.     @Override
  33.     protected void onCreate(Bundle savedInstanceState) {
  34.         //Super
  35.         super.onCreate(savedInstanceState);
  36.  
  37.         //Set layout
  38.         setContentView(R.layout.activity_main);
  39.  
  40.         //Get camera
  41.         View camera = findViewById(R.id.OpenCVCamera);
  42.         if (camera != null && camera instanceof CameraBridgeViewBase) {
  43.             //Configure camera
  44.             mCamera = (CameraBridgeViewBase)camera;
  45.             mCamera.setCvCameraViewListener(this);
  46.  
  47.             requestPermissions(new String[] { Manifest.permission.CAMERA }, 1);
  48.         }
  49.     }
  50.  
  51.     @Override
  52.     protected void onResume() {
  53.         //Super
  54.         super.onResume();
  55.  
  56.         //Try to init
  57.         OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_10, this, mCallback);
  58.     }
  59.  
  60.     @Override
  61.     public void onCameraViewStarted(int width, int height) {
  62.         //Do nothing
  63.     }
  64.  
  65.     @Override
  66.     public void onCameraViewStopped() {
  67.         //Do nothing
  68.     }
  69.  
  70.     @Override
  71.     public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
  72.         //Get edge from the image
  73.         Mat result = new Mat();
  74.         Imgproc.Canny(inputFrame.rgba(), result, 70, 100);
  75.  
  76.         //Return result
  77.         return result;
  78.     }
  79.  
  80.     @Override
  81.     protected void onPause() {
  82.         //Disable camera
  83.         super.onPause();
  84.         if (mCamera != null) mCamera.disableView();
  85.     }
  86.  
  87.     @Override
  88.     protected void onDestroy() {
  89.         //Disable camera
  90.         super.onPause();
  91.         if (mCamera != null) mCamera.disableView();
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement