1. 程式人生 > >android獲取攝像頭視訊幀資料

android獲取攝像頭視訊幀資料

1.思路

一.通過android的Camera.setPreviewCallback(PreviewCallback cb)監聽回撥.

二.此回撥函式public void onPreviewFrame(byte[] data, Camera camera)每回調一次就是獲取到攝像頭的一幀原始資料即data位元組陣列。

三.這個位元組陣列經過YuvImage類包裝壓縮成特定型別圖片。

2.程式碼實現

一.許可權

 <uses-permission android:name="android.permission.CAMERA"/>

二.佈局

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.yuncai.menjin.camera_demo.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:gravity="center"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="200dp"
            android:layout_height="200dp">

            <TextureView
                android:id="@+id/texture_view"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_gravity="center_horizontal" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:text="TextureView(攝像頭預覽)"
                android:textColor="#f00"
                android:textSize="10sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="200dp"
            android:layout_height="200dp">

            <ImageView
                android:id="@+id/iv_pic"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_gravity="right"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:text="ImageView(視訊幀資料預覽)"
                android:textColor="#f00"
                android:textSize="10sp" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="bottom"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_takePic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="獲取視訊幀資料" />
    </LinearLayout>
</merge>

三.java實現程式碼

package com.yuncai.menjin.camera_demo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {

    private TextureView textureView;
    private ImageView ivPic;
    private Button btnTakePic;
    private Camera mCamera;

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

    private void initListener() {
        textureView.setSurfaceTextureListener(this);
        btnTakePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCallBack();
            }
        });

    }

    private void initData() {
        int numberOfCameras = Camera.getNumberOfCameras();// 獲取攝像頭個數
        if(numberOfCameras<1){
            Toast.makeText(this, "沒有相機", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
    }

    private void initView() {
        textureView = (TextureView) findViewById(R.id.texture_view);
//        textureView.setRotation(0); // // 設定預覽角度,並不改變獲取到的原始資料方向(與Camera.setDisplayOrientation(0)相同)
        ivPic = (ImageView) findViewById(R.id.iv_pic);
        btnTakePic = (Button) findViewById(R.id.btn_takePic);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        // 開啟相機 0後置 1前置
        mCamera = Camera.open(0);
        if (mCamera != null) {
            // 設定相機預覽寬高,此處設定為TextureView寬高
            Camera.Parameters params = mCamera.getParameters();
            params.setPreviewSize(width, height);
            // 設定自動對焦模式
            List<String> focusModes = params.getSupportedFocusModes();
            if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
                params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                mCamera.setParameters(params);
            }
            try {
//                mCamera.setDisplayOrientation(0);// 設定預覽角度,並不改變獲取到的原始資料方向
                // 繫結相機和預覽的View
                mCamera.setPreviewTexture(surface);
                // 開始預覽
                mCamera.startPreview();
            } catch (IOException e) {
                e.printStackTrace();
            }
         }
    }

    private void addCallBack() {
        if(mCamera!=null){
            mCamera.setPreviewCallback(new Camera.PreviewCallback() {
                @Override
                public void onPreviewFrame(byte[] data, Camera camera) {
                    Camera.Size size = camera.getParameters().getPreviewSize();
                    try{
                        YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null);
                        if(image!=null){
                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                            image.compressToJpeg(new Rect(0, 0, size.width, size.height), 80, stream);
                            Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
                            ivPic.setImageBitmap(bmp);
                            stream.close();
                        }
                    }catch(Exception e){
                       e.printStackTrace();
                    }
                }
            });
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mCamera.stopPreview();
        mCamera.release();
        return false;
    }
    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {}
}

3.效果