-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Video capture #69
Closed
Closed
Video capture #69
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2ea8b74
video capture test
YushraJewon fb341a5
add missing imports
YushraJewon 8c139a5
add camera-video in plugin xml
YushraJewon 3aa4f2f
remove space
YushraJewon ddcce21
setQuality lowest and remove toast
YushraJewon b3a39f0
Merge branch 'master' of github.com:spoonconsulting/cordova-plugin-si…
7ce1e7a
indentation and suggestions
d87739f
resolve conflict move videoRecorder setup in setUp camera
03d77e9
indent
b891b4c
remove exception err from videoCallback and remove torch from videoCa…
029c815
remove flash from js options
041a975
remove unused codes and set recording null after recording
fd3f009
separate start and stop video capture
a67b68d
stopVideoCapture
004489f
Merge branch 'master' of github.com:spoonconsulting/cordova-plugin-si…
27c03ad
using a single callback
3594f47
Merge branch 'master' of github.com:spoonconsulting/cordova-plugin-si…
9667c56
remove first line define plugin and set videocallback to null and vid…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
package com.spoon.simplecamerapreview; | ||
|
||
import android.Manifest; | ||
import android.annotation.SuppressLint; | ||
import android.content.ContentValues; | ||
import android.content.Context; | ||
import android.content.pm.PackageManager; | ||
import android.content.res.Configuration; | ||
import android.graphics.Point; | ||
import android.hardware.camera2.CameraCharacteristics; | ||
import android.location.Location; | ||
import android.net.Uri; | ||
import android.os.Bundle; | ||
import android.provider.MediaStore; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.util.Log; | ||
|
@@ -29,8 +33,17 @@ | |
import androidx.camera.core.ImageCaptureException; | ||
import androidx.camera.core.Preview; | ||
import androidx.camera.lifecycle.ProcessCameraProvider; | ||
import androidx.camera.video.FileOutputOptions; | ||
import androidx.camera.video.Quality; | ||
import androidx.camera.video.QualitySelector; | ||
import androidx.camera.video.Recorder; | ||
import androidx.camera.video.Recording; | ||
import androidx.camera.video.VideoCapture; | ||
import androidx.camera.video.VideoRecordEvent; | ||
import androidx.camera.view.PreviewView; | ||
import androidx.core.app.ActivityCompat; | ||
import androidx.core.content.ContextCompat; | ||
|
||
import androidx.exifinterface.media.ExifInterface; | ||
import androidx.fragment.app.Fragment; | ||
|
||
|
@@ -51,6 +64,11 @@ interface CameraCallback { | |
void onCompleted(Exception err, String nativePath); | ||
} | ||
|
||
interface VideoCallback { | ||
void onStart(Boolean recording, String nativePath); | ||
void onStop(Boolean recording, String nativePath); | ||
} | ||
|
||
interface CameraStartedCallback { | ||
void onCameraStarted(Exception err); | ||
} | ||
|
@@ -76,6 +94,9 @@ public class CameraPreviewFragment extends Fragment { | |
private PreviewView viewFinder; | ||
private Preview preview; | ||
private ImageCapture imageCapture; | ||
private VideoCapture<Recorder> videoCapture; | ||
Recording recording = null; | ||
ProcessCameraProvider cameraProvider = null; | ||
private Camera camera; | ||
private CameraStartedCallback startCameraCallback; | ||
private Location location; | ||
|
@@ -86,6 +107,9 @@ public class CameraPreviewFragment extends Fragment { | |
private static float ratio = (4 / (float) 3); | ||
private static final String TAG = "SimpleCameraPreview"; | ||
private String captureDevice; | ||
private String filename; | ||
private File videoFile; | ||
private VideoRecordEvent videoRecordEvent; | ||
|
||
public CameraPreviewFragment() { | ||
|
||
|
@@ -120,13 +144,11 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c | |
viewFinder.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)); | ||
containerView.addView(viewFinder); | ||
startCamera(); | ||
|
||
return containerView; | ||
} | ||
|
||
public void startCamera() { | ||
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(getActivity()); | ||
ProcessCameraProvider cameraProvider = null; | ||
|
||
try { | ||
cameraProvider = cameraProviderFuture.get(); | ||
|
@@ -136,8 +158,8 @@ public void startCamera() { | |
startCameraCallback.onCameraStarted(new Exception("Unable to start camera")); | ||
return; | ||
} | ||
setUpCamera(captureDevice,cameraProvider); | ||
|
||
setUpCamera(captureDevice,cameraProvider); | ||
preview.setSurfaceProvider(viewFinder.getSurfaceProvider()); | ||
|
||
if (startCameraCallback != null) { | ||
|
@@ -258,6 +280,59 @@ public void hasFlash(HasFlashCallback hasFlashCallback) { | |
hasFlashCallback.onResult(camera.getCameraInfo().hasFlashUnit()); | ||
} | ||
|
||
public void startVideoCapture(VideoCallback videoCallback) { | ||
if (recording != null) { | ||
recording.stop(); | ||
recording = null; | ||
return; | ||
} | ||
UUID uuid = UUID.randomUUID(); | ||
|
||
String filename = uuid.toString() + ".mp4"; | ||
if (ActivityCompat.checkSelfPermission(this.getContext(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { | ||
ActivityCompat.requestPermissions(this.getActivity(), new String[]{Manifest.permission.RECORD_AUDIO}, 200); | ||
} | ||
File videoFile = new File( | ||
getContext().getFilesDir(), | ||
filename | ||
); | ||
|
||
FileOutputOptions outputOptions = new FileOutputOptions.Builder(videoFile).build(); | ||
|
||
recording = videoCapture.getOutput() | ||
.prepareRecording(this.getContext().getApplicationContext(), outputOptions) | ||
.withAudioEnabled() | ||
.start(ContextCompat.getMainExecutor(this.getContext()), videoRecordEvent -> { | ||
if (videoRecordEvent instanceof VideoRecordEvent.Start) { | ||
videoCallback.onStart(true, null); | ||
} else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) { | ||
VideoRecordEvent.Finalize finalizeEvent = (VideoRecordEvent.Finalize) videoRecordEvent; | ||
if (finalizeEvent.hasError()) { | ||
// Handle the error | ||
int errorCode = finalizeEvent.getError(); | ||
Throwable errorCause = finalizeEvent.getCause(); | ||
Log.e(TAG, "Video recording error: " + errorCode, errorCause); | ||
} else { | ||
// Handle video saved | ||
videoCallback.onStop(false, Uri.fromFile(videoFile).toString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to move this line after the log(line 317) |
||
Uri savedUri = finalizeEvent.getOutputResults().getOutputUri(); | ||
Log.i(TAG, "Video saved to: " + savedUri); | ||
} | ||
recording = null; | ||
} | ||
// Other event types can be handled if needed | ||
}); | ||
|
||
} | ||
public void stopVideoCapture() { | ||
if (recording !=null) { | ||
recording.stop(); | ||
recording = null; | ||
} | ||
} | ||
|
||
|
||
|
||
public void takePicture(boolean useFlash, CameraCallback takePictureCallback) { | ||
if (torchActivated) { | ||
useFlash = true; | ||
|
@@ -361,7 +436,7 @@ public void switchCameraTo(String device, CameraSwitchedCallback cameraSwitchedC | |
@SuppressLint("RestrictedApi") | ||
public void setUpCamera(String captureDevice, ProcessCameraProvider cameraProvider) { | ||
CameraSelector cameraSelector; | ||
if (captureDevice != null && captureDevice.equals("ultra-wide-angle")) { | ||
if (captureDevice.equals("ultra-wide-angle")) { | ||
cameraSelector = new CameraSelector.Builder() | ||
.addCameraFilter(cameraInfos -> { | ||
List<Camera2CameraInfoImpl> backCameras = new ArrayList<>(); | ||
|
@@ -398,18 +473,24 @@ public void setUpCamera(String captureDevice, ProcessCameraProvider cameraProvid | |
targetResolution = CameraPreviewFragment.calculateResolution(getContext(), targetSize); | ||
} | ||
|
||
Recorder recorder = new Recorder.Builder() | ||
.setQualitySelector(QualitySelector.from(Quality.LOWEST)) | ||
.build(); | ||
videoCapture = VideoCapture.withOutput(recorder); | ||
|
||
|
||
preview = new Preview.Builder().build(); | ||
imageCapture = new ImageCapture.Builder() | ||
.setTargetResolution(targetResolution) | ||
.build(); | ||
|
||
cameraProvider.unbindAll(); | ||
try { | ||
camera = cameraProvider.bindToLifecycle( | ||
getActivity(), | ||
cameraSelector, | ||
preview, | ||
imageCapture | ||
imageCapture, | ||
videoCapture | ||
); | ||
} catch (IllegalArgumentException e) { | ||
// Error with result in capturing image with default resolution | ||
|
@@ -420,9 +501,9 @@ public void setUpCamera(String captureDevice, ProcessCameraProvider cameraProvid | |
getActivity(), | ||
cameraSelector, | ||
preview, | ||
imageCapture | ||
imageCapture, | ||
videoCapture | ||
); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.