I need to implement an OrientationEventListener
to get the camera working correctly. Google posted a sample implementation of onOrientationChanged
that looks like this:
@Override
public void onOrientationChanged(int orientation) {
if (orientation == ORIENTATION_UNKNOWN) return;
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Ca开发者_开发技巧mera.getCameraInfo(cameraId, info);
orientation = (orientation + 45) / 90 * 90;
int rotation = 0;
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
rotation = (info.orientation - orientation + 360) % 360;
} else { // back-facing camera
rotation = (info.orientation + orientation) % 360;
}
mParameters.setRotation(rotation);
}
But I'm building against API level 8, so I don't have CameraInfo
. How can I accomplish a similar thing as the above without CameraInfo
?
Although the orientation changes, I find I have to also change the camera preview to match, if you want full-screen images on rotation, so I use a similar method but in the onLayout() method of a camera surface view like so:
/*
* This class provides the surface for the camera.
*/
public class CameraSurfaceView extends ViewGroup implements SurfaceHolder.Callback
{
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
if (changed)
{
final int width = right - left;
final int height = bottom - top;
int previewWidth = width;
int previewHeight = height;
if (mPreviewSize != null)
{
Display display = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
switch (display.getRotation())
{
case Surface.ROTATION_0:
previewWidth = mPreviewSize.height;
previewHeight = mPreviewSize.width;
mCamera.setDisplayOrientation(90);
break;
case Surface.ROTATION_90:
previewWidth = mPreviewSize.width;
previewHeight = mPreviewSize.height;
break;
case Surface.ROTATION_180:
previewWidth = mPreviewSize.height;
previewHeight = mPreviewSize.width;
break;
case Surface.ROTATION_270:
previewWidth = mPreviewSize.width;
previewHeight = mPreviewSize.height;
mCamera.setDisplayOrientation(180);
break;
}
}
final int scaledChildHeight = previewHeight * width / previewWidth;
cameraView.layout(0, height - scaledChildHeight, width, height);
}
}
}
This has worked for me on an HTC Desire using Android API 8 (Android 2.2).
public void onOrientationChanged(int orientation) {
if (orientation == ORIENTATION_UNKNOWN) return;
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
parameters.setRotation(degrees );
mcamera.setParameters(parameters);
}
Hope this helps.
EDIT :
Refer the Camera app source code to know how they handled it. This link is to the latest source. Can go back in revisions and see for 2.2 version.
Camera source
EDIT : Here is the link to one of those revisions nearby 2.2 release. Search onOrientationChanged
in this file: Camera src
精彩评论