android 自定义相机源码,Android自定义录像(一)之录像功能实现(附demo源码)

package com.alanjet.videorecordertest;

import android.annotation.TargetApi;

import android.app.Activity;

import android.content.Intent;

import android.content.pm.ActivityInfo;

import android.graphics.PixelFormat;

import android.hardware.Camera;

import android.media.CamcorderProfile;

import android.media.MediaRecorder;

import android.os.Build;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.View;

import android.view.Window;

import android.view.WindowManager;

import android.widget.ImageButton;

import android.widget.Toast;

import java.io.File;

import java.util.Calendar;

public class MainActivity extends Activity implements SurfaceHolder.Callback {

private static final String TAG = "MainActivity";

private SurfaceView mSurfaceview;

private ImageButton mBtnStartStop;

private ImageButton mBtnSet;

private ImageButton mBtnShowFile;

private boolean mStartedFlg = false;

private MediaRecorder mRecorder;

private SurfaceHolder mSurfaceHolder;

private Camera myCamera;

private Camera.Parameters myParameters;

private Camera.AutoFocusCallback mAutoFocusCallback=null;

private boolean isView = false;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//重写AutoFocusCallback接口

mAutoFocusCallback=new Camera.AutoFocusCallback() {

@Override

public void onAutoFocus(boolean success, Camera camera) {

if(success){

Log.i(TAG, "AutoFocus: success...");

}else {

Log.i(TAG, "AutoFocus: failure...");

}

}

};

initScreen();

setContentView(R.layout.activity_main);

mSurfaceview = (SurfaceView)findViewById(R.id.capture_surfaceview);

mBtnStartStop = (ImageButton) findViewById(R.id.ib_stop);

mBtnSet= (ImageButton) findViewById(R.id.capture_imagebutton_setting);

mBtnShowFile= (ImageButton) findViewById(R.id.capture_imagebutton_showfiles);

mBtnShowFile.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent=new Intent(MainActivity.this,ShowVideoActivity.class);

startActivity(intent);

finish();

}

});

mBtnSet.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(MainActivity.this,"相机设置待开发~",Toast.LENGTH_SHORT).show();

}

});

SurfaceHolder holder = mSurfaceview.getHolder();// 取得holder

holder.addCallback(this); // holder加入回调接口

holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

/**

* 获取系统时间,保存文件以系统时间戳命名

*/

public static String getDate(){

Calendar ca = Calendar.getInstance();

int year = ca.get(Calendar.YEAR);

int month = ca.get(Calendar.MONTH);

int day = ca.get(Calendar.DATE);

int minute = ca.get(Calendar.MINUTE);

int hour = ca.get(Calendar.HOUR);

int second = ca.get(Calendar.SECOND);

String date = "" + year + (month + 1 )+ day + hour + minute + second;

Log.d(TAG, "date:" + date);

return date;

}

/**

* 获取SD path

*/

public String getSDPath(){

File sdDir = null;

boolean sdCardExist = Environment.getExternalStorageState()

.equals(android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在

if (sdCardExist)

{

sdDir = Environment.getExternalStorageDirectory();// 获取跟目录

return sdDir.toString();

}

return null;

}

//初始化屏幕设置

public void initScreen(){

requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏

// 设置横屏显示

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

// 选择支持半透明模式,在有surfaceview的activity中使用。

getWindow().setFormat(PixelFormat.TRANSLUCENT);

}

//初始化Camera设置

public void initCamera()

{

if(myCamera == null && !isView)

{

myCamera = Camera.open();

Log.i(TAG, "camera.open");

}

if(myCamera != null && !isView) {

try {

myParameters = myCamera.getParameters();

myParameters.setPreviewSize(1920, 1080);

myParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);

myCamera.setParameters(myParameters);

myCamera.setPreviewDisplay(mSurfaceHolder);

myCamera.startPreview();

isView = true;

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

Toast.makeText(MainActivity.this, "初始化相机错误",

Toast.LENGTH_SHORT).show();

}

}

}

@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width,

int height) {

// TODO Auto-generated method stub

mSurfaceHolder = holder;

}

@Override

public void surfaceCreated(SurfaceHolder holder) {

// TODO Auto-generated method stub

mSurfaceHolder = holder;

initCamera();

mBtnStartStop.setOnClickListener(new View.OnClickListener() {

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

if (!mStartedFlg) {

// Start

if (mRecorder == null) {

mRecorder = new MediaRecorder(); // Create MediaRecorder

}

try {

myCamera.unlock();

mRecorder.setCamera(myCamera);

// Set audio and video source and encoder

// 这两项需要放在setOutputFormat之前

mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

mRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_720P));

mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

// Set output file path

String path = getSDPath();

if (path != null) {

File dir = new File(path + "/VideoRecorderTest");

if (!dir.exists()) {

dir.mkdir();

}

path = dir + "/" + getDate() + ".mp4";

mRecorder.setOutputFile(path);

Log.d(TAG, "bf mRecorder.prepare()");

mRecorder.prepare();

Log.d(TAG, "af mRecorder.prepare()");

Log.d(TAG, "bf mRecorder.start()");

mRecorder.start(); // Recording is now started

Log.d(TAG, "af mRecorder.start()");

mStartedFlg = true;

mBtnStartStop.setBackground(getDrawable(R.drawable.rec_stop));

}

} catch (Exception e) {

e.printStackTrace();

}

} else {

// stop

if (mStartedFlg) {

try {

mRecorder.stop();

mRecorder.reset();

mBtnStartStop.setBackground(getDrawable(R.drawable.rec_start));

} catch (Exception e) {

e.printStackTrace();

}

}

mStartedFlg = false;

}

}

});

}

@Override

public void surfaceDestroyed(SurfaceHolder holder) {

// TODO Auto-generated method stub

// surfaceDestroyed的时候同时对象设置为null

mSurfaceview = null;

mSurfaceHolder = null;

if (mRecorder != null) {

mRecorder.release();

mRecorder = null;

}

}

@Override

protected void onRestart() {

super.onRestart();

}

@Override

protected void onResume() {

super.onResume();

}

@Override

protected void onStart() {

super.onStart();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单实现自定义圆形View的示例代码: ``` public class CircleView extends View { private Paint mPaint; private int mColor; public CircleView(Context context) { super(context); init(); } public CircleView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleView); mColor = ta.getColor(R.styleable.CircleView_circle_color, Color.RED); ta.recycle(); init(); } private void init() { mPaint = new Paint(); mPaint.setColor(mColor); mPaint.setAntiAlias(true); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec); int size = Math.min(width, height); setMeasuredDimension(size, size); } @Override protected void onDraw(Canvas canvas) { int radius = getWidth() / 2; canvas.drawCircle(radius, radius, radius, mPaint); } public void setColor(int color) { mColor = color; mPaint.setColor(mColor); invalidate(); } } ``` 这个自定义View继承自View类,重写了onMeasure和onDraw方法,实现了一个简单的圆形View。此外,还包含一个setColor方法,用于设置圆形的颜色。 在XML布局文件中,可以使用该自定义View并设置其属性,如下所示: ``` <com.example.customview.CircleView android:id="@+id/circle_view" android:layout_width="100dp" android:layout_height="100dp" app:circle_color="@color/blue" /> ``` 其中,app:circle_color是自定义的属性,在CircleView类的构造方法中使用TypedArray获取该属性的值,并设置圆形的颜色。 希望这个示例代码能够帮到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值