注意:前面存在sdk註冊和各種狀態預設,被我省略了,這裏只展示核心代碼
1.效果圖:
2.主界面:
package com.example.administrator.testz;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.view.TextureView;
import androidx.annotation.Nullable;
import dji.sdk.camera.VideoFeeder;
import dji.sdk.codec.DJICodecManager;
/**
* Created by wrs on 15/1/2020,下午 6:20
* projectName: Testz
* packageName: com.example.administrator.testz
*/
@SuppressLint("Registered")
public class CameraActivity extends Activity implements TextureView.SurfaceTextureListener {
private DJICodecManager codecManager = null;
private VideoFeeder.VideoDataListener videoDataListener = null;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
TextureView mVideoSurface = (TextureView) findViewById(R.id.texture_video_previewer_surface);
if (null != mVideoSurface) {
mVideoSurface.setSurfaceTextureListener(this);
// This callback is for
videoDataListener = new VideoFeeder.VideoDataListener() {
@Override
public void onReceive(byte[] bytes, int size) {
if (null != codecManager) {
codecManager.sendDataToDecoder(bytes, size);
}
}
};
}
initSDKCallback();
}
private void initSDKCallback() {
try {
VideoFeeder.getInstance().getPrimaryVideoFeed().addVideoDataListener(videoDataListener);
} catch (Exception ignored) {
}
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
if (codecManager == null) {
codecManager = new DJICodecManager(getApplicationContext(), surface, width, height);
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
if (codecManager != null) {
codecManager.cleanSurface();
codecManager = null;
}
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
}
3.佈局頁面|;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/texture_video_previewer_surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"/>
</LinearLayout>
end