多媒体(拍照+录像)

        多媒体不止在音频和视频上面有涉及,其实我们的手机照相机里的照相功能以及录像功能也属于多媒体,所以今天我所要说的就是照相机的拍照以及录像功能

        一、拍照

       先来一个简易版的Demo

       XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.zking.android_27_camera_mediarecoder.MainActivity">

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/sv_main_surface"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="takePhoto"
        android:text="拍照"
        />
</RelativeLayout>

        activity:

package com.zking.android_27_camera_mediarecoder;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private SurfaceView sv_main_surface;
    private Camera camera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface);
        sv_main_surface.getHolder().addCallback(new SurfaceHolder.Callback() {



            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                //打开照相机
                camera = Camera.open();
                //设置参数
                Camera.Parameters parameters=camera.getParameters();
                parameters.setPictureFormat(PixelFormat.JPEG);
                parameters.set("jpeg-quality",85);
                camera.setParameters(parameters);

                //将画面展示到Surfaceview
                try {
                    camera.setPreviewDisplay(sv_main_surface.getHolder());
                    //开启预览
                    camera.startPreview();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                //关闭照相机
                if(camera!=null){
                    camera.release();
                    camera.stopPreview();
                    camera=null;
                }
            }
        });
    }
    public void takePhoto(View view){
        //DCIM/Camera/
        camera.takePicture(null, null, new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] bytes, Camera camera) {
                //实例化文件输出流
                //写

                //压缩(图片)
                //将字节数组转成Bitmap
                Bitmap bitmap= BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                try {
                    FileOutputStream fos=new FileOutputStream("/mnt/sdcard/dcim/camera/G150831_"+System.currentTimeMillis()+".png");
                    bitmap.compress(Bitmap.CompressFormat.PNG,85,fos);

                    //重新继续拍照
                    camera.stopPreview();
                    camera.startPreview();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}

           

        截图如下:


          二、录像

         XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_media_recorder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.zking.android_27_camera_mediarecoder.MediaRecorderActivity">
    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/sv_media_recorder_surface"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="开始"
            android:onClick="start"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="停止"
            android:onClick="stop"
            android:layout_weight="1"
            />
    </LinearLayout>
</RelativeLayout>


         activity:

package com.zking.android_27_camera_mediarecoder;

import android.media.MediaRecorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.MediaController;

import java.io.IOException;

public class MediaRecorderActivity extends AppCompatActivity {

    private SurfaceView sv_media_recorder_surface;
    private MediaRecorder mediaRecorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_recorder);
        sv_media_recorder_surface = (SurfaceView) findViewById(R.id.sv_media_recorder_surface);
        //实例化媒体录制器
        mediaRecorder = new MediaRecorder();
    }
    public void start(View view){
        mediaRecorder.reset();

        //设置参数
        //设置音频,视频来源
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

        //设置输出格式
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

        //设置编码格式(视频,音频)
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        //设置保存路径
        mediaRecorder.setOutputFile("/mnt/sdcard/G150831_"+System.currentTimeMillis()+".mp4");

        //画面
        mediaRecorder.setPreviewDisplay(sv_media_recorder_surface.getHolder().getSurface());

        //准备
        try {
            mediaRecorder.prepare();

            mediaRecorder.start();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void stop(View view){
        mediaRecorder.stop();
    }
}

              截图如下:


       最后,别忘记加权限了

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>





        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值