我正面临相机视图的问题,即相机的视图正在拉伸,并且没有像默认相机应用程序或任何其他相机应用程序给出的普通视图。 我无法获得相机用于拉伸大于屏幕尺寸的表面视图。
enter code here
private class SurfaceCallback implements SurfaceHolder.Callback {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open();
camera.setPreviewDisplay(holder);
camera.setDisplayOrientation(getPreviewDegree(MainActivity.this));
camera.startPreview(); //
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
setCameraParameters();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
private int getPreviewDegree(Activity activity) {
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degree = 0;
switch (rotation) {
case Surface.ROTATION_0:
degree = 90;
break;
case Surface.ROTATION_90:
degree = 0;
break;
case Surface.ROTATION_180:
degree = 270;
break;
case Surface.ROTATION_270:
degree = 180;
break;
}
return degree;
}
private void setCameraParameters() {
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes();
if (sizeList.size() > 0) {
Camera.Size cameraSize = sizeList.get(0);
parameters.setPreviewSize(cameraSize.width, cameraSize.height);
}
sizeList = parameters.getSupportedPictureSizes();
if (sizeList.size() > 0) {
Camera.Size cameraSize = sizeList.get(0);
for (Camera.Size size : sizeList) {
if (size.width * size.height < 10 * 100) {
cameraSize = size;
break;
}
}
parameters.setPictureSize(cameraSize.width, cameraSize.height);
}
parameters.setPictureFormat(ImageFormat.JPEG);
parameters.setJpegQuality(100);
parameters.setJpegThumbnailQuality(100);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
camera.setParameters(parameters);
startOrientationChangeListener();
}
private void startOrientationChangeListener() {
OrientationEventListener mOrEventListener = new OrientationEventListener(MainActivity.this) {
@Override
public void onOrientationChanged(int rotation) {
if (((rotation >= 0) && (rotation <= 45)) || (rotation > 315)) {
rotation = 0;
} else if ((rotation > 45) && (rotation <= 135)) {
rotation = 90;
} else if ((rotation > 135) && (rotation <= 225)) {
rotation = 180;
} else if ((rotation > 225) && (rotation <= 315)) {
rotation = 270;
} else {
rotation = 0;
}
if (rotation == mOrientation)
return;
mOrientation = rotation;
updateCameraOrientation();
}
};
mOrEventListener.enable();
}
private void updateCameraOrientation() {
if (camera != null) {
Camera.Parameters parameters = camera.getParameters();
int rotation = 90 + mOrientation == 360 ? 0 : 90 + mOrientation;
if (mIsFrontCamera) {
if (rotation == 90) rotation = 270;
else if (rotation == 270) rotation = 90;
}
parameters.setRotation(rotation);
camera.setDisplayOrientation(90);
camera.setParameters(parameters);
}
}
答案 0 :(得分:0)
在SurfaceCallback类的surfaceCreated metod中添加SurfaceHoler参数后尝试:
holder = mSurfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
希望它可以帮助你。
答案 1 :(得分:0)