Android自定义相机结果图像方向
我正在编写一个遵循本教程的自定义相机应用程序: http://marakana.com/forums/android/examples/39.html
现在我可以使用相机并在用户单击按钮后获取图像数据。 但是我遇到了预览是横向的问题,所以我在surfaceCreated中添加了以下代码,
@Override
public void surfaceCreated(SurfaceHolder holder) {
// Open the camera when the preview is created
Log.d("Callback", "[surfaceCreated]");
theCamera = Camera.open();
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int displayRotation = display.getRotation();
switch (displayRotation) {
case Surface.ROTATION_0: theCamera.setDisplayOrientation(90); break;
case Surface.ROTATION_90: break;
case Surface.ROTATION_180: break;
case Surface.ROTATION_270: theCamera.setDisplayOrientation(180); break;
}
}
预览没问题。 但是,当我获取图像数据时,我使用 BitmapFactory.decodeByteArray 来获取位图,并将其设置为在 xml 布局中定义的 imageView。
这是我的 main.xml
<SurfaceView
android:id="@+id/previewSurfaceView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="250px" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Button" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="250px" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Button" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
</LinearLayout>
PreviewSurfaceView是相机预览的小视图
,button1和button2是我点击时的按钮,相机捕获图像并设置imageView1/imageView2对应的imagedata
有一些问题:
我将布局宽度和高度以及最大宽度和高度设置为 100dp,将比例类型设置为 fitCenter。但它似乎没有按照我想要的方式显示。我希望图像显示在边界内。我在布局设置中做错了吗?
即使我以纵向模式拍摄照片,图像也始终为横向。
此外,我想添加一个按钮来切换相机的开/关,但我不知道如何做到这一点。
有人可以为我建议解决方案吗?谢谢。
I'm writing a custom camera application which follow this tutorial:
http://marakana.com/forums/android/examples/39.html
Now I'm able to use the camera and get the image data after user click a button.
But I got the problem that the preview is sideway so I add the following code in surfaceCreated
@Override
public void surfaceCreated(SurfaceHolder holder) {
// Open the camera when the preview is created
Log.d("Callback", "[surfaceCreated]");
theCamera = Camera.open();
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int displayRotation = display.getRotation();
switch (displayRotation) {
case Surface.ROTATION_0: theCamera.setDisplayOrientation(90); break;
case Surface.ROTATION_90: break;
case Surface.ROTATION_180: break;
case Surface.ROTATION_270: theCamera.setDisplayOrientation(180); break;
}
}
The preview was fine.
However when I got the image data, I use BitmapFactory.decodeByteArray
to get the bitmap, and set it to my imageView that is defined in my xml layout.
Here is my main.xml
<SurfaceView
android:id="@+id/previewSurfaceView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="250px" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Button" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="250px" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Button" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
</LinearLayout>
previewSurfaceView is the small view that for the camera preview
button1 and button2 are the button when I clicked on, the camera capture the image and set the imagedata corresponding to imageView1/imageView2
There are some problems:
I set the layout width and height and also the max width and height to 100dp, also the scale type to fitCenter. But it seems not to display as what I want. I want the image is show inside the bound. Am I doing wrong in the layout setting?
The image is always in landscape even I took the picture in portrait mode.
Besides, I want to add a button to toggle the on/off of camera but I can't figure out how to do this.
Can somebody suggest the solution for me? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看我的答案这里,因为它看起来是相同的你遇到的问题。
Have a look at my answer here as it appears to be the same issue you are having.
您需要调用相机参数对象的
setRotation
方法,然后在相机上设置参数。实现
OrientationListener
以获取旋转,在setRotation
中使用该值示例代码用法 此处。
You need to call the method
setRotation
on the camera parameter object and then set the parameter on the camera.Implement the
OrientationListener
to get the rotation use that value insetRotation
Sample code usage here.