Working daily log Andrews 10

android photo shoot video capture process     https://www.cnblogs.com/younghao/p/5089118.html , https://www.jianshu.com/p/e5312fd916dd?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

Code 

package com.zsch.forestinventory.activity.left_activity.LandForm;

import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import com.zsch.androidlib.activity.BaseActivity;
import com.zsch.androidlib.engine.CameraManager;
import com.zsch.androidlib.ui.MySurfaceView;
import com.zsch.androidlib.utils.AppConstants;
import com.zsch.androidlib.view.LVCircularRing;
import com.zsch.forestinventory.MyApplication;
import com.zsch.forestinventory.R;
import com.zsch.forestinventory.activity.MainActivity;
import com.zsch.forestinventory.db.gen.DaoSession;
import com.zsch.forestinventory.entity.landforms.LandForm;
import com.zsch.forestinventory.entity.landforms.Video;
import com.zsch.forestinventory.entity.outdoor_scene.Image;
import com.zsch.forestinventory.entity.outdoor_scene.OutdoorScene;
import com.zsch.forestinventory.entity.outdoor_scene.SceneGeoPoint;
import com.zsch.forestinventory.entity.outdoor_scene.ScenePoint;
import com.zsch.forestinventory.utils.DateUtils;
import android.hardware.Camera.Size;
import org.osmdroid.util.GeoPoint;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;

/**
* Created by TinySun on 2017/10/17.
* 拍照用的Activity
*/
CameraLandFormVideoActivity the extends BaseActivity the implements class public View.OnClickListener, CameraManager.IPhotoSaveStateListener {

public static String PROJECT_NAME_ID Final = "projectNameId"; //
public static String IMAGE_TYPE_ID Final = "imagetype_id";
public static String LAND_FROM_ID Final = "landFromId"; // Add geological point id to remove the scene point came here to start accepting mainactivity project name and geological point id
public static vIDEO_ID Final String = "video_id";
public static String video_type Final = "video_type";



private ImageButton mShutter ;; // record button
private ImageButton ibBack; // back button
private LVCircularRing ivLoading; // after the shooting circle animation


// Private CameraManager mCameraManager;
Private Camera mCamera;
private MediaRecorder mRecorder; // record video class
// private MySurfaceView mSurfaceView; // display the video controls to achieve this Callback Interface Interface
// Private FrameLayout mSurfaceViewLayout;
Private SurfaceHolder mSurfaceHolder;
Private SurfaceView mCameraPreview; // display the video controls to achieve this Callback interface to access
Private Final static int the camera_ID = 0;
Private mIsRecording boolean = false;
Private mIsSufaceCreated boolean = false; // determine whether to create a successful surfaceView


Private DaoSession Msession;
Private Landform LandformsImages; // the new entity LanfForm
private String projectName;
String imagetype_id Private;
Private String Video;
Private String savepath;


@Override
protected void initVariables () {// initialize variables
long landFromId = getIntent () getLongExtra ( LAND_FROM_ID, -1L);. // mainactivity came here to accept the geological point ID
the projectName its getIntent = () getStringExtra (PROJECT_NAME_ID);. // Gets the name of the project was passed over this must have to do first after the current write another
imagetype_id = getIntent () getStringExtra (IMAGE_TYPE_ID );. // get passed over imagetype_id
Video = getIntent () getStringExtra (video_type);. // Gets pass over the type of video


IF (landFromId <0)
onBackPressed ();
Msession = ((the MyApplication) getApplication ()) the getSession ();. // instantiate
executes // Now examples User object query object gets more acquisition Friend deep object table 1 to 1 by connecting the object table to obtain another object
LandformsImages = mSession.getLandFormDao () loadDeep (landFromId );.. // 1 through pass over the two tables both tables id check one connection data together

savePath = AppConstants.APP_DIR + projectName + AppConstants.VIDEO_FILE; // save path for the video program path APP_DIR


startPreview (); // start preview
}

@Override
protected void as initView (the Bundle savedInstanceState) {
the setContentView (R.layout.activity_camera_land_form_video);
mSurfaceViewLayout = // (the FrameLayout) the findViewById (R.id.mSurfaceViewLayout);
mShutter = (ImageButton) the findViewById (R.id.record_shutter);
ibBack = (ImageButton) the findViewById (R.id.ibBack);
ivLoading = (LVCircularRing) the findViewById (R.id.ivLoading); // camera after rotating frame
mCameraPreview = (SurfaceView) findViewById (R.id.camera_preview ); // photo preview

mSurfaceHolder = mCameraPreview.getHolder (); // get holder
mSurfaceHolder.addCallback(mSurfaceCallback);// holder加入回调接口
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

mShutter.setOnClickListener(this);
ibBack.setOnClickListener(this);

ivLoading.setViewColor(getResources().getColor(R.color.colorProgressBarDefaultWheel));
ivLoading.setBarColor(getResources().getColor(R.color.colorProgressBarWheel));



}

@Override
protected void loadData() {


}
private SurfaceHolder.Callback mSurfaceCallback = new SurfaceHolder.Callback() {

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mIsSufaceCreated = false;
}

@Override
// create surfaceView must listen to create finished handle playback after
public void surfaceCreated (SurfaceHolder Holder) {
mIsSufaceCreated = to true;
}

@Override
public void surfaceChanged (SurfaceHolder Holder, the format int, int width, int height) {
StartPreview ();
}
};




