In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to use Android Camera to achieve preview box display related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this article on how to use Android Camera to achieve preview box display article will have a harvest, let's take a look at it.
It only takes a few words for Android to preview the Camer interface.
1. First, add permissions to AndroidManifest.xml.
2. Create a xml containing control TextureView
For example, activity_main.xml:
3. Create and use Camera in Activity
(1) use Camera.open (0) to get the Camera object
(2) Camera sets parameters, and finally executes camera.startPreview
(3) just release the object when you close the preview box
For example, the following MainActivity.java code:
Package com.lwz.camera;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import androidx.core.app.ActivityCompat;import android.Manifest;import android.content.Context;import android.content.pm.PackageManager;import android.graphics.SurfaceTexture;import android.hardware.Camera;import android.os.Bundle;import android.util.Log;import android.view.Display;import android.view.TextureView;import android.view.View;import android.view.WindowManager;import android.widget.Toast Public class MainActivity extends AppCompatActivity {private static final String TAG = "Camera2Test"; private TextureView mTextureView; / / Preview box object @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); Log.e (TAG, "onCreate!") ; setContentView (R.layout.activity_main); intiView (); initEvent ();} private void intiView () {mTextureView = (TextureView) findViewById (R.id.textureView) } private void initEvent () {/ / Preview button Click the listening findViewById (R.id.btnStart) .setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Log.i (TAG, "btnStart!") ; startPreview ();}}); / / Click the stop preview button to listen to findViewById (R.id.btnStop) .setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Log.i (TAG, "btnStop!") ; stopPreview ();}}); / / Preview box status monitoring mTextureView.setSurfaceTextureListener (new TextureView.SurfaceTextureListener () {@ Override public void onSurfaceTextureAvailable (@ NonNull SurfaceTexture surface, int width, int height) {Log.i (TAG, "onSurfaceTextureAvailable width =" + width + ", height =" + height)) / / when SurefaceTexture is available, you can set camera parameters and turn on camera handleRequestCamera (surface); / / handleRequestCamera (mTextureView.getSurfaceTexture ()) / / 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) {Log.i (TAG, "onSurfaceTextureSizeChanged width =" + width + ", height =" + height)) } @ Override public boolean onSurfaceTextureDestroyed (@ NonNull SurfaceTexture surface) {Log.i (TAG, "onSurfaceTextureDestroyed!") ; return false;} @ Override public void onSurfaceTextureUpdated (@ NonNull SurfaceTexture surface) {/ / normal preview will always print / / Log.i (TAG, "onSurfaceTextureUpdated!") ;});} Camera mCameram; / / can be used to turn off the open camera and release int mCameraId = 0; private void handleRequestCamera (SurfaceTexture texture) {Log.i (TAG, "handleRequestCamera") / / simple judgment permission if (ActivityCompat.checkSelfPermission (this, Manifest.permission.CAMERA)! = PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions (this, new String [] {"android.permission.CAMERA"}, 100); Log.e (TAG, "openCamera no Permission!") ; Toast.makeText (this, "No camera permission", Toast.LENGTH_LONG). Show (); return;} try {/ / 0 mCameram 1 mCameram = Camera.open (mCameraId); / / it can be used to switch front and rear cameras on mobile phones, depending on the underlying support of Log.i (TAG, "handleRequestCamera mCameraId =" + mCameraId). Camera.Parameters parameters = mCameram.getParameters (); parameters.setPreviewSize (720,720); / / parameters.setPreviewSize (1280); / / different devices have different screen sizes, and some devices with the wrong size will crash mCameram.setParameters (parameters); mCameram.setPreviewTexture (texture); mCameram.startPreview () } catch (Exception error) {Log.e (TAG, "handleRequestCamera error =" + error.getMessage ());}} / * * start Preview * / private void startPreview () {Log.i (TAG, "startPreview"); SurfaceTexture mSurfaceTexture = mTextureView.getSurfaceTexture (); handleRequestCamera (mSurfaceTexture) } / * * stop previewing * choose whether to release or not according to the situation, * you can stopPreview, stop the preview interface, and then use startPreview to restore the preview interface * / private void stopPreview () {if (mCameram! = null) {mCameram.stopPreview (); mCameram.release (); mCameram = null } @ Override protected void onDestroy () {super.onDestroy (); stopPreview (); / / the object to be released when the interface exits}}
It is important to note that before calling Camera.open, 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.
But if Camera.open is called when View or Activity is created in the code, the interface cannot be previewed.
If you need more than one place of code, call Camera.open, and you can normally set a global variable to determine whether SurfaceTexture is available.
This is the end of the article on "how to use Android Camera to achieve preview box display". Thank you for reading! I believe that everyone has a certain understanding of "how to use Android Camera to achieve preview box display" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.