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(); } } }
camera bytes
最新推荐文章于 2021-09-12 10:26:07 发布