Skip to content

Instantly share code, notes, and snippets.

@figonzal1
Created April 16, 2024 14:28
Show Gist options
  • Save figonzal1/61f6405584c77685ca706756ea7d4057 to your computer and use it in GitHub Desktop.
Save figonzal1/61f6405584c77685ca706756ea7d4057 to your computer and use it in GitHub Desktop.
OpenCV Camera Running in Fragment
package cl.tempus.testopencvjava.ui.main;
import static androidx.core.content.ContextCompat.getSystemService;
import static org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import static org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import static cl.tempus.testopencvjava.utils.MyUtils.detectObjects;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.RotatedRect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import java.util.Collections;
import java.util.List;
import cl.tempus.testopencvjava.databinding.FragmentOpencvCameraBinding;
/**
* A simple {@link Fragment} subclass.
* Use the {@link OpenCVCameraFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class OpenCVCameraFragment extends Fragment implements CvCameraViewListener2 {
private FragmentOpencvCameraBinding binding;
private CameraBridgeViewBase mOpenCvCameraView;
public OpenCVCameraFragment() {
// Required empty public constructor
}
public static OpenCVCameraFragment newInstance() {
return new OpenCVCameraFragment();
}
protected List<? extends CameraBridgeViewBase> getCameraViewList() {
return Collections.singletonList(mOpenCvCameraView);
}
@Override
public void onStart() {
super.onStart();
List<? extends CameraBridgeViewBase> cameraViews = getCameraViewList();
for (CameraBridgeViewBase cameraBridgeViewBase : cameraViews) {
if (cameraBridgeViewBase != null) {
cameraBridgeViewBase.setCameraPermissionGranted();
}
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentOpencvCameraBinding.inflate(inflater, container, false);
View root = binding.getRoot();
requireActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_FULLSCREEN);
mOpenCvCameraView = binding.tutorial1ActivityJavaSurfaceView;
mOpenCvCameraView.setCvCameraViewListener(this);
return root;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null) mOpenCvCameraView.disableView();
}
@Override
public void onPause() {
super.onPause();
if (mOpenCvCameraView != null) mOpenCvCameraView.disableView();
}
@Override
public void onResume() {
super.onResume();
if (mOpenCvCameraView != null) mOpenCvCameraView.enableView();
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
@Override
public void onCameraViewStarted(int width, int height) {
Log.d("CAMERA_FRAGMENT", "Camera view started");
}
@Override
public void onCameraViewStopped() {
Log.d("CAMERA_FRAGMENT", "Camera view stopped");
}
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
Mat frame = inputFrame.rgba();
List<MatOfPoint> objectsContours = detectObjects(frame);
// Crea una nueva matriz para el frame rotado
Mat rotatedFrame = new Mat();
// Obtiene el punto central del frame
Point center = new Point(frame.cols() / 2.0, frame.rows() / 2.0);
// Define el ángulo de rotación
double angle = -90; // Ángulo de rotación de 90 grados
// Obtiene la matriz de rotación
Mat rotationMatrix = Imgproc.getRotationMatrix2D(center, angle, 1);
// Aplica la rotación al frame
Imgproc.warpAffine(frame, rotatedFrame, rotationMatrix, frame.size());
for (MatOfPoint cnt : objectsContours) {
MatOfPoint2f cnt2f = new MatOfPoint2f(cnt.toArray());
RotatedRect rect = Imgproc.minAreaRect(cnt2f);
//Draw center circle
Imgproc.circle(rotatedFrame, rect.center, 15, new Scalar(0, 0, 255), -1);
// Obtener los puntos del rectángulo
Point[] points = new Point[4];
rect.points(points);
// Dibujar rectangulo en telefono
for (int i = 0; i < 4; ++i) {
Imgproc.line(rotatedFrame, points[i], points[(i + 1) % 4], new Scalar(255, 0, 0), 2);
}
double height = rect.boundingRect().height;
double width = rect.boundingRect().width;
Imgproc.putText(rotatedFrame, "Width: " + width + "px", new Point(rect.center.x - 100, rect.center.y - 60), 2, 1.0, new Scalar(0, 255, 0), 2);
Imgproc.putText(rotatedFrame, "Height: " + height + "px", new Point(rect.center.x - 100, rect.center.y - 25), 2, 1.0, new Scalar(0, 255, 0), 2);
}
return rotatedFrame;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment