1. 程式人生 > >利用TextureView來實現拍照功能

利用TextureView來實現拍照功能

很簡單直接上原始碼

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>

效果圖