我正在尝试修复我的相机应用程序。当我在横向模式下握住相机时,它会在屏幕上横向预览。我在堆栈上找到了这个修复:Android - Camera preview is sideways
在我的CameraSurfaceView
类构造函数中,我得到了这样的表面this.Surface = getHolder();
。在该surfaceChanged
方法中,我根据 Surface.ROTATION_<#> 检查显示旋转 0、90、180 和 270 度(方法如下)。但是,每个ROTATION
Surface 常量都带有以下错误下划线ROTATION_<#> cannot be resolved or is not a field
。我不确定我做错了什么。我CameraSurfaceView
的活动与我的活动是分开的,所以我想知道显示是否与它有关?希望这被正确解释,如果您需要更多代码或任何内容,请告诉我。我提前感谢这一点!
import java.io.IOException;
import java.util.List;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ZoomControls;
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
int orientation = 0;
Camera.Parameters params = Cam.getParameters();
WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
android.view.Display display = window.getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0)
{
params.setPreviewSize(height, width);
Cam.setDisplayOrientation(90);
}
if(display.getRotation() == Surface.ROTATION_90)
{
params.setPreviewSize(height, width);
}
if(display.getRotation() == Surface.ROTATION_180)
{
params.setPreviewSize(height, width);
}
if(display.getRotation() == Surface.ROTATION_270)
{
params.setPreviewSize(height, width);
Cam.setDisplayOrientation(180);
}
List<Camera.Size> previewSizes = params.getSupportedPreviewSizes();
Camera.Size previewSize = getBestPreviewSize(width, height);
params.setPreviewSize(previewSize.width, previewSize.height);
int zoom = 0;
params.setZoom(zoom);
zoomControls.setOnZoomInClickListener(new View.OnClickListener(){
public void onClick(View v){
Camera.Parameters params = Cam.getParameters();
maxZoomLevel = params.getMaxZoom();
if(currentZoomLevel < maxZoomLevel){
currentZoomLevel++;
Cam.startSmoothZoom(currentZoomLevel);
}
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener(){
public void onClick(View v){
Camera.Parameters params = Cam.getParameters();
maxZoomLevel = params.getMaxZoom();
if(currentZoomLevel > 0){
currentZoomLevel--;
Cam.startSmoothZoom(currentZoomLevel);
}
}
});
Cam.setParameters(params);
try {
Cam.setPreviewDisplay(Surface);
} catch (IOException e) {
e.printStackTrace();
}
Cam.startPreview();
}