// start preview
Private void StartPreview () {
// camera object to ensure that there is only one
if {// when the camera object, and not a two sufaceView objects (mCamera = null || mIsSufaceCreated!! ) returns to the presence of
return;
}
mCamera = Camera.open (the camera_ID); // get the object the camera
Camera.Parameters parameters = mCamera.getParameters (); // Get the photographic parameters of
List <Camera.Size> sizes = parameters.getSupportedPictureSizes ( ); // set the size of the preview time, must be used getSupportedPreviewSizes () value
// List <Camera. Size> sizeVideos = parameters.getSupportedVideoSizes (); parameters.setVideoSize () method of how to use this method has no error mRecorder.setVideoSize (1920, 1080);
IF (sizes.get (0) .width> sizes.get (sizes. size () -. 1) .width) {
parameters.setPictureSize (sizes.get (0) .width, sizes.get (0) .height); // this is determined by determining the presence or absence of the camera lens to
} the else {
parameters.setPictureSize (sizes.get (sizes.size () -. 1) .width, sizes.get (sizes.size () -. 1) .height);
}
// parameters.setPreviewSize (sizes.get (sizes.size () - 1) .width, sizes.get (sizes.size () - 1) .height); I feel the video here should setPreviewSize




parameters.setFocusMode (Camera .Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); // get the focus
parameters.setPreviewFrameRate (20 is);

// set the correct direction the camera preview preview direction
// mCamera.setDisplayOrientation (90); // angle adjustment window 90 is a vertical default horizontal position
mCamera.setParameters (Parameters);

the try {
mCamera.setPreviewDisplay (mSurfaceHolder); // set the SurfaceView SurfaceHolder for previewing.
MCamera.setPreviewCallback // (mPreviewCallback);
} the catch (Exception E) {
}
mCamera.startPreview (); // start previewing
}

// stop the preview
void stopPreview Private () {
// release Camera object
IF (mCamera = null!) {
the try {
mCamera.setPreviewDisplay (null);
} the catch (Exception E) {

}

mCamera.stopPreview ();
mCamera.release ();
mCamera = null;
}
}


