我正在开发一个依赖于相机API的Android应用程序,使用HTC EVO作为我的测试设备。无论我到目前为止尝试过什么,相机预览看起来正确的唯一时间是横向模式(90度旋转,具体而言)。在纵向模式(0度旋转)下,似乎没有办法正确定位预览。
设备上的默认相机应用程序(适用于HTC Sense)允许任何类型的旋转而没有任何问题,因此我知道没有硬件限制。我甚至从HTC的开发人员site下载了一些源代码,但显然它都是C内核的东西。
有人能指出我正确的方向来解决这个问题吗?有没有办法在Android 2.1或2.2中正确旋转预览?
谢谢。
P.S。这是我正在使用的代码,如果它有帮助......
package spikes.cameraSpike03;
import java.util.List;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity implements SurfaceHolder.Callback {
private static final String LOG_TAG = "spikes.cameraSpike03 - MainActivity";
private Camera _camera;
private boolean _previewIsRunning = false;
private SurfaceView _svCameraView;
private SurfaceHolder _surfaceHolder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
_svCameraView = (SurfaceView)findViewById(R.id.svCameraView);
_surfaceHolder = _svCameraView.getHolder();
_surfaceHolder.addCallback(this);
_surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(_previewIsRunning){
_camera.stopPreview();
}
try{
Camera.Parameters parameters = _camera.getParameters();
//Get the optimal preview size so we don't get an exception when setting the parameters
List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
Size optimalPreviewSize = getOptimalPreviewSize(supportedPreviewSizes, width, height);
parameters.setPreviewSize(optimalPreviewSize.width, optimalPreviewSize.height);
_camera.setParameters(parameters);
_camera.setPreviewDisplay(holder);
}
catch(Exception ex){
ex.printStackTrace();
Log.e(LOG_TAG, ex.toString());
}
_camera.startPreview();
_previewIsRunning = true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
_camera = Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
_camera.stopPreview();
_previewIsRunning = false;
_camera.release();
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
}
答案 0 :(得分:5)
如果符合您的设计,请尝试将“活动”的方向锁定在水平位置,或者使用这些方法进行游戏:
private android.hardware.Camera mCamera;
...
mCamera.setDisplayOrientation(90);
params.setRotation(90);
mCamera.setParameters(params);
但我认为setDisplayOrientation可能只是2.2 ......