如何在纵向模式下在 Surfaceholder 上获得相机预览?
我正在使用以下自定义 SurfaceView 来执行某些 AR 任务。我在 AndroidManifest 中为我的 Activity 设置了 android:screenOrientation="portrait"
,但图像以奇怪的长宽比旋转了 90 度。我正在使用三星 Galaxy S...
有人可以帮忙吗?
public class CustomCameraView extends SurfaceView
{
Camera camera;
SurfaceHolder previewHolder;
SurfaceHolder.Callback surfaceHolderListener = new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open();
camera.setPreviewDisplay(previewHolder);
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int w, int h)
{
Parameters params = camera.getParameters();
params.setPreviewSize( 800, 480);
params.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(params);
camera.startPreview();
}
public void surfaceDestroyed(SurfaceHolder arg0)
{
camera.stopPreview();
camera.release();
}
};
public CustomCameraView(Context context)
{
super(context);
previewHolder = this.getHolder();
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
previewHolder.addCallback(surfaceHolderListener);
}
protected void onDraw (Canvas canvas)
{
super.onDraw(canvas);
}
public void closeCamera()
{
if(camera != null)
camera.release();
}
}
I'm using the following custom SurfaceView for some AR task. I set android:screenOrientation="portrait"
for my activity in the AndroidManifest, but the image is rotated by 90 deg with weird aspect ratio. I'm using a Samsung Galaxy S...
Anybody can help?
public class CustomCameraView extends SurfaceView
{
Camera camera;
SurfaceHolder previewHolder;
SurfaceHolder.Callback surfaceHolderListener = new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open();
camera.setPreviewDisplay(previewHolder);
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int w, int h)
{
Parameters params = camera.getParameters();
params.setPreviewSize( 800, 480);
params.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(params);
camera.startPreview();
}
public void surfaceDestroyed(SurfaceHolder arg0)
{
camera.stopPreview();
camera.release();
}
};
public CustomCameraView(Context context)
{
super(context);
previewHolder = this.getHolder();
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
previewHolder.addCallback(surfaceHolderListener);
}
protected void onDraw (Canvas canvas)
{
super.onDraw(canvas);
}
public void closeCamera()
{
if(camera != null)
camera.release();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
许多设备上的相机应用程序都有一个缺陷,它只能在横向模式下正确显示,将显示锁定为横向模式可以修复此问题,但还有其他问题(例如强制您管理布局)
Stackflow 讨论 1
Stackflow 讨论 2
等等...
一些设备+sdk 版本按照纵向工作网络上的文档和示例,有些没有,我只是希望他们不会,并实现了仅景观路线,有点痛苦,但它有效。
The camera application on many devices has a defect where it will only properly display in landscape mode, locking the display to landscape mode fixes this but has other problems (like forcing you to manage the layout)
Stackflow Discussion 1
Stackflow Discussion 2
Etc ...
Some devices+sdk versions work in portrait as per the doc's and samples around the web, some don't I have just come to expect they won't and implemented the landscape only route, a bit of a pain but it works.