Android 音視頻開發(四):使用 Camera API 採集視頻資料

來源:互聯網
上載者:User

標籤:一點   resize   release   try   void   mission   ack   儲存   his   

本文主要將的是:使用 Camera API 採集視頻資料並儲存到檔案,分別使用 SurfaceView、TextureView 來預覽 Camera 資料,取到 NV21 的資料回調。

註: 需要許可權:<uses-permission android:name="android.permission.CAMERA" />

一、預覽 Camera 資料

做過Android開發的人一般都知道,有兩種方法能夠做到這一點:SurfaceView、TextureView。

下面是使用SurfaceView預覽資料的方式:

SurfaceView surfaceView;
Camera camera;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

surfaceView = (SurfaceView) findViewById(R.id.surface_view);
surfaceView.getHolder().addCallback(this);

// 開啟網路攝影機並將展示方向旋轉90度
camera = Camera.open();
camera.setDisplayOrientation(90);

}

//------ Surface 預覽 -------
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}


@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int w, int h) {

}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
camera.release();
}

下面是使用TextureView預覽資料的方式:

    TextureView textureView;    Camera camera;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textureView = (TextureView) findViewById(R.id.texture_view);        textureView.setSurfaceTextureListener(this);// 開啟網路攝影機並將展示方向旋轉90度        camera = Camera.open();        camera.setDisplayOrientation(90);    }
//------ Texture 預覽 ------- @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) { try { camera.setPreviewTexture(surfaceTexture); camera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { camera.release(); return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { }

 

Android 音視頻開發(四):使用 Camera API 採集視頻資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.