http://blog.csdn.net/wds1181977/article/details/52174840

MediaProjection介绍

MediaProjection可以用来捕捉屏幕,具体来说可以截取当前屏幕和录制屏幕视频。MediaProjection由MediaProjectionManager来管理和获取。

使用步骤

首先获取MediaProjectionManager,和其他的Manager一样通过 Context.getSystemService() 传入参数MEDIA_PROJECTION_SERVICE获得实例。

接着调用MediaProjectionManager.createScreenCaptureIntent()弹出dialog询问用户是否授权应用捕捉屏幕,同时覆写onActivityResult()获取授权结果。

如果授权成功,通过MediaProjectionManager.getMediaProjection(int resultCode, Intent resultData)获取MediaProjection实例,通过MediaProjection.createVirtualDisplay(String name, int width, int height, int dpi, int flags, Surface surface, VirtualDisplay.Callback callback, Handler handler)创建VirtualDisplay实例。实际上在上述方法中传入的surface参数,是真正用来截屏或者录屏的。

截屏

截屏这里用到ImageReader类,这个类的getSurface()方法获取到surface直接传入MediaProjection.createVirtualDisplay()方法中,此时就可以执行截取。通过ImageReader.acquireLatestImage()方法即可获取当前屏幕的Image,经过简单处理之后即可保存为Bitmap。

           private void startCapture() {

            mImageName = System.currentTimeMillis() + ".png";

            Log.e(TAG, "image name is : " + mImageName);

            Image image = mImageReader.acquireLatestImage();

            int width = image.getWidth();

            int height = image.getHeight();

            final Image.Plane[] planes = image.getPlanes();

            final ByteBuffer buffer = planes[0].getBuffer();

            int pixelStride = planes[0].getPixelStride();

            int rowStride = planes[0].getRowStride();

            int rowPadding = rowStride - pixelStride * width;

            Bitmap bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);

            bitmap.copyPixelsFromBuffer(buffer);

            bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height);

            image.close();

            if (bitmap != null) {

                Log.e(TAG, "bitmap  create success ");

                try {

                    File fileFolder = new File(mImagePath);

                    if (!fileFolder.exists())

                        fileFolder.mkdirs();

                    File file = new File(mImagePath, mImageName);

                    if (!file.exists()) {

                        Log.e(TAG, "file create success ");

                        file.createNewFile();

                    }

                    FileOutputStream out = new FileOutputStream(file);

                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

                    out.flush();

                    out.close();

                    Log.e(TAG, "file save success ");

                    Toast.makeText(this.getApplicationContext(), "截图成功", Toast.LENGTH_SHORT).show();

                } catch (IOException e) {

                    Log.e(TAG, e.toString());

                    e.printStackTrace();

                }

            }

        }

录屏

