J'ai créé une application de caméra personnalisée à l'aide du code source this , mais sur quelques périphériques (comme sur les périphériques haute résolution), je reçois:
RuntimeException setParameters failed
Je suis confronté à cette exception, pour cette raison:
params.setPictureSize(1200, 900);
Et j'ai remarqué, si j'utilise (1600, 1200)
au lieu de (1200, 900)
, je ne suis pas confronté à de tels problèmes
Logcat:
11-07 11:45:20.630: E/AndroidRuntime(3827): FATAL EXCEPTION: main
11-07 11:45:20.630: E/AndroidRuntime(3827): Java.lang.RuntimeException: Unable to resume activity {pl.gatti.dgcam/pl.gatti.dgcam.DgCamActivity}: Java.lang.RuntimeException: setParameters failed
11-07 11:45:20.630: E/AndroidRuntime(3827): at Android.app.ActivityThread.performResumeActivity(ActivityThread.Java:2825)
11-07 11:45:20.630: E/AndroidRuntime(3827): at Android.app.ActivityThread.handleResumeActivity(ActivityThread.Java:2854)
11-07 11:45:20.630: E/AndroidRuntime(3827): at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2318)
11-07 11:45:20.630: E/AndroidRuntime(3827): at Android.app.ActivityThread.access$600(ActivityThread.Java:144)
11-07 11:45:20.630: E/AndroidRuntime(3827): at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1317)
11-07 11:45:20.630: E/AndroidRuntime(3827): at Android.os.Handler.dispatchMessage(Handler.Java:99)
11-07 11:45:20.630: E/AndroidRuntime(3827): at Android.os.Looper.loop(Looper.Java:152)
11-07 11:45:20.630: E/AndroidRuntime(3827): at Android.app.ActivityThread.main(ActivityThread.Java:5132)
11-07 11:45:20.630: E/AndroidRuntime(3827): at Java.lang.reflect.Method.invokeNative(Native Method)
11-07 11:45:20.630: E/AndroidRuntime(3827): at Java.lang.reflect.Method.invoke(Method.Java:511)
11-07 11:45:20.630: E/AndroidRuntime(3827): at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:793)
11-07 11:45:20.630: E/AndroidRuntime(3827): at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:560)
11-07 11:45:20.630: E/AndroidRuntime(3827): at dalvik.system.NativeStart.main(Native Method)
11-07 11:45:20.630: E/AndroidRuntime(3827): Caused by: Java.lang.RuntimeException: setParameters failed
11-07 11:45:20.630: E/AndroidRuntime(3827): at Android.hardware.Camera.native_setParameters(Native Method)
11-07 11:45:20.630: E/AndroidRuntime(3827): at Android.hardware.Camera.setParameters(Camera.Java:1490)
11-07 11:45:20.630: E/AndroidRuntime(3827): at pl.gatti.dgcam.DgCamActivity.createCamera(DgCamActivity.Java:124)
11-07 11:45:20.630: E/AndroidRuntime(3827): at pl.gatti.dgcam.DgCamActivity.onResume(DgCamActivity.Java:163)
11-07 11:45:20.630: E/AndroidRuntime(3827): at Android.app.Instrumentation.callActivityOnResume(Instrumentation.Java:1185)
11-07 11:45:20.630: E/AndroidRuntime(3827): at Android.app.Activity.performResume(Activity.Java:5182)
11-07 11:45:20.630: E/AndroidRuntime(3827): at Android.app.ActivityThread.performResumeActivity(ActivityThread.Java:2815)
Code:
private void createCamera() {
// Create an instance of Camera
mCamera = getCameraInstance();
// Setting the right parameters in the camera
Camera.Parameters params = mCamera.getParameters();
params.setPictureSize(1200, 900);
params.setPictureFormat(PixelFormat.JPEG);
params.setJpegQuality(85);
mCamera.setParameters(params);
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
// Calculating the width of the preview so it is proportional.
float widthFloat = (float) (deviceHeight) * 4 / 3;
int width = Math.round(widthFloat);
// Resizing the LinearLayout so we can make a proportional preview. This
// approach is not 100% perfect because on devices with a really small
// screen the the image will still be distorted - there is place for
// improvment.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, deviceHeight);
preview.setLayoutParams(layoutParams);
// Adding the camera preview after the FrameLayout and before the button
// as a separated element.
preview.addView(mPreview, 0);
}
Vous pouvez voir complete
CameraActivity code de classe ici
Et voici ma classe CameraPreview
:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_Push_BUFFERS);
mHolder.setFixedSize(100, 100);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the
// preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("DG_DEBUG", "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// make any resize, rotate or reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.setDisplayOrientation(90); // Portrait only
mCamera.startPreview();
} catch (Exception e) {
Log.d("DG_DEBUG", "Error starting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
}
Le problème est causé par:
params.setPictureSize(1200, 900);
car la taille requise n'est pas prise en charge par l'appareil photo.
Utilisez getSupportedPictureSizes
pour obtenir toutes les tailles de prévisualisation disponibles.
Pour vérifier quelle est la taille d'image maximale disponible à partir de l'appareil photo:
List<Size> allSizes = param.getSupportedPictureSizes();
Camera.Size size = allSizes.get(0); // get top size
for (int i = 0; i < allSizes.size(); i++) {
if (allSizes.get(i).width > size.width)
size = allSizes.get(i);
}
//set max Picture Size
params.setPictureSize(size.width, size.height);
Toute application de caméra est limitée par la caméra HAL prise en charge pour ce périphérique. Donc, dans la caméra HAL, nous définissons que nous allons fournir une liste des tailles prises en charge, ces tailles pouvant être la taille de l'aperçu, la taille de l'image ou la taille de la vidéo. Je pense donc que vous êtes confronté à ce problème, car 1200 * 900 n'est pas pris en charge par la caméra HAL ou le code de niveau inférieur.