diff --git a/.idea/modules.xml b/.idea/modules.xml index 5d72818..85e2023 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,8 +3,11 @@ + + + \ No newline at end of file diff --git a/app/src/main/java/com/rw/imagingsample/MainActivity.java b/app/src/main/java/com/rw/imagingsample/MainActivity.java index 29263dd..508bbce 100644 --- a/app/src/main/java/com/rw/imagingsample/MainActivity.java +++ b/app/src/main/java/com/rw/imagingsample/MainActivity.java @@ -12,11 +12,18 @@ import android.os.Bundle; import android.util.Log; import android.view.TextureView; +import android.view.View; import com.rw.imaging.CameraController; import com.rw.imaging.ImagingUtils; import com.rw.imagingsample.databinding.ActivityMainBinding; +import org.opencv.android.Utils; +import org.opencv.core.Core; +import org.opencv.core.CvType; +import org.opencv.core.Mat; +import org.opencv.imgproc.Imgproc; + public class MainActivity extends AppCompatActivity implements CameraController.PreviewCallback { private ActivityMainBinding mBinding; @@ -28,6 +35,26 @@ protected void onCreate(Bundle savedInstanceState) mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); + mBinding.frontCam.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + stopCamera(); + startCamera(CameraController.CameraType.Front, 90); + } + }); + + mBinding.backCam.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + stopCamera(); + startCamera(CameraController.CameraType.Default, 90); + } + }); + } @Override @@ -41,31 +68,46 @@ protected void onResume() } else { - startCamera(); + startCamera(CameraController.CameraType.Front, 90); } } - @Override - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) - { - super.onRequestPermissionsResult(requestCode, permissions, grantResults); - - if(grantResults[0] == PackageManager.PERMISSION_GRANTED) - startCamera(); - } +// @Override +// public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) +// { +// super.onRequestPermissionsResult(requestCode, permissions, grantResults); +// +// if(grantResults[0] == PackageManager.PERMISSION_GRANTED) +// startCamera(); +// } @SuppressWarnings("MissingPermission") - private void startCamera() + private void startCamera(CameraController.CameraType type, int rotation) { - CameraController.connect(CameraController.CameraType.Front, 90); + Log.d("IMG", "opencv version: " + ImagingUtils.getVersion()); + + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) + { + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1); + } + else + { + CameraController.connect(type, rotation); - CameraController.PreviewSize p = new CameraController.PreviewSize(640, 480);// CameraController.getBestPreviewSize(640, 480); + CameraController.PreviewSize p = new CameraController.PreviewSize(640, 480);// CameraController.getBestPreviewSize(640, 480); - Log.d("IMG", "best preview: " + p.width+"x"+p.width); + Log.d("IMG", "best preview: " + p.width + "x" + p.width); - CameraController.setPreviewCallback(this); + CameraController.setPreviewCallback(this); + + CameraController.startPreview(mBinding.previewMain, p); + } + } - CameraController.startPreview(mBinding.previewMain, p); + private void stopCamera() + { + CameraController.stopPreview(); + CameraController.disconnect(); } @Override @@ -73,8 +115,7 @@ protected void onPause() { super.onPause(); - CameraController.stopPreview(); - CameraController.disconnect(); + stopCamera(); } @Override @@ -85,17 +126,15 @@ public void onCameraFrame(byte[] frame, CameraController.PreviewSize previewSize long t = System.currentTimeMillis(); ImagingUtils.PreviewResizeParams p = new ImagingUtils.PreviewResizeParams.Builder() -// .setTargetSize(c.getWidth(), c.getHeight()) + .setTargetSize(c.getWidth(), c.getHeight()) .setBounds(0, (previewSize.width-previewSize.height)/2, previewSize.height, previewSize.width - (previewSize.width-previewSize.height)/2) .setRotation(ImagingUtils.PreviewResizeParams.Rotation.Clockwise_90) .create(); Bitmap b = ImagingUtils.rotateCropAndResizePreview(frame, previewSize.width, previewSize.height, p); + c.drawBitmap(b, 0, 0, null); - c.drawBitmap(b, new Rect(0, 0, b.getWidth(), b.getHeight()), new Rect(0, 0, c.getWidth(), c.getHeight()), null); -// c.drawBitmap(b, 0, 0, null); - Log.d("IMG", "processing time: " + (System.currentTimeMillis() - t)); mBinding.previewCustom.unlockCanvasAndPost(c); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8e45339..cc02afb 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -58,5 +58,24 @@ app:layout_constraintLeft_toLeftOf="@+id/preview_custom" app:layout_constraintRight_toRightOf="@+id/preview_custom" app:layout_constraintTop_toBottomOf="@+id/preview_custom"/> + +