目录
0、相关文章:
TextureView(文章1:阅读量1k,详细)
视频画面帧的展示控件SurfaceView及TextureView对比(文章2:阅读量1w,7赞)
Android控件--TextureView(文章3:阅读量1.3w,5赞)
1、正文:
参照文章1,首先完全根据文章1进行编码,但是出现问题:
- 运行后,显示的黑屏
- 没有申请相机权限
因此在其基础上进行了修改:
项目名称:Common3
1.1、TextureViewActivity.java
public class TextureViewActivity extends AppCompatActivity {
@BindView(R.id.texture_view)
TextureView textureView;
private Camera mCamera;
private static final int PERMISSIONS_REQUEST = 1;
private static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_texture_view);
ButterKnife.bind(this);
if (hasPermission()) {
initDatas();
} else {
requestPermission();
}
}
private void initDatas(){
textureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
private TextureView.SurfaceTextureListener mSurfaceTextureListener =
new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
initBackCamera(surface);
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mCamera.stopPreview();
mCamera.release();
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
};
/**
* 进行相机的初始化配置
*/
private void initBackCamera(SurfaceTexture surface) {
mCamera = Camera.open();
Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
textureView.setLayoutParams(new FrameLayout.LayoutParams(
previewSize.width, previewSize.height, Gravity.CENTER));
try {
mCamera.setPreviewTexture(surface);
} catch (IOException t) {
}
mCamera.startPreview();
textureView.setAlpha(1.0f);
textureView.setRotation(90.0f);
}
private boolean hasPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return checkSelfPermission(PERMISSION_CAMERA) == PackageManager.PERMISSION_GRANTED;
} else {
return true;
}
}
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(PERMISSION_CAMERA)) {
Toast.makeText(
TextureViewActivity.this,
"Camera permission is required for this demo",
Toast.LENGTH_LONG)
.show();
}
requestPermissions(new String[]{PERMISSION_CAMERA}, PERMISSIONS_REQUEST);
}
}
@Override
public void onRequestPermissionsResult(
final int requestCode, final String[] permissions, final int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initDatas();
} else {
requestPermission();
}
}
}
}
1.2、xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gs.common3.aView.TextureViewActivity">
<TextureView
android:id="@+id/texture_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
1.3、清单配置文件
记得加上camera权限