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.
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 in setRotation
Sample code usage here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.