Android Camera開發(fā)系列:調(diào)整Camera預覽方向

有時候我們想根據(jù)自己的需要調(diào)整下Camera的預覽方向,那么是調(diào)用哪個API可以達到我們的目的呢迷殿?

我們看下下圖拍的幾張小可愛的照片乱顾,分別是正常方向、旋轉(zhuǎn)180度绪妹、90度拍的照片晚碾。

?
正常方向
旋轉(zhuǎn)180度
旋轉(zhuǎn)90度

一、Camera API

Camera1上喂急,我們可以通過setDisplayOrientation(int degress)格嘁;來設置camera預覽的方向。

mCamera.setDisplayOrientation(Surface.ROTATION_180);

這里也貼下源碼里面關于setDisplayOrientaion接口的詳細說明廊移。

 /**
     * Set the clockwise rotation of preview display in degrees. This affects
     * the preview frames and the picture displayed after snapshot. This method
     * is useful for portrait mode applications. Note that preview display of
     * front-facing cameras is flipped horizontally before the rotation, that
     * is, the image is reflected along the central vertical axis of the camera
     * sensor. So the users can see themselves as looking into a mirror.
     *
     * <p>This does not affect the order of byte array passed in {@link
     * PreviewCallback#onPreviewFrame}, JPEG pictures, or recorded videos. This
     * method is not allowed to be called during preview.
     *
     * <p>If you want to make the camera image show in the same orientation as
     * the display, you can use the following code.
     * <pre>
     * 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);
     * }
     * </pre>
     *
     * <p>Starting from API level 14, this method can be called when preview is
     * active.
     *
     * <p><b>Note: </b>Before API level 24, the default value for orientation is 0\. Starting in
     * API level 24, the default orientation will be such that applications in forced-landscape mode
     * will have correct preview orientation, which may be either a default of 0 or
     * 180\. Applications that operate in portrait mode or allow for changing orientation must still
     * call this method after each orientation change to ensure correct preview display in all
     * cases.</p>
     *
     * @param degrees the angle that the picture will be rotated clockwise.
     *                Valid values are 0, 90, 180, and 270.
     * @throws RuntimeException if setting orientation fails; usually this would
     *    be because of a hardware or other low-level error, or because
     *    release() has been called on this Camera instance.
     * @see #setPreviewDisplay(SurfaceHolder)
     */
    public native final void setDisplayOrientation(int degrees);

二糕簿、Camera2 API

Camera2的API上,找了一通狡孔,發(fā)現(xiàn)并沒有像Camera1上懂诗,可以通過類似的接口來設置預覽方向,不過可以通過TextureView.setTransform(matrix);來通過調(diào)整textureView的顯示來達到目的苗膝。

  public void openCamera(){
        CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
        try {
           ......
//調(diào)整預覽畫面顯示方向
configureTextureViewTransform(mTextureView.getWidth(),mTextureView.getHeight());
            manager.openCamera(cameraId, mStateCallback, null);
           ......
        }
}

private void configureTextureViewTransform(int viewWidth, int viewHeight) {
        if (null == mTextureView) {
            return;
        }
        int rotation = 0 ;/*activity.getWindowManager().getDefaultDisplay().getRotation();*/
        Matrix matrix = new Matrix();
        RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
        RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
        float centerX = viewRect.centerX();
        float centerY = viewRect.centerY();
        if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
            bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
            matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
            float scale = Math.max(
                    (float) viewHeight / mPreviewSize.getHeight(),
                    (float) viewWidth / mPreviewSize.getWidth());
            matrix.postScale(scale, scale, centerX, centerY);
            matrix.postRotate(90 * (rotation - 2), centerX, centerY);
        }else if (Surface.ROTATION_180 == rotation) {
            matrix.postRotate(180, centerX, centerY);
        }
        mTextureView.setTransform(matrix);
    }

*本人從事Android Camera相關開發(fā)已有5年殃恒,
*目前在深圳上班,
*小伙伴記得點我頭像辱揭,看【個人介紹】進行關注哦离唐,希望和更多的小伙伴一起交流 ~
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市问窃,隨后出現(xiàn)的幾起案子亥鬓,更是在濱河造成了極大的恐慌,老刑警劉巖域庇,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嵌戈,死亡現(xiàn)場離奇詭異,居然都是意外死亡听皿,警方通過查閱死者的電腦和手機熟呛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來尉姨,“玉大人庵朝,你說我怎么就攤上這事。” “怎么了偿短?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵欣孤,是天一觀的道長。 經(jīng)常有香客問我昔逗,道長降传,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任勾怒,我火速辦了婚禮婆排,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘笔链。我一直安慰自己段只,他們只是感情好,可當我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布鉴扫。 她就那樣靜靜地躺著赞枕,像睡著了一般。 火紅的嫁衣襯著肌膚如雪坪创。 梳的紋絲不亂的頭發(fā)上炕婶,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天,我揣著相機與錄音莱预,去河邊找鬼柠掂。 笑死,一個胖子當著我的面吹牛依沮,可吹牛的內(nèi)容都是我干的涯贞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼危喉,長吁一口氣:“原來是場噩夢啊……” “哼宋渔!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起姥饰,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤傻谁,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后列粪,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡谈飒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年岂座,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片杭措。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡费什,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出手素,到底是詐尸還是另有隱情鸳址,我是刑警寧澤瘩蚪,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站稿黍,受9級特大地震影響疹瘦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜巡球,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一言沐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧酣栈,春花似錦险胰、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至窖维,卻和暖如春缨睡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背陈辱。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工奖年, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人沛贪。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓陋守,卻偏偏與公主長得像,于是被迫代替她去往敵國和親利赋。 傳聞我的和親對象是個殘疾皇子水评,可洞房花燭夜當晚...
    茶點故事閱讀 45,060評論 2 355