About getParameters failed (empty parameters)
I am trying to use a camera capture app using SurfaceView.
Application overview
-
It can detect Face in real time (on the camera screen).
-
It can store a movie.
I am using android.media.MediaRecord class to save movie.
myRecorder = new MediaRecorder();
myRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
myRecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/Movies/sample.3gp");
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
myRecorder.setVideoFrameRate(30);
myRecorder.setVideoSize(320, 240);
myRecorder.prepare();
myRecorder.start();
after avobe step, RuntimeException occurs in next step (camera is android.hardware.Camera object)
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
//Log.d(TAG, "onPreviewFrame: ");
int width = camera.getParameters().getPreviewSize().width; <--
int height = camera.getParameters().getPreviewSize().height;
03-22 22:54:09.134 27875-27875/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: wbcompany.co.jp.facedetector3, PID: 27875
java.lang.RuntimeException: getParameters failed (empty parameters)
at android.hardware.Camera.native_getParameters(Native Method)
at android.hardware.Camera.getParameters(Camera.java:2019)
at wbcompany.co.jp.facedetector3.CameraView.onPreviewFrame(CameraView.java:150)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1192)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6189)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
before calling 'myRecorder.start ()', this exception is not thrown.
I have no idea about this solution. Please give me a solution to this problem.
My runtime: Android 7.0 / API level 24
source to share
This is a weird error message, but the case is very real. When the camera is busy in the MediaRecorder, it will not be available for other purposes.
Generally speaking, accessing camera options can be very inefficient on some devices. It is highly recommended not to name camera.getParameters()
a preview for every frame. Create local fields in your CameraView class or in the activity that embeds it, and keep the width and height when the preview starts. They will not change unless you explicitly stop the camera and change its configuration.
If I don't miss something, your callback onPreviewFrame()
happens on the main thread (UI). It is good practice to call Camera.open()
on a separate HandlerThread so that the preview processing slows down the UI.
source to share