camera bytes

package map.zhongke.myapplication;


import android.graphics.Bitmap;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

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

import static android.graphics.ImageFormat.NV21;

public class MainActivity extends AppCompatActivity implements Camera.PreviewCallback {
    private String  tag="MainActivity:";
    private void log(Object  sd){
        String   vad=sd.toString();
        Log.e(tag,vad);
    }
Camera camera;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        log("oncreate");

    }
  public void openCamera(){
      camera= Camera.open(0);
      Camera.Parameters parameters = camera.getParameters();
      List<Camera.Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
      for (int i = 0; i <supportedPreviewSizes.size() ; i++) {
          System.out.println(supportedPreviewSizes.get(i).height);
          System.out.println(supportedPreviewSizes.get(i).width);
          log("-------");
      }
      //  parameters.setPreviewFpsRange(4, 10);
      parameters.setPreviewFormat(ImageFormat.NV21);
      //    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
      parameters.setPictureFormat(ImageFormat.JPEG);
      camera.setParameters(parameters);
      int weight=camera.getParameters().getPreviewSize().width;
      int height=camera.getParameters().getPreviewSize().height;
      int previewFormat = camera.getParameters().getPreviewFormat();
      int getPictureFormat = camera.getParameters().getPictureFormat();
      /*  int size = weight * height;
        size  = size * ImageFormat.getBitsPerPixel(parameters.getPreviewFormat()) / 8;
        byte[]  bytes=new byte[size];
        camera.addCallbackBuffer(bytes);*/
      //  camera.setPreviewCallbackWithBuffer(this);
      camera.setPreviewCallback(this);
    /*    try {
            camera.setPreviewDisplay(null);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
      camera.startPreview();

      log("previewFormat"+previewFormat);
      log("weight"+weight);
      log("height"+height);
      log("getPictureFormat"+getPictureFormat);
  }
  public void closeCamera(){
      if (camera!=null){
          camera.stopPreview();
          camera.setPreviewCallback(null);
          camera.release();
          camera=null;
      }
  }
    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
          log("onPreviewFrame");
    }
    boolean  isrun =true;
    public void testBtn(View view){
        if (isrun){
            isrun=false;
            openCamera();
        }else {
            isrun=true;
            closeCamera();
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值