@Override
public void onClick (View View) {// click on a trigger event
Switch (view.getId ()) {
Case R.id.record_shutter: // click on the record button to start and stop recording click stop again
if (mIsRecording) {// mIsRecording preview state or the judgment is in the shooting status
stopRecording ();
} the else {
initMediaRecorder ();
the startRecording ();
}
BREAK;
Case R.id.ibBack: // return button
onBackPressed ();
BREAK;
}
}
// recording function
private void initMediaRecorder () {
while // Camera recording video, in addition to Camera.open () and outside Camera.release () calls must also manage Camera.lock () and Camera.unlock () call, MediaRecorder to allow access to the camera hardware.
mRecorder = new MediaRecorder (); // instantiate objects created mediarecorder
mCamera.unlock (); // unlock camera for MediaRecorder use. From the preview it unlocked
; mRecorder.reset ()
consistent // settings in the Camera object to the Recorder, to ensure the video with a preview of the direction
mRecorder.setCamera (mCamera); // set the camera to be used for video capture, using the application's current camera examples
// mRecorder.setOrientationHint (90); whether horizontal screen (without changing the phrase when // save the video file to play the video file.
// Set the video source Camera (camera)
mRecorder.setVideoSource (MediaRecorder.VideoSource.CAMERA);
// set the sound source
mRecorder.setAudioSource (MediaRecorder.AudioSource.MIC);
// set the recording is completed THREE_GPP encapsulation format for video is 3gp.MPEG_4 MP4
mRecorder.setOutputFormat (MediaRecorder.OutputFormat.MPEG_4);
// set the video recording encoding H264 H263
mRecorder.setVideoEncoder (MediaRecorder.VideoEncoder.H264);
// set the encoding bit rate, video will not be provided and the file size of an image blur 3.26M (30 seconds) wonders
mRecorder.setVideoEncodingBitRate (5 * 900 * 1024) ;
// set the audio encoding format
mRecorder.setAudioEncoder (MediaRecorder.AudioEncoder.DEFAULT);
// set the video recording resolution. Must be placed behind the set and encoding format, otherwise an error
mRecorder.setVideoSize (1920, 1080);
// set the video frame rate recording. Must be placed behind the set and encoding format, otherwise an error
mRecorder.setVideoFrameRate (30);
// set the maximum recording time is 30s
mRecorder.setMaxDuration (30000);
// will show on the screen to SurfaceView
mRecorder.setPreviewDisplay (mSurfaceHolder.getSurface ( ));
// // set the output video file path
// mRecorder.setOutputFile ( "/ sdcard /" + generateVideoName () + "MP4.");

// set the last path to establish a video memory file storage path through the UUID
/ / environment category: provides access to environment variables
// Environment.getExternalStorageState used to obtain the status of the SD card
//File.separator path separator File file1 = new File ( "C : \ tmp \ test.txt"); in \ window and linux as delimiters is not the same in order to prevent a recognition error across systems File.separator
// File file = new File (Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_MOVIES) + File.separator + savePath); // create a file folder name
File file = new File (savePath) ; // create a file folder name
if (file!. EXISTS ()) {
// create a multi-level directory
file.mkdirs ();
}
String file_name = generateVideoName ();
// set the output path video files
mRecorder.setOutputFile (file.getPath () + "/ " + file_Name) ; // create a file name
// database storage when you click to stop shooting after the data will return to the main function
hasSaveVideo (file_name);


}

void hasSaveVideo Private (String file_name) {
// the name of the video memory 1. 2. 3. Item type video id4 geological point id5 project name..
Connections Video Video = null;
Video new new Connections Video = ();
video.setLandForm_id (its getIntent (). getLongExtra (LAND_FROM_ID, -1L)); // geological point of the above mentioned id
// video.setProject_id (); // project the above mentioned id.
video.setProjectName (projectName);
video.setVideo_type (getIntent () getStringExtra (video_type));. // video type
video.setName (file_Name); // video name 2
mSession.insert (video); // this step video of the id and the name into the database name id will be relevant class of

the Intent the Data = new new the Intent ();
data.putExtra (LAND_FROM_ID, video.getLandForm_id ()); // geological point id return to the main page in
data.putExtra (VIDEO_ID, video.getId ()) ; // return the photo id
data.putExtra (VIDEO_TYPE, video.getVideo_type ()) ; // returns the type of photo id may be a plan can also be a photo
data.putExtra (PROJECT_NAME_ID, projectName); // Returns the name of the item list is no project name using the id on the line project name real need which can not be the problem here appears
setResult (RESULT_OK, data); // here is the judge after onActivityResult should be mainactivity return before the picture id id and real return () method, there are


}