录屏需要用到上篇博文中的MediaCadec,这个类将原始的屏幕数据编码,在通过MediaMuxer分装为mp4格式保存。MediaCodec.createInputSurface()获取一个surface对象讲起传入MediaProjection.createVirtualDisplay()即可获取屏幕原始多媒体数据,之后读取MediaCodec编码输出数据经过MediaMuxer封装处理为mp4即可播放,实现录屏。

       private void recordVirtualDisplay() {//循环多去编解码器输出数据经过处理保存为mp4

        while (!mIsQuit.get()) {

            int index = mMediaCodec.dequeueOutputBuffer(mBufferInfo, 10000);

            Log.i(TAG, "dequeue output buffer index=" + index);

            if (index == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {//后续输出格式变化

                resetOutputFormat();

            } else if (index == MediaCodec.INFO_TRY_AGAIN_LATER) {//请求超时

                Log.d(TAG, "retrieving buffers time out!");

                try {

                    // wait 10ms

                    Thread.sleep(10);

                } catch (InterruptedException e) {

                }

            } else if (index >= 0) {//有效输出

                if (!mMuxerStarted) {

                    throw new IllegalStateException("MediaMuxer dose not call addTrack(format) ");

                }

                encodeToVideoTrack(index);

                mMediaCodec.releaseOutputBuffer(index, false);

            }

        }

    }

        private void encodeToVideoTrack(int index) {//输出数据为mp4文件

        ByteBuffer encodedData = mMediaCodec.getOutputBuffer(index);

        if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {//是特定格式信息等配置数据,不是媒体数据

            // The codec config data was pulled out and fed to the muxer when we got

            // the INFO_OUTPUT_FORMAT_CHANGED status.

            // Ignore it.

            Log.d(TAG, "ignoring BUFFER_FLAG_CODEC_CONFIG");

            mBufferInfo.size = 0;

        }

        if (mBufferInfo.size == 0) {

            Log.d(TAG, "info.size == 0, drop it.");

            encodedData = null;

        } else {

            Log.d(TAG, "got buffer, info: size=" + mBufferInfo.size

                    + ", presentationTimeUs=" + mBufferInfo.presentationTimeUs

                    + ", offset=" + mBufferInfo.offset);

        }

        if (encodedData != null) {//存在编码数据

            encodedData.position(mBufferInfo.offset);

            encodedData.limit(mBufferInfo.offset + mBufferInfo.size);

            mMuxer.writeSampleData(mVideoTrackIndex, encodedData, mBufferInfo);//写入

            Log.i(TAG, "sent " + mBufferInfo.size + " bytes to muxer...");

        }

    }

        private void resetOutputFormat() {

        // should happen before receiving buffers, and should only happen once

        if (mMuxerStarted) {

            throw new IllegalStateException("output format already changed!");

        }

        MediaFormat newFormat = mMediaCodec.getOutputFormat();

        Log.i(TAG, "output format changed.\n new format: " + newFormat.toString());

        mVideoTrackIndex = mMuxer.addTrack(newFormat);

        mMuxer.start();

        mMuxerStarted = true;

        Log.i(TAG, "started media muxer, videoIndex=" + mVideoTrackIndex);

    }

附录参考

官方文档

Android视频录制

Android 5.0截屏

Android录屏代码 
本文Demo

 
 

最新文章

  1. log4cplus 在配置文件中设置文件路径,程序自动创建目录,且在日志文件前按日期创建相应的目录
  2. 搭建jms的步骤和过程
  3. Twain头文件
  4. 常用HTML正则表达式
  5. Linux下实现获取远程机器文件
  6. std::shared_ptr(二)
  7. css技术和实例
  8. Linux系统各发行版镜像下载(2)
  9. nginx+tomcat 配置虚拟目录。。
  10. jQuery 随滚动条滚动效果 (适用于内容页长文章)
  11. 6-06. 理性任务调度(25)(拓扑排序啊 ZJU_PAT)
  12. Extjs6(三)——用extjs6.0写一个简单页面
  13. slurm任务调度系统部署和测试(一)
  14. javaScript事件流是什么?
  15. 《React Native 精解与实战》书籍连载「iOS 平台与 React Native 混合开发」
  16. Glide高级详解—缓存与解码复用
  17. C# Windows Service 基础
  18. ODBC数据库
  19. PHP基础记录
  20. 简单工厂模式使用ResourceBundle读取.properties配置文件

热门文章

  1. excel——之锁定表头不可编辑
  2. office2010如何使用excel冻结窗格
  3. WEB安全第七篇--终结篇考验逻辑思维:逻辑漏洞大汇总(越权、会话逻辑、业务逻辑、暴力破解)
  4. 关于wcf三大工具的使用(wsdl.exe svcutil.exe disco.exe)
  5. linux 中 ll 命令显示 的大小 是什么单位的啊?
  6. c# Use NAudio Library to Convert MP3 audio into WAV audio(将Mp3格式转换成Wav格式)
  7. applicationContext.xml的文件位置就可以有两种默认实现
  8. 微信小程序 --- 文件的上传和下载
  9. JSP教程
  10. SaltStack生产案例-服务部署(redis,mysql,apache+php,haproxy+keepalived)