Android:HTC EVO 上的相机预览方向(Android 2.1 或 2.2)
我正在开发一个依赖于相机 API 的 Android 应用程序,使用 HTC EVO 作为我的测试设备。到目前为止,无论我尝试过什么,相机预览看起来正确的唯一一次是在横向模式下(具体来说,旋转 90 度)。在纵向模式(0 度旋转)下,似乎无法正确定向预览。
设备上的默认相机应用程序(适用于 HTC Sense)允许任何类型的旋转,没有任何问题,所以我知道没有硬件限制。我什至从 HTC 的开发人员 网站 下载了一些源代码,但显然这些都是 C 内核的东西。
谁能指出我解决这个问题的正确方向?有没有办法在 Android 2.1 或 2.2 中正确旋转预览?
谢谢。
PS这是我正在使用的代码,以防有帮助......
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;
}
}
I am developing an Android application that relies on the camera API, using an HTC EVO as my testing device. No matter what I've tried so far, the only time the camera preview looks right is in landscape mode (90 degrees rotation, to be specific). It seems as though there is no way to orient the preview correctly when in portrait mode (0 degrees rotation).
The default camera application on the device (for HTC Sense) allows for any kind of rotation without any problem, so I know that there is no hardware limitation. I even downloaded some source code from HTC's developer site, but it's all in C - kernel stuff, apparently.
Can anyone point me in the right direction for solving this problem? Is there a way to rotate the preview correctly in Android 2.1 or 2.2?
Thank you.
P.S. Here is the code I'm using, in case it helps...
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果适合您的设计,请尝试将 Activity 的方向锁定为水平方向,或者尝试一下:
...
但我认为 setDisplayOrientation 可能只是 2.2...
Try locking the orientation of the Activity in horizontal if that fits with your design, or have a play with these:
...
But I think setDisplayOrientation might be 2.2 only...