Camera2 教程 4:通過TEXTURE OES方式實現(xiàn)相機預(yù)覽

青橙相機

GLES11Ext.GL_TEXTURE_EXTERNAL_OES的用處是什么?
上一張實現(xiàn)相機預(yù)覽的每一幀的輸出格式是YUV的(YUV)续徽,那么這個擴展紋理的作用就是實現(xiàn)YUV格式到RGB的自動轉(zhuǎn)化饭弓,我們就不需要再為此寫YUV轉(zhuǎn)RGB的代碼了

1. 創(chuàng)建xml和Render

//xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.jdf.camera.ui.CameraV2GLSurfaceView
        android:id="@+id/glsurfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
//Activity
   @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera2_oes);
        //獲取SurfaceView
        glSurfaceView = findViewById(R.id.glsurfaceView);
        //封裝相機操作
        mCameraLoader = new Camera2OESLoader(this);
        //主要是實例化Render诲侮,并綁定
        glSurfaceView.init(mCameraLoader, false, Camera2OESActivity.this);
        findViewById(R.id.saveImage).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageUtils.saveBitmap = true;
            }
        });
    }

//GlSurfaceView
public class CameraV2GLSurfaceView extends GLSurfaceView {
    public static final String TAG = "Filter_CameraV2GLSurfaceView";
    private JImageRender mCameraV2Renderer;

    public void init(Camera2OESLoader camera, boolean isPreviewStarted, Context context) {
        setEGLContextClientVersion(2);

        mCameraV2Renderer = new JImageRender(new JImageFilter(context));
        mCameraV2Renderer.init(this, camera, isPreviewStarted, context);

        setRenderer(mCameraV2Renderer);
    }

    public CameraV2GLSurfaceView(Context context, AttributeSet attributes) {
        super(context, attributes);
    }
}

2 開啟相機

在Activity的onResume中乾戏,判斷glSurfaceView加載完畢后模暗,獲取相機相機傳感器方向岸晦,并且根據(jù)GlSurfaceView大小獲取合適的預(yù)覽大小

//Activity
    @Override
    protected void onResume() {
        super.onResume();
        boolean laidOut = ViewCompat.isLaidOut(glSurfaceView);
        boolean b = !glSurfaceView.isLayoutRequested();
        JLog.d(TAG, "onResume.....laidOut[%b],[%b]",laidOut,b);
        if (laidOut && b) {
            mCameraLoader.onResume(glSurfaceView.getWidth(), glSurfaceView.getHeight());
        } else {
            glSurfaceView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop,
                                           int oldRight, int oldBottom) {
                    JLog.d(TAG, "onResume.....onLayoutChange");
                    glSurfaceView.removeOnLayoutChangeListener(this);
                    mCameraLoader.onResume(glSurfaceView.getWidth(), glSurfaceView.getHeight());
                }
            });
        }
    }

Camera2OESLoader.java

    public void onResume(int width, int height) {
        JLog.d(TAG, "onResume[%d,%d]...", width, height);
        mViewWidth = width;
        mViewHeight = height;
        setUpCamera();
    }


    protected void setUpCamera() {
        try {
            //獲取屏幕方向欧啤,相機傳感器方向,和預(yù)覽大小
            setUpCameraOutputs();
            if (ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                JLog.e(TAG, "Dont have CAMERA permission");
                return;
            }
            mCameraManager.openCamera(mCameraId, mCameraDeviceCallback, mCameraHandler);
        } catch (CameraAccessException e) {
            JLog.e(TAG, "Opening camera (ID: " + mCameraId + ") failed.");
            e.printStackTrace();
        }
    }


mCameraHandler是根據(jù)Camera2OESLoader中HandlerThread創(chuàng)建的启上,因為后續(xù)我們需要再Render線程中啟動預(yù)覽邢隧,為了保證相機打開和啟動預(yù)覽在同一個線程,需要指定同一個Handler

相機啟動成功后碧绞,我們?nèi)ender中創(chuàng)建OES紋理id府框,SufaceTexture和啟動預(yù)覽

3 啟動預(yù)覽

  • JImageRender.onSurfaceCreated

