This article shares the specific code displayed in the Android Camera1 implementation preview box for your reference. The specific content is as follows
It is actually very simple to preview the Camer interface on Android, and it only takes a few words.
1. First, add permissions
<uses-permission android:name=""/>
2. Create an XML containing control TextureView
For example activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent"> <TextureView android: android:layout_width="match_parent" android:layout_height="match_parent" /> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="0.8dp" android:text="stop preview" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true"/> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="0.8dp" android:text="start preview" android:layout_alignParentBottom="true" android:layout_toStartOf="@id/btnStop"/> </RelativeLayout>
3. Create and use Camera in Activity
(1) Use (0) to get the Camera object
(2) Camera sets parameters and finally executes
(3) Just release the object when closing the preview box
For example, the following code:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends AppCompatActivity { private static final String TAG = "Camera2Test"; private TextureView mTextureView; //Preview box object @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); (TAG, "onCreate!"); setContentView(.activity_main); intiView(); initEvent(); } private void intiView() { mTextureView = (TextureView) findViewById(); } private void initEvent() { //Click the preview button to listen findViewById().setOnClickListener(new () { @Override public void onClick(View v) { (TAG, "btnStart!"); startPreview(); } }); //Stop preview button click to listen findViewById().setOnClickListener(new () { @Override public void onClick(View v) { (TAG, "btnStop!"); stopPreview(); } }); //Preview box status monitoring (new () { @Override public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) { (TAG, "onSurfaceTextureAvailable width = " + width + ",height = " + height); //When SurefaceTexture is available, you can set the camera parameters and turn on the camera handleRequestCamera(surface); //handleRequestCamera(()); //If it is in the same class as mTextureView, the effect is the same as above } @Override public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) { (TAG, "onSurfaceTextureSizeChanged width = " + width + ",height = " + height); } @Override public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) { (TAG, "onSurfaceTextureDestroyed!"); return false; } @Override public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) { //When you preview normally, you will keep printing //(TAG, "onSurfaceTextureUpdated!"); } }); } Camera mCameram; //It can be used to turn off and release the camera on. int mCameraId = 0; private void handleRequestCamera(SurfaceTexture texture) { (TAG, "handleRequestCamera"); //Simple judgment permission if ((this, ) != PackageManager.PERMISSION_GRANTED) { (this, new String[]{""}, 100); (TAG, "openCamera no Permission!"); (this, "No camera permission", Toast.LENGTH_LONG).show(); return; } try { //0/1/2 mCameram = (mCameraId);//It can be used to switch front and rear cameras on the mobile phone. Different devices depend on the underlying support status. (TAG, "handleRequestCamera mCameraId = " + mCameraId); parameters = (); (720, 1280); // (1280, 720);// Different devices have different screen sizes, and some devices will crash if they set the wrong size. (parameters); (texture); (); } catch (Exception error) { (TAG, "handleRequestCamera error = " + ()); } } /** * Start preview */ private void startPreview() { (TAG, "startPreview"); SurfaceTexture mSurfaceTexture = (); handleRequestCamera(mSurfaceTexture); } /** * Stop preview * Choose whether to release according to the situation. * You can stopPreview and stop the preview interface. Use startPreview to restore the preview interface later. */ private void stopPreview() { if (mCameram != null) { (); (); mCameram = null; } } @Override protected void onDestroy() { (); stopPreview();//The interface exits and releases the object } }
It should be noted that before calling, make sure that the preview box is ready.
That is, the onSurfaceTextureAvailable method has been called back, and there is no problem when the normal interface is displayed.
However, if it is called when the View or Activity is created in the code, the interface cannot be previewed at this time.
If you need multiple codes and call them, you can set a global variable to determine whether the SurfaceTexture is available.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.