当前位置:操作系统 > 安卓/Android >>

android录像和拍照功能

先在 manifest 里添加权限
 
    <uses-permission android:name="android.permission.CAMERA" />
 <!-- 调用摄像头权限 -->
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
 <!-- 录制视频/音频权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <!-- sd卡读写权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
 <!-- 挂载sd卡 -->
 
layout 布局预览窗口 camera.xml
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Su易做图ceView
        android:id="@+id/arc_hf_video_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <TextView
        android:id="@+id/arc_hf_video_timer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="40dp"
        android:textColor="#ffff0000"
        android:textSize="35dp"
        android:textStyle="bold" >
    </TextView>

    <LinearLayout
        android:id="@+id/arc_hf_video_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/arc_hf_video_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/arc_hf_btn_video_start" />

        <Button
            android:id="@+id/arc_hf_img_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/arc_hf_btn_img" />
    </LinearLayout>

</RelativeLayout>
 
主Activity MCamera.java
 
public class MCamera extends Activity {
    private Button mVideoStartBtn;
    private Su易做图ceView mSu易做图ceview;
    private MediaRecorder mMediaRecorder;
    private Su易做图ceHolder mSu易做图ceHolder;
    private File mRecVedioPath;
    private File mRecAudioFile;
    private TextView timer;
    private int hour = 0;
    private int minute = 0;
    private int second = 0;
    private boolean bool;
    private int parentId;
    protected Camera camera;
    protected boolean isPreview;
    private Drawable iconStart;
    private Drawable iconStop;
    private boolean isRecording = true; // true表示没有录像,点击开始;false表示正在录像,点击暂停

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
         * 全屏显示
         */
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setContentView(R.layout.map_video);
        iconStart = getResources().getDrawable(
                R.drawable.arc_hf_btn_video_start);
        iconStop = getResources().getDrawable(R.drawable.arc_hf_btn_video_stop);

        parentId = getIntent().getIntExtra("parentId", 0);
        timer = (TextView) findViewById(R.id.arc_hf_video_timer);
        mVideoStartBtn = (Button) findViewById(R.id.arc_hf_video_start);
        mSu易做图ceview = (Su易做图ceView) this.findViewById(R.id.arc_hf_video_view);

        // 设置计时器不可见
        timer.setVisibility(View.GONE);

        // 设置缓存路径
        mRecVedioPath = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/hfdatabase/video/temp/");
        if (!mRecVedioPath.exists()) {
            mRecVedioPath.mkdirs();
        }

        // 绑定预览视图
        Su易做图ceHolder holder = mSu易做图ceview.getHolder();
        holder.addCallback(new Callback() {

            @Override
            public void su易做图ceDestroyed(Su易做图ceHolder holder) {
                if (camera != null) {
      &nb

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,