The first is the camera class, which has been deprecated on 5.0, not recommended but still supported. The latest is the Camera2 class, I have not studied, it is said that the usage is completely different, but also a pit jump into another pit. Above is the introduction and the required permissions, remember to add permissions. Step in the following diagram: said the general process to use this class, three marked important entries, follow the steps to go. Note that 5th, Pass a fully initialized surfaceholder, need a fully initialized surface, that is, you need to wait until surface initialization is complete before you call Setpreviewdisplay (Surfaceholder).
So it's best to put Opencamera's work in the Surfaceholder callback: like here:
@Override Public void surfacecreated (Surfaceholder holder) {System. out. println ("surfacecreated"= camera.open ();}
And what we're going to do is configure the camera.
Private voidInitcamera () {Parameters=camera.getparameters ();p Arameters.setflashmode ("off");//No Flashparameters.setwhitebalance (Camera.Parameters.WHITE_BALANCE_AUTO);p Arameters.setscenemode ( Camera.Parameters.SCENE_MODE_AUTO);p Arameters.setfocusmode (Camera.Parameters.FOCUS_MODE_AUTO); Parameters.setpreviewformat (IMAGEFORMAT.YV12);p arameters.setpicturesize (Camwidth, camHeight); Parameters.setpreviewsize (Camwidth, camheight);//these two properties will be error if these two properties are set differently than the real phonecamera.setparameters (parameters);//Automatic adjustment of the screen lensif( This. Getresources (). GetConfiguration (). Orientation! =Configuration.orientation_landscape) {parameters.Set("Orientation","Portrait");//Parameters.Set("Rotation", -);//lens angle to 90 degrees (the default camera is the horizontal beat)Camera.setdisplayorientation ( -);//more than 2.2 can be used}Else//if it is a horizontal screen{parameters.Set("Orientation","Landscape");//Camera.setdisplayorientation (0);//more than 2.2 can be used}byte[] buf =New byte[Camwidth * Camheight *3/2];camera.addcallbackbuffer (BUF); Camera.setpreviewcallback ( This);}
These are the things I call Open in surfacechanged:
@Override Public voidSurfacechanged (Surfaceholder holder,intFormatintWidthintheight) {System. out. println ("surfacechanged"); Initcamera ();Try{camera.setpreviewdisplay (holder); Camera.startpreview (); System. out. println ("Startpreview");} Catch(IOException e) {e.printstacktrace ();}}
We initialized the camera and set the Setpreviewcallback, so our processing of the image or the processing of the video can be camera.previewcallback its callback
@Override Public void onpreviewframe (byte[] data, Camera camera) {ifnull) { return;} System. out. println ("onpreviewframe"); int ret = avccodec.offerencoder (data, h264);}
Inside to do the processing, before setting the Camera.setpreviewcallbackwithbuffer (); The result onpreviewframe is only called once in open, and the reason for the day is found .... Only to find out it was here. First of all, follow-up do encode Add. GitHub resp Added.https://github.com/yocn/camerapreview
Preview of the camera with Surfaceview and camera mates