Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call the external camera by Android

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of Android how to call the external camera, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to call the external camera in Android. Let's take a look.

1. Layout file

2. The main code of the corresponding MainActivity.java is as follows

Package com.deepreality.takephotowithusbcamera;import android.Manifest;import android.graphics.Bitmap;import android.graphics.BitmapFactory;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.TextureView;import android.view.View;import android.widget.ImageButton;import android.widget.Toast;import com.tbruyelle.rxpermissions2.RxPermissions;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException Import java.text.SimpleDateFormat;import java.util.Date;public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener, View.OnClickListener {private static final String TAG = MainActivity.class.getSimpleName (); private Camera mCamera; private ImageButton mPlayButton; private RxPermissions rxPermissions; private int permissionNum; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); rxPermissions = new RxPermissions (MainActivity.this); checkUserAllPermissions () MPlayButton = (ImageButton) findViewById (R.id.play); mPlayButton.setOnClickListener (this); ((TextureView) findViewById (R.id.textureview)) .setSurfaceTextureListener (this);} private void takePic () {if (mCamera! = null) {/ / call capture camera to capture mCamera.takePicture (null, null, pictureCallback) } else {Log.e ("TAG", "Please check the camera!") ;} private Bitmap mBitmap; public Camera.PictureCallback pictureCallback = new Camera.PictureCallback () {@ Override public void onPictureTaken (byte [] data, Camera camera) {Log.i ("ygy", "onPictureTaken"); SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); / / set date format System.out.println (df.format (new Date () / / new Date () to get the current system time String picName = df.format (new Date ()); Toast.makeText (getApplicationContext (), "saving...", Toast.LENGTH_LONG) .show (); mBitmap = BitmapFactory.decodeByteArray (data, 0, data.length); File file = new File ("/ storage/emulated/0/" + picName + ".jpg") Try {file.createNewFile (); BufferedOutputStream os = new BufferedOutputStream (new FileOutputStream (file)); mBitmap.compress (Bitmap.CompressFormat.JPEG, 100, os); os.flush (); os.close (); Toast.makeText (getApplicationContext (), "Image saved successfully", Toast.LENGTH_LONG). Show () } catch (IOException e) {e.printStackTrace ();}; @ Override public void onSurfaceTextureAvailable (SurfaceTexture surface, int width, int height) {mCamera = Camera.open (0); if (mCamera! = null) {try {mCamera.setPreviewTexture (surface); mCamera.startPreview () } catch (IOException e) {Log.d ("TAG", e.getMessage ());}} @ Override protected void onStop () {if (mCamera! = null) {mCamera.stopPreview (); mCamera.release (); mCamera = null;} super.onStop () } @ Override public void onSurfaceTextureSizeChanged (SurfaceTexture surface, int width, int height) {} @ Override public boolean onSurfaceTextureDestroyed (SurfaceTexture surface) {if (mCamera! = null) {mCamera.stopPreview (); mCamera.release (); mCamera = null;} return false } @ Override public void onSurfaceTextureUpdated (SurfaceTexture surface) {} @ Override public void onClick (View v) {if (mCamera = = null) {return;} takePic () } / * check and get user rights * / private void checkUserAllPermissions () {rxPermissions .requestEach (Manifest.permission.WRITE_EXTERNAL_STORAGE) Manifest.permission.CAMERA) .subscribe (permission-> {if (permission.granted) {} else if (permission.shouldShowRequestPermissionRationale) {} else {} permissionNum + +) If (permissionNum = 2) {}});}}

3. Add user rights to AndroidManifest.xml in the manifest file.

This is the end of the article on "how Android invokes the external camera". Thank you for reading! I believe you all have a certain understanding of "how to use external cameras in Android". If you want to learn more, you are 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report