很简单直接上源码
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private TextureView mTextureView;
private Camera mCamera;
private SurfaceTexture mSurfaceTexture;
private TextView mTextViewStart, mTextViewStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
mTextViewStart = (TextView) findViewById(R.id.act_main_textview_start);
mTextViewStart.setOnClickListener(new CliclListener());
mTextViewStop = (TextView) findViewById(R.id.act_main_textview_stop);
mTextViewStop.setOnClickListener(new CliclListener());
mTextureView = (TextureView) findViewById(R.id.act_main_textureview);
mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Log.i("aaa", "onSurfaceTextureAvailable width:" + width + " height:" + height);
mSurfaceTexture = surface;
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
Log.i("aaa", "onSurfaceTextureSizeChanged width:" + width + " height:" + height);
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
Log.i("aaa", "onSurfaceTextureDestroyed");
stopPreviewCamera();
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// Log.i("aaa","onSurfaceTextureUpdated");
}
});
}
//开始预览视屏
private void startPreviewCamera() {
mCamera = Camera.open();
Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
Log.i("aaa", "previewSize.width:" + previewSize.width + " previewSize.height:" + previewSize.height);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(previewSize.width, previewSize.height);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
mTextureView.setLayoutParams(layoutParams);
try {
mCamera.setPreviewTexture(mSurfaceTexture);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
mTextureView.setAlpha(1.0f);
mTextureView.setRotation(90.0f);
}
private void stopPreviewCamera() {
mCamera.stopPreview();
mCamera.release();
}
//检查拍照权限
private void checkCameraPermission() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
Log.i("aaa", "已经获取权限");
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, 1);//1 can be another integer
}
}
private class CliclListener implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.act_main_textview_start:
checkCameraPermission();
startPreviewCamera();
break;
case R.id.act_main_textview_stop:
stopPreviewCamera();
mTextureView.setBackgroundColor(Color.WHITE);
break;
}
}
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.wb.textureviewtest">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="22" />
<!-- 增加文件存储和访问摄像头的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>