在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → 本地视频录制,自动对焦,焦距,分辨率设置

本地视频录制,自动对焦,焦距,分辨率设置

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:1.41M
  • 下载次数:42
  • 浏览次数:2591
  • 发布时间:2014-12-15
  • 实例类别:Android平台开发
  • 发 布 人:黯然
  • 所需积分:3
 相关标签: 视频 分辨率 录制

实例介绍

【实例简介】

【实例截图】

【核心代码】

package com.wen.camera.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.example.androidvideorecord.R;
import com.wen.camera.activity.VideoCameraActivity;
import com.wen.video.Adapter.VideoSizeAdapter;
import com.wen.view.VerticalSeekBar;

import android.graphics.Color;
import android.graphics.Rect;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Size;
import android.media.MediaRecorder;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.util.Log;
import android.util.SparseIntArray;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class VideoCameraActivity extends Activity implements OnClickListener,
		OnTouchListener {

	private MediaRecorder mediaRecorder;
	private Camera camera;
	private CameraPreview preview;
	private Camera.Parameters cameraParams;

	private FrameLayout layout;
	private TextView videoSize;
	private ImageView back;

	private SparseIntArray widthArray = new SparseIntArray();
	private SparseIntArray heightArray = new SparseIntArray();
	private int sizeIndex = 0;
	private String path;
	private String fileName;

	private boolean isTaking;

	private TextView timer;
	private int hour = 0;
	private int minute = 0;
	private int second = 0;
	private boolean bool;

	private VerticalSeekBar mZoomBar = null;
	private Button btnVideoButton;

	private AutoFocusCallback myAutoFocusCallback = null;
	private boolean isControlEnable = true;
	private LinearLayout videoSizeLayout;
	private boolean isVideoSizeSupport = true;
	private boolean isVideoAuto = true;

	private VideoSizeAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_video);
		myAutoFocusCallback = new AutoFocusCallback() {
			public void onAutoFocus(boolean success, Camera camera) {
				if (success) {
					camera.setOneShotPreviewCallback(null);
				} else {
				}
			}
		};

		initView();
	}

	/**
	 * @Title initView
	 * @Description 鍔犺浇瑙嗗浘鍙婄浉鍏冲睘鎬�
	 * @author wen
	 * @Date 2013-12-10 涓婂崍10:14:21
	 */
	private void initView() {
		layout = (FrameLayout) findViewById(R.id.take_video_layout);
		btnVideoButton = (Button) findViewById(R.id.arc_hf_video_start);
		videoSizeLayout = (LinearLayout) findViewById(R.id.videoSizeLayout);
		videoSize = (TextView) findViewById(R.id.take_video_size);
		timer = (TextView) findViewById(R.id.arc_hf_video_timer);
		mZoomBar = (VerticalSeekBar) findViewById(R.id.seekBar);
		back = (ImageView) findViewById(R.id.take_video_back);

		timer.setVisibility(View.GONE);
		videoSizeLayout.setVisibility(View.VISIBLE);

		back.setOnClickListener(this);
		btnVideoButton.setOnClickListener(this);
		initCamera();
		// 娣诲姞ZoomBar
		int max = camera.getParameters().getMaxZoom();
		if (max == 0) {
			mZoomBar.setVisibility(View.GONE);
		} else {
			mZoomBar.setMax(max);
			mZoomBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				public void onStopTrackingTouch(SeekBar seekBar) {

				}

				public void onStartTrackingTouch(SeekBar seekBar) {
				}

				public void onProgressChanged(SeekBar seekBar, int progress,
						boolean fromUser) {
					try {
						if (camera != null && cameraParams.isZoomSupported()) {
							if (cameraParams.isSmoothZoomSupported()) {
								camera.startSmoothZoom(progress);
							} else {
								Log.e("VideoCameraActivity", "涓嶆敮鎸佹櫤鑳藉彉鐒�");
								cameraParams.setZoom(progress);
								camera.setParameters(cameraParams);
							}
						}
					} catch (Exception e) {
						mZoomBar.setVisibility(View.GONE);
					}
				}
			});
		}
		path = Environment.getExternalStorageDirectory().getAbsolutePath()
				  "/";
		fileName = "video1216.mp4";
	}

	/**
	 * @Title initCamera
	 * @Description 鍔犺浇鐩告満鐩稿叧缁勪欢
	 * @author wen
	 * @Date 2013-12-10 涓婂崍10:14:21
	 */
	private void initCamera() {
		camera = Camera.open();
		preview = new CameraPreview(this, camera);
		preview.setFocusable(false);
		preview.setEnabled(false);

		cameraParams = camera.getParameters();
		if (VERSION.SDK_INT >= 14) {
			cameraParams.setFocusMode("auto");
			camera.autoFocus(myAutoFocusCallback);
		} else {
			cameraParams
					.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
			isVideoAuto = false;
		}

		List<Size> tempList = cameraParams.getSupportedPreviewSizes();
		if (tempList != null && tempList.size() > 0) {
			for (int i = 0; i < tempList.size(); i  ) {
				widthArray.put(i, tempList.get(i).width);
				heightArray.put(i, tempList.get(i).height);
			}
			setVideoSize();
		} else {
			isVideoSizeSupport = false;
			videoSizeLayout.setVisibility(View.GONE);
		}

		cameraParams.setRotation(90);
		camera.setParameters(cameraParams);
		try {
			camera.setPreviewDisplay(preview.getHolder());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		layout.addView(preview);
		layout.setOnTouchListener(this);
	}

	/**
	 * @Title startCamera
	 * @Description 寮�濮嬪綍鍍�
	 * @author wen
	 * @Date 2013-12-10 涓婂崍10:14:21
	 */
	private void startCamera() {
		second = 0;
		minute = 0;
		hour = 0;
		bool = true;
		camera.startPreview();
		camera.unlock();// 瑙i攣鍚庢墠鑳借皟鐢紝鍚﹀垯鎶ラ敊
		mediaRecorder = new MediaRecorder();
		mediaRecorder.setCamera(camera);// 灏哻amera娣诲姞鍒拌棰戝綍鍒剁鍙�
		mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 闊抽害
		mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);// 瑙嗛婧�
		mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);// 杈撳嚭鏍煎紡
		mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);// 澹伴煶婧愮爜
		mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);// 瑙嗛婧愮爜
		if (isVideoSizeSupport) {
			mediaRecorder.setVideoSize(widthArray.get(sizeIndex),
					heightArray.get(sizeIndex));
		} else {
			mediaRecorder.setVideoSize(320, 240);
		}
		mediaRecorder.setVideoFrameRate(3);

		mediaRecorder.setPreviewDisplay(preview.getHolder().getSurface());
		mediaRecorder.setOutputFile(path   fileName);
		try {
			mediaRecorder.prepare();
			timer.setVisibility(View.VISIBLE);
			handler.postDelayed(task, 1000);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			mediaRecorder.start();
			Toast.makeText(this, "寮�濮嬪綍鍒�", Toast.LENGTH_SHORT).show();
		} catch (IllegalStateException e) {
			this.finish();
			Toast.makeText(this, "涓嶈兘褰曞埗瑙嗛!", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mediaRecorder != null) {
			mediaRecorder.release();
		}
		if (camera != null) {
			camera.lock();
			camera.release();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.arc_hf_video_start:
			if (isControlEnable) {
				isControlEnable = false;
				startOrStopVideo();
				handler.sendEmptyMessageDelayed(0, 2000);
			} else {
				Toast.makeText(this, "2S鐨勫欢杩熸搷浣�", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.take_video_back:
			if (isTaking) {
				showDialog();
			} else {
				finish();
			}
			break;
		default:
			break;
		}
	}

	public void onRealution(View view) {
		if (isTaking) {
			Toast.makeText(this, "褰曞埗瑙嗛涓嶅厑璁稿垏鎹㈣棰戝昂瀵�", Toast.LENGTH_SHORT).show();
		} else {
			final Dialog dialog = new Dialog(this);
			ListView listView = new ListView(this);
			listView.setCacheColorHint(Color.TRANSPARENT);
			listView.setBackgroundColor(Color.WHITE);
			if (adapter == null)
				adapter = new VideoSizeAdapter(this, widthArray, heightArray);
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					adapter.index = arg2;
					sizeIndex = arg2;
					setVideoSize();
					dialog.dismiss();
				}
			});
			dialog.setTitle("鍒嗚鲸鐜囪缃�");
			dialog.setContentView(listView);
			dialog.setCanceledOnTouchOutside(true);
			dialog.show();
		}
	}

	private void startOrStopVideo() {
		if (isTaking) {
			mediaRecorder.stop();
			Toast.makeText(this, "褰曞埗瀹屾垚锛屽凡淇濆瓨", Toast.LENGTH_SHORT).show();
			finish();
		} else {
			videoSizeLayout.setVisibility(View.GONE);
			startCamera();
			isTaking = true;
		}
	}

	/*
	 * 瀹氭椂鍣ㄨ缃紝瀹炵幇璁℃椂
	 */
	private Handler handler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			isControlEnable = true;
			return false;
		}
	});
	private Runnable task = new Runnable() {
		public void run() {
			if (bool) {
				handler.postDelayed(this, 1000);
				second  ;
				if (second >= 60) {
					minute  ;
					second = second % 60;
				}
				if (minute >= 60) {
					hour  ;
					minute = minute % 60;
				}
				timer.setText(format(hour)   ":"   format(minute)   ":"
						  format(second));
			}
		}
	};

	/*
	 * 鏍煎紡鍖栨椂闂�
	 */
	public String format(int i) {
		String s = i   "";
		if (s.length() == 1) {
			s = "0"   s;
		}
		return s;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			if (camera != null && isVideoAuto) {
				float x = event.getX();
				float y = event.getY();
				float touchMajor = event.getTouchMajor();
				float touchMinor = event.getTouchMinor();

				Rect touchRect = new Rect((int) (x - touchMajor / 2),
						(int) (y - touchMinor / 2), (int) (x   touchMajor / 2),
						(int) (y   touchMinor / 2));

				this.submitFocusAreaRect(touchRect);
			}
		}
		return false;
	}

	private void submitFocusAreaRect(final Rect touchRect) {
		Camera.Parameters cameraParameters = camera.getParameters();
		if (cameraParameters == null
				|| cameraParameters.getMaxNumFocusAreas() == 0) {
			return;
		}

		// Convert from View's width and height to  /- 1000
		Rect focusArea = new Rect();
		focusArea.set(touchRect.left * 2000 / preview.getWidth() - 1000,
				touchRect.top * 2000 / preview.getHeight() - 1000,
				touchRect.right * 2000 / preview.getWidth() - 1000,
				touchRect.bottom * 2000 / preview.getHeight() - 1000);
		// Submit focus area to camera
		ArrayList<Camera.Area> focusAreas = new ArrayList<Camera.Area>();
		focusAreas.add(new Camera.Area(focusArea, 1000));
		cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
		cameraParameters.setFocusAreas(focusAreas);
		try {
			camera.setParameters(cameraParameters);
			camera.autoFocus(myAutoFocusCallback);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (isTaking) {
				showDialog();
			} else {
				finish();
			}
			// 杩欓噷涓嶉渶瑕佹墽琛岀埗绫荤殑鐐瑰嚮浜嬩欢锛屾墍浠ョ洿鎺eturn
			return true;
		}
		// 缁х画鎵ц鐖剁被鐨勫叾浠栫偣鍑讳簨浠�
		return super.onKeyDown(keyCode, event);
	}

	private void showDialog() {
		// 寮瑰嚭纭畾閫�鍑哄璇濇
		new AlertDialog.Builder(this).setTitle("閫�鍑�").setMessage("纭畾閫�鍑鸿棰戝綍鍒跺悧锛�")
				.setPositiveButton("纭畾", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						VideoCameraActivity.this.finish();
					}
				})
				.setNegativeButton("鍙栨秷", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).show();
	}

	private void setVideoSize() {
		videoSize.setText(widthArray.get(sizeIndex)   "X"
				  heightArray.get(sizeIndex));
	}

}

标签: 视频 分辨率 录制

实例下载地址

本地视频录制,自动对焦,焦距,分辨率设置

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 真无聊 发表于: 2015-08-19 09:59 26
好,不错

支持(0) 盖楼(回复)

第 2 楼 masai158 发表于: 2015-11-05 10:54 06
wo xiang 看看!!

支持(0) 盖楼(回复)

第 3 楼 t1_one 发表于: 2016-05-31 18:01 19
111

支持(0) 盖楼(回复)

第 4 楼 t1_one 发表于: 2016-05-31 18:01 20
111

支持(0) 盖楼(回复)

第 5 楼 t1_one 发表于: 2016-05-31 18:01 22
111

支持(0) 盖楼(回复)

第 6 楼 星火燎原 发表于: 2016-08-03 20:35 27
支持自动对焦

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有26条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
;
报警