界面效果
![这里写图片描述](https://img-blog.csdn.net/20180125133853875?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZG9yaXNfZA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
功能介绍
照相机预览
SurfaceHolder holder = surfaceView.getHolder()
holder.addCallback(this)
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
surfaceView.setKeepScreenOn(true)
@Override
public void surfaceCreated(SurfaceHolder holder) {
surfaceHolder = holder;
initCamera();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int i, int i1, int i2) {
surfaceHolder = holder;
cameraPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
surfaceView = null;
surfaceHolder = null;
stopRecord();
stopPlay();
}
private void initCamera() {
if (camera != null) {
camera.release();
camera = null;
}
try {
camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
if (camera != null) {
cameraPara = camera.getParameters();
cameraPara.setPreviewSize(600, 480);
cameraPara.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.cancelAutoFocus();
camera.setParameters(cameraPara);
}
} catch (Exception e) {
e.printStackTrace();
if (camera != null) {
camera.release();
camera = null;
}
}
}
private void cameraPreview() {
if (camera != null) {
try {
camera.setPreviewDisplay(surfaceView.getHolder());
camera.setDisplayOrientation(90);
camera.startPreview();
cameraPara.setFlashMode(isFlashOn ? Parameters.FLASH_MODE_TORCH : Parameters.FLASH_MODE_OFF);
camera.setParameters(cameraPara);
} catch(Exception e) {
e.printStackTrace();
}
}
}
录像
private static final int RECORDER_MAX_LENGTH = 30 * 60 * 1000;
private Handler handler = new Handler();
private long srtInterval = 1000;
private void startRecord() {
hour = minute = second = 0;
startRecordTime = new Date().getTime();
handler.postDelayed(updateTimeRunnable, 1000);
lastSrtTime = "00:00:00,000";
srtRowNum = 1;
handler.postDelayed(recordSrtRunnable, srtInterval);
if (mediaRecorder == null) {
mediaRecorder = new MediaRecorder();
}
if (camera == null) {
initCamera();
camera.setDisplayOrientation(90);
}
camera.unlock();
mediaRecorder.setCamera(camera);
try {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mediaRecorder.setVideoSize(640, 480);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.setVideoEncodingBitRate(3 * 1024 * 1024);
mediaRecorder.setOrientationHint(90);
mediaRecorder.setMaxDuration(RECORDER_MAX_LENGTH);
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.setOutputFile(path);
mediaRecorder.prepare();
mediaRecorder.start();
} catch (Exception e) {
e.printStackTrace();
stopRecord();
}
}
private Runnable updateTimeRunnable = new Runnable() {
@Override
public void run() {
if (second >= 59) {
if (minute >= 59) {
hour++;
minute = 0;
} else {
minute++;
}
second = 0;
} else {
second++;
}
if (hour > 0) {
tvTime.setText(String.format("%02d:%02d:%02d", hour, minute, second));
} else {
tvTime.setText(String.format("%02d:%02d", minute, second));
}
if (new Date().getTime() - startRecordTime >= RECORDER_MAX_LENGTH) {
stopRecord();
return;
}
handler.postDelayed(this, 1000);
}
}
private Runnable recordSrtRunnable = new Runnable() {
@Override
public void run() {
StringBuilder srtBuilder = new StringBuilder();
String srtTime = String.format("%02d:%02d:%02d,000", hour, minute, second);
srtBuilder.append(srtRowNum++ + "\n");
srtBuilder.append(String.format("%s --> %s\n", lastSrtTime, srtTime));
srtBuilder.append(String.format("%s%.2f\n", "高程:", altitude));
srtBuilder.append("\n");
srtBuilder.append(srtRowNum++ + "\n");
srtBuilder.append(String.format("%s --> %s\n", lastSrtTime, srtTime));
srtBuilder.append(String.format("%s%s %s%s\n", "纬度:", latitude, "经度:", longitude));
srtBuilder.append("\n");
lastSrtTime = srtTime;
File srtFile = new File(srtPath);
try {
FileOutputStream fs = new FileOutputStream(srtFile.getAbsolutePath(), true);
fs.write(srtBuilder.toString().getBytes());
fs.close();
} catch (Exception e) {
e.printStackTrack();
}
handler.postDelayed(this, srtInterval);
}
}
private void stopRecord() {
try {
if (mediaRecorder != null) {
handler.removeCallbacks(recordSrtRunnable);
handler.removeCallbacks(updateTimeRunnable);
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
mediaRecorder = null;
}
if (camera != null) {
camera.release();
camera = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
播放
private void startPlay() {
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
}
mediaPlayer.reset();
Uri uri = Uri.parse(path);
mediaPlayer = MediaPlayer.create(MainActivity.this, uri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
stopPlay();
}
});
try {
mediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer.start();
}
private void stopPlay() {
if (mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}
其他
getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)
btnFlash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFlashOn) {
btnFlash.setImageResource(R.drawable.video_flash_close)
isFlashOn = false
} else {
btnFlash.setImageResource(R.drawable.video_flash_on)
isFlashOn = true
}
cameraPara.setFlashMode(isFlashOn ? Parameters.FLASH_MODE_TORCH : Parameters.FLASH_MODE_OFF)
camera.setParameters(cameraPara)
}
})
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1
00:00:00,000 --> 00:00:01,000
高程:150.60
2
00:00:00,000 --> 00:00:01,000
纬度:22.982481666666665 经度:113.36807833333333
3
00:00:01,000 --> 00:00:02,000
高程:150.60
4
00:00:01,000 --> 00:00:02,000
纬度:22.982481666666665 经度:113.36807833333333
![这里写图片描述](https://img-blog.csdn.net/20180125150238179?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZG9yaXNfZA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)