Video Capture of Android Audio and Video (System API Preview)

  • 2021-10-27 08:54:32
  • OfStack

We know the basic knowledge related to video. In the following articles, we should be able to collect our video with audio. The video comes from one frame and one frame. We should first learn to preview the video, and then collect one frame of picture. It is simple to collect video to understand this problem. First of all, the first reaction is to open Google search and Android video capture. We need to know how to capture through API, and involuntarily go to Camera API in Android official website. Android has two video capture API, Camera was used before Android 5.0, and now it has been abandoned. We still have to learn how to use it. Camera2 is the latest video capture API, and we focus on its use. In this article, we master the calling system of photographing and recording video API to achieve photographing and recording functions.

Camera

It is the API used by API21 (Android5.0) to collect camera data. The key contents from the beginning to each link are recorded as follows.

Basic knowledge

Let's first understand how many classes are associated with Camera.
Camera: Old API control camera device after API21
SurfaceView: Display camera preview image to user
MediaRecorder: Recording video from camera

Privilege declaration

Camera permissions: We must declare 1 permission to use Camera devices


<uses-permission android:name="android.permission.CAMERA" />

However, when we use Intent to call the system's own Camera device to take pictures and videos, we don't need this permission.
Camera Features: Applications must declare the right to use camera features (this does not know what it means to understand the meaning of this manifest file uses-feature)


<uses-feature android:name="android.hardware.camera" />

Audio recording permission: When recording video, we need audio to add this permission.


<uses-permission android:name="android.permission.RECORD_AUDIO" />

Storage Permission: If we want to save photos and videos on the storage device, we must add this permission.


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Location permissions: If the photo label requires GPS location information, we need the following permissions


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...
<!-- Needed only if your app targets Android 5.0 (API level 21) or higher. -->
<uses-feature android:name="android.hardware.location.gps" />

Call the camera app of the system to take pictures and record videos

Photographing

Request camera characteristics


<manifest ... >
 <uses-feature android:name="android.hardware.camera"
     android:required="true" />
 ...
</manifest>

This permission allows GooglePlay to judge whether the device supports downloading our application. If required is set to true, then 1 must have camera hardware equipment to download. If required is set to false, then those without camera hardware equipment can also download. Of course, we have to judge whether there is a camera available in the program.

Start taking pictures with the default Intent

Call the default Intent that opens the system to take pictures of App


static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
  startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
 }
}

Get a photo picture

We just took a picture through startActivityForResult, and naturally received the returned data at onActivityResult. We displayed the picture on an ImageView


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
  Bundle extras = data.getExtras();
  Bitmap imageBitmap = (Bitmap) extras.get("data");
  mImageView.setImageBitmap(imageBitmap);
 }
}

With this default photo, we do not need to declare any permissions on machines above Android 6.0 to perform successfully.

Customize the path to save photos

In the above operation, we got an bitmap, and our picture information is operated in memory. If we want to save the photographed pictures to the memory card and view the pictures, then we only need to declare an OK for writing the memory card.


<manifest ...>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 ...
</manifest>

Start re-requesting the photo code


static final int REQUEST_TAKE_PHOTO = 1;

private void dispatchTakePictureIntent() {
 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 // Ensure that there's a camera activity to handle the intent
 if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
  // Create the File where the photo should go
  File photoFile = null;
  try {
   photoFile = createImageFile();
  } catch (IOException ex) {
   // Error occurred while creating the File
   ...
  }
  // Continue only if the File was successfully created
  if (photoFile != null) {
   Uri photoURI = FileProvider.getUriForFile(this,
             "com.example.android.fileprovider",
             photoFile);
   takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
   startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
  }
 }
}

String mCurrentPhotoPath;

private File createImageFile() throws IOException {
 // Create an image file name
 String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
 String imageFileName = "JPEG_" + timeStamp + "_";
 File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
 File image = File.createTempFile(
  imageFileName, /* prefix */
  ".jpg",   /* suffix */
  storageDir  /* directory */
 );

 // Save a file: path for use with ACTION_VIEW intents
 mCurrentPhotoPath = image.getAbsolutePath();
 return image;
}

For the above code, we use the FileProvider. getUriForFile method, which returns content://URI. If this API is used above Android 7.0 without processing, it will throw FileUriExposedException. We want to register and configure 1 FileProvider in the manifest file


<uses-feature android:name="android.hardware.camera" />
0

In the res/xml/file_paths configuration file


<uses-feature android:name="android.hardware.camera" />
1

Add a photo to an album

The root directory of our above photo saving location is getExternalFilesDir (Environment.DIRECTORY_PICTURES); Multimedia scanners under this directory can't find our photos because they are private to our App. The following code allows the system's multimedia scanner to add our pictures to the Media Provider 's database so that our pictures can be used for system photo albums and other applications.


<uses-feature android:name="android.hardware.camera" />
2

Decoding scaled picture

We did not do any processing on the picture ImageView. If the picture is larger, it will lead to oom, do 1 zoom processing.


private void setPic() {
 // Get the dimensions of the View
 int targetW = mImageView.getWidth();
 int targetH = mImageView.getHeight();

 // Get the dimensions of the bitmap
 BitmapFactory.Options bmOptions = new BitmapFactory.Options();
 bmOptions.inJustDecodeBounds = true;
 BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
 int photoW = bmOptions.outWidth;
 int photoH = bmOptions.outHeight;

 // Determine how much to scale down the image
 int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

 // Decode the image file into a Bitmap sized to fill the View
 bmOptions.inJustDecodeBounds = false;
 bmOptions.inSampleSize = scaleFactor;
 bmOptions.inPurgeable = true;

 Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
 mImageView.setImageBitmap(bitmap);
}

demo code reference

Video recording

The code for recording video playback is very simple. If you want to customize the video player, it will take more than 1 thing for a long time. Now we can simply play the video recorded by calling the system.

Turn on video recording Intent


<uses-feature android:name="android.hardware.camera" />
4

Receive video Uri in onActivityResult to play


<uses-feature android:name="android.hardware.camera" />
5

demo link view


Related articles: