Android realizes the function of taking pictures and recording videos
- 2021-09-11 21:25:42
- OfStack
In this paper, we share the specific codes of Android to take pictures and record videos for your reference. The specific contents are as follows
Camera in the document
To use Camera, let's first look at what is described in the documentation below 1. Compared with most other classes, Camera is described in detail in the document, including the steps needed in the use process. Of course, this also shows that it is tedious in actual use.
First, you need to declare the following permissions and attributes in AndroidManifest. xml:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Then, to take pictures, you need the following 10 steps:
1. An example is obtained by open (int) method
2. Get the default settings through the getParameters () method
3. If necessary, modify the Camera. Parameters object returned above and call setParameters (Camera. Parameters) to set
4. If necessary, call setDisplayOrientation (int) to set the display direction
5. This step is very important. Pass in an initialized SurfaceHolder through setPreviewDisplay (SurfaceHolder), otherwise you cannot preview it.
6. This step is also very important. Start updating your preview interface through startPreview (). It must start before you take pictures.
7. Call takePicture (Camera. ShutterCallback, Camera. PictureCallback, Camera. PictureCallback, Camera. PictureCallback) to take a picture and wait for its callback
8. After taking pictures, the preview exhibition stops. If you want to continue taking pictures, you need to call startPreview () again.
9. Call stopPreview () to stop the preview.
10. Importantly, calling release () releases Camera so that other applications can use the camera as well. Your application should be released when onPause () is called, and re-open () when onResume () is called.
The above is the introduction of taking pictures with Camera in the document. Next, let's talk about my usage scenario.
Just put the code on it
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
>
<SurfaceView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/sv_main_surface"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="takePhoto"
android:text=" Photographing "
/>
</LinearLayout>
Activity
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private SurfaceView sv_main_surface;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface);
sv_main_surface.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
// Turn on the camera
camera = Camera.open();
// Setting parameters
Camera.Parameters parameters=camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.set("jpeg-quality",85);
camera.setParameters(parameters);
// Show the screen to SurfaceView
try {
camera.setPreviewDisplay(sv_main_surface.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
// Turn on preview effect
camera.startPreview();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
if(camera!=null){
camera.stopPreview();
camera.release();
camera=null;
}
}
});
}
public void takePhoto(View view){
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
// Technology: Image compression technology
Bitmap bitmap=BitmapFactory.decodeByteArray(bytes,0,bytes.length);
try {
FileOutputStream fos=new FileOutputStream("/mnt/sdcard/G150820_"+System.currentTimeMillis()+".png");
bitmap.compress(Bitmap.CompressFormat.PNG,85,fos);
camera.stopPreview();
camera.startPreview();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
}
}
Recording video:
Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sv_recorder_surface"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Begin "
android:onClick="start"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Stop "
android:onClick="stop"
/>
</LinearLayout>
</RelativeLayout>
Code:
import android.media.MediaRecorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import java.io.IOException;
public class MediaRecorderActivity extends AppCompatActivity {
private SurfaceView sv_recorder_surface;
private MediaRecorder mediaRecorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_recorder);
sv_recorder_surface = (SurfaceView) findViewById(R.id.sv_recorder_surface);
// Instantiate a media recorder
mediaRecorder = new MediaRecorder();
}
public void start(View view){
mediaRecorder.reset();
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// Formatting
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// Set the save path
mediaRecorder.setOutputFile("/mnt/sdcard/G150820_"+System.currentTimeMillis()+".mp4");
mediaRecorder.setPreviewDisplay(sv_recorder_surface.getHolder().getSurface());
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stop(View view){
if(mediaRecorder!=null){
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder=null;
}
}
}
The most important thing is don't forget to add permission
<!-- Permissions to open the camera -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO">
</uses-permission>
The function of simply taking pictures and recording videos is realized.