实际上,我现在在应用程序中面临两个问题。
1)使用前置摄像头时获取RuntimeException。
2)使用后置摄像头拍摄的图像的方向取决于设备,这意味着在某些设备中我获得了很好的方向,但在某些设备中却没有。
根据站点规则,我不应该在一个问题中问两个问题,但是两个问题的问题根源都是相同的setParameters
,所以我在一个问题中问。
代码:
负责捕获图像。
public void TakeImage(){
// try {
Camera.Parameters param;
param = mCamera.getParameters();
param.setRotation(90);
Camera.Size bestSize = null;
List<Camera.Size> sizeList = mCamera.getParameters().getSupportedPreviewSizes();
bestSize = sizeList.get(0);
for (int i = 1; i < sizeList.size(); i++) {
if ((sizeList.get(i).width * sizeList.get(i).height) > (bestSize.width * bestSize.height)) {
bestSize = sizeList.get(i);
}
}
List<Integer> supportedPreviewFormats = param.getSupportedPreviewFormats();
Iterator<Integer> supportedPreviewFormatsIterator = supportedPreviewFormats.iterator();
while (supportedPreviewFormatsIterator.hasNext()) {
Integer previewFormat = supportedPreviewFormatsIterator.next();
if (previewFormat == ImageFormat.YV12) {
param.setPreviewFormat(previewFormat);
}
}
param.setPreviewSize(bestSize.width, bestSize.height);
param.setPictureSize(bestSize.width, bestSize.height);
mCamera.setParameters(param);
mCamera.takePicture(null, null, mPicture);
// }
// catch (Exception e){
// Toast.makeText(getApplicationContext(),"camera capture exception",Toast.LENGTH_SHORT).show();
// }
}
使用前置摄像头时出现LogCate异常:
2019-04-19 10:49:14.054 20679-20679/com.example.clapdetection E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.clapdetection, PID: 20679
java.lang.RuntimeException: setParameters failed
at android.hardware.Camera.native_setParameters(Native Method)
at android.hardware.Camera.setParameters(Camera.java:1980)
at com.example.clapdetection.Activity.CameraActivity.TakeImage(CameraActivity.java:465)
at com.example.clapdetection.Activity.CameraActivity$1$1.onClick(CameraActivity.java:110)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22433)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)