我解决了这个使用下面的代码:
class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
[...]
public void show(){
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
if (!showing) {
showing=true;
try {
mCamera = Camera.open();
aspectRatio();
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
mCamera.setDisplayOrientation(90);
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getContext(), R.string.text_camera_open_error, Toast.LENGTH_SHORT).show();
}
}
}
private void aspectRatio(){
if (!aspect_radio_corrected){
aspect_radio_corrected=true;
Display default_disp=((Activity)getContext()).getWindowManager().getDefaultDisplay();
Camera.Parameters parameters = mCamera.getParameters();
Camera.Size optimalPreviewSize=parameters.getPreviewSize();
FrameLayout.LayoutParams l_params=(FrameLayout.LayoutParams)getLayoutParams();
float ratio = (float) optimalPreviewSize.height/optimalPreviewSize.width;
l_params.width=default_disp.getWidth();
l_params.height=Math.round(l_params.width/ratio);
setLayoutParams(l_params);
requestLayout();
}
}
[...]
}
而且在布局文件我有:
<com.socialcoaster.utils.CameraPreview
android:id="@+id/cameraView"
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
似乎这是与当前预览大小的纵横比相关的错误安装在相机和surfaceView区域以显示它。你可以看到,只需将aspectRatio()
的值更改为100
等固定值即可。
你有没有找到这方面的解决方案? – seriakillaz
@seriakillaz问题仍然存在,所以我使用内置的相机应用程序来添加图片。 – Youddh