Android Camera实现旋转角度

本文实例为大家分享了Android Camera实现旋转角度的具体代码,供大家参考,具体内容如下

概述

相机图像数据都是来自于图像传感器(Image Sensor),相机模组出厂的时候有一个默认的取景方向,一般为以下两种,请留意相机模组中小人的方向

  • Sensor 安装默认都是 Sensor 的长边与手机的长边平行
  • 将上述图1的模组装入手机,结果为下图

  • 两颗模组不一定如图左右摆放,也可以上下摆放,只要遵循长边对长边即可
  • 此时使用后摄预览或拍照,取景方向是正常的,而手机目前相对自然方向(正常竖屏使用状态)顺时针夹角为90度,这也就是常说的 Sensor orientation 是90度

将上述图2的模组装入手机,结果为下图

  • 两颗模组不一定如图左右摆放,也可以上下摆放,只要遵循长边对长边即可
  • 此时使用后摄预览或拍照,若要使取景方向正常,需将手机顺时针旋转180度,此时手机相对自然方向(正常竖屏使用状态)顺时针夹角为270度,这也就是常说的 Sensor orientation 是270度

旋转角度规律

  • 以下说明以 Sensor orientation 90度为例(大多数sensor都是该情况)
  • 屏幕显示旋转角度:Activity#getWindowManager().getDefaultDisplay().getRotation()的值,可以是 ROTATION_0(正常竖屏使用状态)、ROTATION_90(手机向右侧放)、ROTATION_180(手机竖屏倒置)、ROTATION_270(手机向左侧放)
  • 以屏幕角度 ROTATION_180且使用后摄为例,其他情况类比推理

当前情况下图1模组中的小人头部朝向左边,有两种方式判断当前sensor取景后图像方向

简单方式:跟随小人的视角去看实际被拍摄的物体(假设为正常站立的人),所看到的景象是头部向右横置的人,此时若要使看到的图像恢复为正常情况,则需要将图像顺时针旋转270度

复杂方式:sensor扫描方向遵从小人头部左侧顶点向右扫描,当前情况下也就是从左下向上逐行扫描,然后依次存储到内存中,存储为图片的时候是水平从左向右存储,导致存储后的图像是头部向右横置的人,若要使图像被拍摄后为正常情况,则需要将图像顺时针旋转270度

代码实现

Camera API1(官方实现)

public static void setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    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;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360;  // compensate the mirror
    } else {  // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
}

Camera API2

Camera API2 不需要经过任何预览画面方向的矫正,就可以正确现实画面,因为当使用 TextureView 或者 SurfaceView 进行画面预览的时候,系统会自动矫正预览画面的方向

private static final SparseIntArray ORIENTATIONS = new SparseIntArray();

// Conversion from screen rotation to JPEG orientation.
static {
    ORIENTATIONS.append(Surface.ROTATION_0, 90);
    ORIENTATIONS.append(Surface.ROTATION_90, 0);
    ORIENTATIONS.append(Surface.ROTATION_180, 270);
    ORIENTATIONS.append(Surface.ROTATION_270, 180);
}

/**
 * Retrieves the JPEG orientation from the specified screen rotation.
 *
 * @param rotation The screen rotation.
 * @return The JPEG orientation (one of 0, 90, 270, and 360)
 */
private int getOrientation(int rotation) {
    // Sensor orientation is 90 for most devices, or 270 for some devices (eg. Nexus 5X)
    // We have to take that into account and rotate JPEG properly.
    // For devices with orientation of 90, we simply return our mapping from ORIENTATIONS.
    // For devices with orientation of 270, we need to rotate the JPEG 180 degrees.
    return (ORIENTATIONS.get(rotation) + mSensorOrientation + 270) % 360;
}