創(chuàng)建OES 紋理id

   @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        JLog.i(TAG, "onSurfaceCreated......");
        mOESTextureId = Utils.createOESTextureObject();
        mFilterEngine.ifNeedInit();

    }
  • JImageRender.onDrawFrame
    然后在onDrawFrame第一次調(diào)用時,完成SurfaceTexture初始化和預(yù)覽啟動
   @Override
    public void onDrawFrame(GL10 gl) {

        Long t1 = System.currentTimeMillis();
        if (mSurfaceTexture != null) {
            mSurfaceTexture.updateTexImage();
            mSurfaceTexture.getTransformMatrix(transformMatrix);
        }

        if (!bIsPreviewStarted) {
            bIsPreviewStarted = initSurfaceTexture();
            bIsPreviewStarted = true;
            return;
        }

        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);

        mFilterEngine.onDraw(transformMatrix,mOESTextureId,glCubeBuffer,glTextureBuffer);

        long t2 = System.currentTimeMillis();
        long t = t2 - t1;
        Log.i(TAG, "onDrawFrame: time: " + t);
    }
  • JImageRender. initSurfaceTexture
  public boolean initSurfaceTexture() {
     
        mSurfaceTexture = new SurfaceTexture(mOESTextureId);
        mCameraLoaer.setPreviewTexture(mSurfaceTexture);
        mCameraLoaer.startPreview();
        return true;
    }
  • Camera2OESLoader.startPreview
 public void startPreview() {
       
        mSurfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        final Surface surface = new Surface(mSurfaceTexture);

        try {

            mCameraDevice.createCaptureSession(Arrays.asList( surface), new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured(@NonNull CameraCaptureSession session) {
                    try {
                        CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                        builder.addTarget(surface);
                        mCaptureRequest = builder.build();
                        mCameraCaptureSession = session;
                        mCameraCaptureSession.setRepeatingRequest(mCaptureRequest, null, mCameraHandler);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onConfigureFailed(@NonNull CameraCaptureSession session) {

                }
            }, mCameraHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

4 繪制渲染

預(yù)覽啟動后成功后讥邻,會更新SurfaceTexture迫靖,并更新紋理數(shù)據(jù)對應(yīng)的紋理舉證,然后將該SurfaceTexture對應(yīng)的OES紋理ID傳遞到OPENGL進(jìn)行繪制

    @Override
    public void onDrawFrame(GL10 gl) {

        Long t1 = System.currentTimeMillis();
        if (mSurfaceTexture != null) {
           //更新SurfaceTexture紋理內(nèi)容
            mSurfaceTexture.updateTexImage();
            mSurfaceTexture.getTransformMatrix(transformMatrix);
        }

        if (!bIsPreviewStarted) {
            bIsPreviewStarted = initSurfaceTexture();
            bIsPreviewStarted = true;
            return;
        }

        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
        //使用opengl繪制mOESTextureId對應(yīng)的紋理內(nèi)容
        mFilterEngine.onDraw(transformMatrix, mOESTextureId,glCubeBuffer,glTextureBuffer)
  
    }

注意:
updateTexImage()方法只能在包OpenGLES環(huán)境的線程里調(diào)用兴使,即Renderer接口所獨立創(chuàng)建的線程當(dāng)中系宜。一般在onDrawFrame中調(diào)用updateTexImage()將數(shù)據(jù)綁定給OpenGLES對應(yīng)的紋理對象

JimageFilter.onDraw

  public void onDraw(float[] transformMatrix, int textureId, FloatBuffer cubeBuffer, FloatBuffer textureBuffer) {
      
        GLES30.glUseProgram(glProgId);
           ....
        if (textureId != OpenGlUtils.NO_TEXTURE) {
            ...
           glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId);
           ...
           glUniformMatrix4fv(uTextureMatrixLocation, 1, false, transformMatrix, 0);
        }
         ...
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);

    }

onDraw過程與上一節(jié)的相機預(yù)覽流程一致,主要差別有兩點

  1. 綁定和解綁紋理id的類型為GL_TEXTURE_EXTERNAL_OES
  2. 需要額外將從SurfaceTexture中產(chǎn)生的變換矩陣uTextureMatrixLocation傳遞到著色器中

5 定義OES著色器

因為紋理id使用的是OES格式发魄,著色器中紋理類型也要對應(yīng)修改盹牧,相對于上以上相機預(yù)覽實現(xiàn),作色器主要有兩個變化

頂點著色器中励幼,需要傳遞SurfaceTexture產(chǎn)生的變換矩陣對象uTextureMatrix汰寓,并根據(jù)矩陣值調(diào)整紋理坐標(biāo)位置

attribute vec4 position;
uniform mat4 uTextureMatrix;
attribute vec4 inputTextureCoordinate;
varying vec2 textureCoordinate;
void main()
{
  textureCoordinate = (uTextureMatrix * inputTextureCoordinate).xy;
  gl_Position = position;
}`

片元著色器中,需要聲明紋理的類型為samplerExternalOES 苹粟,而不是sampler2D; 需要在文件頭聲明使用了samplerExternalOES有滑,否則加載著色器會報錯

#extension GL_OES_EGL_image_external : require
precision mediump float;
uniform samplerExternalOES inputImageTexture;
varying vec2 textureCoordinate;
void main()
{
  gl_FragColor = texture2D(inputImageTexture, textureCoordinate);
}

6 顯示效果

預(yù)覽效果

7 流程總結(jié)

相機OES預(yù)覽.png

主要流程為:

  1. 定義GlSurfaceView和定義OEG紋理類型的著色器

  2. 將GlSurfaceView和Render,設(shè)置渲染模式

  3. 在GlSurfaceView加載完畢后,啟動相機

  4. 在Render的onSurfaceCreate中嵌削,完成OES紋理id創(chuàng)建和初始化Opengl對象

  5. 在相機開啟成功的時候毛好,在onDrawFrame中完成預(yù)覽啟動望艺;之所以在onDrawFrame啟動,是因為其他階段肌访,相機不一定啟動成功

  6. 預(yù)覽啟動成功后找默,會在Render的每一幀調(diào)用時,更新SurfaceTexture的內(nèi)容吼驶,并更新對應(yīng)的OES紋理id惩激;將紋理id傳遞到opengl對象中進(jìn)行渲染

  7. 渲染成功后,預(yù)覽數(shù)據(jù)會顯示到屏幕上

8 代碼位置

代碼具體實現(xiàn)參考 QCCamera中的JCamera2OESFboActivity

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末旨剥,一起剝皮案震驚了整個濱河市咧欣,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌轨帜,老刑警劉巖魄咕,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蚌父,居然都是意外死亡哮兰,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門苟弛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來喝滞,“玉大人,你說我怎么就攤上這事膏秫∮以猓” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵缤削,是天一觀的道長窘哈。 經(jīng)常有香客問我,道長亭敢,這世上最難降的妖魔是什么滚婉? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮帅刀,結(jié)果婚禮上让腹,老公的妹妹穿的比我還像新娘。我一直安慰自己扣溺,他們只是感情好骇窍,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著锥余,像睡著了一般像鸡。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天只估,我揣著相機與錄音,去河邊找鬼着绷。 笑死蛔钙,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的荠医。 我是一名探鬼主播吁脱,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼彬向!你這毒婦竟也來了兼贡?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤娃胆,失蹤者是張志新(化名)和其女友劉穎遍希,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體里烦,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡凿蒜,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了胁黑。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片废封。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖丧蘸,靈堂內(nèi)的尸體忽然破棺而出漂洋,到底是詐尸還是另有隱情,我是刑警寧澤力喷,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布刽漂,位于F島的核電站,受9級特大地震影響冗懦,放射性物質(zhì)發(fā)生泄漏爽冕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一披蕉、第九天 我趴在偏房一處隱蔽的房頂上張望颈畸。 院中可真熱鬧,春花似錦没讲、人聲如沸眯娱。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽徙缴。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間于样,已是汗流浹背疏叨。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留穿剖,地道東北人蚤蔓。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像糊余,于是被迫代替她去往敵國和親秀又。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容