// start shooting
private the startRecording void () {
IF (mRecorder = null!) {
the try {
mRecorder.prepare (); // prepare for the configuration settings provided MediaRecorder.
mRecorder.start (); // start recording
} the catch (Exception E) {
mIsRecording = to false; // mIsRecording preview state or the judgment is in the shooting status
}
}
style setting shooting button @
MShutter.setImageDrawable // (getResources () getDrawable (R.drawable.recording_shutter_hl).);
MIsRecording = to true; // mIsRecording preview state or the judgment is in the photographing state
//}
}



// stop and lock the camera shooting mRecorder releasing resources
Private void stopRecording () {

IF (mRecorder = null!) {
mRecorder.stop (); // stop MediaRecorder
mRecorder.release (); // release MediaRecorder
mRecorder = null;
}
(! mCamera = null) {IF
mCamera.lock (); // lock camera
mCamera.stopPreview (); // stop the preview
mCamera.release (); // release the camera

}
// change the button style
MShutter.setImageDrawable // (. GetResources () getDrawable (R.drawable.recording_shutter));
// Finish (); // after execution this.finish () method Activity, perform the following procedure: onPause (), onStop ( ), onDestory (),
mIsRecording = false; // mIsRecording judge is a preview state or the shooting state
// in the photo finish this project need to return to the table interface does not require re-preview
// restart preview
// StartPreview ();

Finish (); // delete onpasue () good results still have to change onpasue () function to let him meet my project they will medRecordi and camera release are unloaded in the onpause and ondestory
}





@Override
public void onBackPressed () { // to solve the problems arise here when the shooting when the need to modify the click finish () will jump to onpuse this is because mRecorder program go stopPreview ();

the Intent the Data = new new the Intent ();
iF (! LandformsImages = null) {
data.putExtra (LAND_FROM_ID, LandformsImages.getId () longValue ().); // scenePoint geological point is not empty id put back pass
setResult (RESULT_OK, Data);
} {the else
setResult (RESULT_CANCELED); // = 0 RESULT_CANCELED ; pass back
}
// two cases before a time-photographed after photographing one is
if (mRecorder = null!) { // is described in the shooting
mIsRecording = to true;
stopRecording (); // not stopped at this shooting mIsRecording = true problem described photographing

} else {// described preview
stopPreview (); // stop the preview mIsRecording = false not described in the preview state imaging
}


Finish ();
}



Private generateVideoName String () {// takePicture () methods which get modified the name of the picture
Calendar calendar = Calendar.getInstance (); // initialize the calendar object class calendar where
StringBuffer buffer = new StringBuffer (); // string storage class
buffer.append ( "V");
String year = Calendar.get (Calendar.YEAR ) + "";
buffer.append (year.substring (2,4));
int = month The Calendar.get (the Calendar.MONTH) +. 1;
IF (month The <10) {
buffer.append ( "0");
}
buffer.append (month The);
int = Day Calendar.get (Calendar.DAY_OF_MONTH);
IF (Day <10) {
buffer.append ( "0");
}
buffer.append (Day);

buffer.append (randomString () ); // add random letters

buffer.append (AppConstants.VIEW_SUFFIX); // add the suffix .mp4

return buffer.toString ();
}


/ **
* generate a random string using the UUID
* @return
* /
public String randomString () {
String STR = Java. util.UUID.randomUUID () toString ();.
String text str.substring = (0,8) + str.substring (str.length () -. 8, str.length ());
return text;
}



public void notifySystemToScan (file file) {// notification system is actually create or delete a file, the system needs to scan the sd card, update
the Intent the Intent = new new the Intent (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
uri uri = Uri.fromFile (file);
the Intent. the setData (URI);
the sendBroadcast (Intent);
}


@Override
public void hasSavePhoto (String path) {

}
}

Remember two camera mRecorder on two video should open the last two to be closed earlier than mRecorder oh camera to preview and then set mrecorder

Guess you like

Origin www.cnblogs.com/dushutai/p/12642746.html