final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android编程实现RotateAnimation设置中心点旋转动画效果

    本文实例讲述了Android编程实现RotateAnimation设置中心点旋转动画效果.分享给大家供大家参考,具体如下: 在xml设置: <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="800" // 设置动画

  • Android 图片缩放与旋转的实现详解

    本文使用Matrix实现Android实现图片缩放与旋转.示例代码如下: 复制代码 代码如下: package com.android.matrix;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Matrix;import android.graphics.drawable.BitmapDrawable

  • Android编程中调用Camera时预览画面有旋转问题的解决方法

    本文实例讲述了Android编程中调用Camera时预览画面有旋转问题的解决方法.分享给大家供大家参考,具体如下: 在调用Camera写应用的时候,前后摄像头的情况有时候是不一样的.有时候,明明后摄像头没有问题,而调用到前摄像头时,却倒转了180°,或者其他角度,百思不得其解.在查看了Android源码之后,发现它的解决办法很是好,接下来贴个源码,以备日后查看. public static int getDisplayRotation(Activity activity) { int rotat

  • Android部分手机拍照后获取的图片被旋转问题的解决方法

    调用Android系统拍照功能后,三星手机拍摄后的照片被旋转了90度,横着拍给你变成竖的,竖的拍给你变成横的.其它品牌的手机都是正常的,就三星出现这个怪事. 在Android适配上,我原来一直以为国内的小米手机够奇葩了,结果还有更奇葩的!你说你没事旋转照片干啥,实在是猜不透其居心何在,纯粹是在给开发者制造麻烦啊! 解决办法是获取到拍照后照片被旋转的角度,再旋转回去就好了. 具体思路: 1.首先在调用拍照方法时,保存拍照后的相片原图,得到原图路径,(PhotoBitmapUtils是我自己写的一个

  • Android开发 旋转屏幕导致Activity重建解决方法

    Android开发文档上专门有一小节解释这个问题.简单来说,Activity是负责与用户交互的最主要机制,任何"设置"(Configuration)的改变都可能对Activity的界面造成影响,这时系统会销毁并重建Activity以便反映新的Configuration. "屏幕方向"(orientation)是一个Configuration,通过查看Configuration类的javadoc可以看到其他Configuration还有哪些:如fontScale.ke

  • Android中利用matrix 控制图片的旋转、缩放、移动

    本文主要讲解利用android中Matrix控制图形的旋转缩放移动,具体参见一下代码: 复制代码 代码如下: /**  * 使用矩阵控制图片移动.缩放.旋转  */  public class CommonImgEffectView extends View { private Context context ;      private Bitmap mainBmp , controlBmp ;      private int mainBmpWidth , mainBmpHeight , c

  • Android实现图片反转、翻转、旋转、放大和缩小

    ********************************************************************** android 实现图片的翻转 ********************************************************************** Resources res = this.getContext().getResources(); img = BitmapFactory.decodeResource(res, R.

  • Android Tween动画之RotateAnimation实现图片不停旋转效果实例介绍

    主要介绍Android中如何使用rotate实现图片不停旋转的效果.Android 平台提供了两类动画,一类是 Tween 动画,即通过对场景里的对象不断做图像变换(平移.缩放.旋转)产生动画效果:第二类是 Frame 动画,即顺序播放事先做好的图像,跟电影类似.本文分析 Tween动画的rotate实现旋转效果. 在新浪微博客户端中各个操作进行中时activity的右上角都会有个不停旋转的图标,类似刷新的效果,给用户以操作中的提示.这种非模态的提示方式推荐使用,那么下面就分享下如何实现这种效果

  • Android开发之图形图像与动画(二)Animation实现图像的渐变/缩放/位移/旋转

    Android 平台提供了两类动画. 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转.平移.放缩和渐变). 下面就讲一下Tweene Animations. 主要类: Animation 动画 AlphaAnimation 渐变透明度 RotateAnimation 画面旋转 ScaleAnimation 渐变尺寸缩放 TranslateAnimation 位置移动 AnimationSet 动画集 一.AlphaAnimation 其中AlphaAnimatio

  • Android实现屏幕旋转方法总结

    本文实例总结了Android实现屏幕旋转方法.分享给大家供大家参考.具体如下: 在介绍之前,我们需要先了解默认情况下android屏幕旋转的机制: 默认情况下,当用户手机的重力感应器打开后,旋转屏幕方向,会导致当前activity发生onDestroy-> onCreate,这样会重新构造当前activity和界面布局,如果在Camera界面,则表现为卡顿或者黑屏一段时间.如果是在横竖屏UI设计方面,那么想很好地支持屏幕旋转,则建议在res中建立layout-land和layout-port两个

随机推荐