如何使用Android API记录屏幕并拍摄屏幕截图? [英] How to record screen and take screenshots, using Android API?

查看:478
本文介绍了如何使用Android API记录屏幕并拍摄屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android在Kitkat和Lollipop上获得了新的API,用于视频捕获屏幕.您可以通过ADB工具或通过代码(从Lollipop开始)来做到这一点.

Android got a new API on Kitkat and Lollipop, to video capture the screen. You can do it either via the ADB tool or via code (starting from Lollipop).

自从新API发布以来,许多应用程序就开始使用此功能,从而可以记录屏幕,微软甚至制作了自己的Google-Now-On-tap竞争对手应用程序.

Ever since the new API was out, many apps came to that use this feature, allowing to record the screen, and Microsoft even made its own Google-Now-On-tap competitor app.

使用ADB,您可以使用:

Using ADB, you can use:

adb shell screenrecord /sdcard/video.mp4 

您甚至可以在Android Studio本身中进行操作.

You can even do it from within Android Studio itself.

我找不到有关如何使用API​​进行操作的任何教程或解释,即代码中的含义.

I can't find any tutorial or explanation about how to do it using the API, meaning in code.

我唯一找到的地方是文档( ,位于屏幕捕获和共享"下),告诉我这一点:

The only place I've found is the documentations (here, under "Screen capturing and sharing"), telling me this:

Android 5.0可让您添加屏幕捕获和屏幕共享 通过新的android.media.projection API为您的应用提供功能. 此功能很有用,例如,如果您要启用 视频会议应用程序中的屏幕共享.

Android 5.0 lets you add screen capturing and screen sharing capabilities to your app with the new android.media.projection APIs. This functionality is useful, for example, if you want to enable screen sharing in a video conferencing app.

新的createVirtualDisplay()方法允许您的应用捕获 主屏幕的内容(默认显示)进入Surface 对象,然后您的应用可以通过网络发送该对象.仅API 允许捕获非安全的屏幕内容,而不是系统音频.到 要开始抓屏,您的应用必须首先请求用户的 通过使用Intent启动屏幕捕获对话框来获得许可 是通过createScreenCaptureIntent()方法获得的.

The new createVirtualDisplay() method allows your app to capture the contents of the main screen (the default display) into a Surface object, which your app can then send across the network. The API only allows capturing non-secure screen content, and not system audio. To begin screen capturing, your app must first request the user’s permission by launching a screen capture dialog using an Intent obtained through the createScreenCaptureIntent() method.

有关如何使用新API的示例,请参见MediaProjectionDemo. 示例项目中的课程.

For an example of how to use the new APIs, see the MediaProjectionDemo class in the sample project.

是的,我找不到任何"MediaProjectionDemo"示例.取而代之的是,我找到了屏幕捕获"示例,但我不了解它的工作原理,因为在运行该示例时,我所看到的只是一个闪烁的屏幕,而且我不认为它会将视频保存到一份文件.该示例似乎有很多错误.

Thing is, I can't find any "MediaProjectionDemo" sample. Instead, I've found "Screen Capture" sample, but I don't understand how it works, as when I've run it, all I've seen is a blinking screen and I don't think it saves the video to a file. The sample seems very buggy.

如何使用新的API执行这些操作:

How do I perform those actions using the new API:

  1. 开始录制,可以选择包括音频(麦克风/扬声器/两者).
  2. 停止记录
  3. 获取屏幕截图而不是视频.

另外,如何自定义它(分辨率,要求的fps,颜色,时间...)?

Also, how do I customize it (resolution, requested fps, colors, time...)?

推荐答案

第一步和肯·怀特(Ken White)正确建议的步骤&您可能已经介绍了

First step and the one which Ken White rightly suggested & which you may have already covered is the Example Code provided officially.

我以前使用过他们的API.我同意截图非常简单.但是,屏幕录制也处于类似的状态.

I have used their API earlier. I agree screenshot is pretty straight forward. But, screen recording is also under similar lines.

我将分3部分回答您的问题,并通过链接将其包裹起来. :)

I will answer your questions in 3 sections and will wrap it up with a link. :)

1.开始录像

private void startScreenRecord(final Intent intent) {
 if (DEBUG) Log.v(TAG, "startScreenRecord:sMuxer=" + sMuxer);
 synchronized(sSync) {
  if (sMuxer == null) {
   final int resultCode = intent.getIntExtra(EXTRA_RESULT_CODE, 0);
   // get MediaProjection 
   final MediaProjection projection = mMediaProjectionManager.getMediaProjection(resultCode, intent);
   if (projection != null) {
    final DisplayMetrics metrics = getResources().getDisplayMetrics();
    final int density = metrics.densityDpi;

    if (DEBUG) Log.v(TAG, "startRecording:");
    try {
     sMuxer = new MediaMuxerWrapper(".mp4"); // if you record audio only, ".m4a" is also OK. 
     if (true) {
      // for screen capturing 
      new MediaScreenEncoder(sMuxer, mMediaEncoderListener,
       projection, metrics.widthPixels, metrics.heightPixels, density);
     }
     if (true) {
      // for audio capturing 
      new MediaAudioEncoder(sMuxer, mMediaEncoderListener);
     }
     sMuxer.prepare();
     sMuxer.startRecording();
    } catch (final IOException e) {
     Log.e(TAG, "startScreenRecord:", e);
    }
   }
  }
 }
}

2.停止录像

 private void stopScreenRecord() {
  if (DEBUG) Log.v(TAG, "stopScreenRecord:sMuxer=" + sMuxer);
  synchronized(sSync) {
   if (sMuxer != null) {
    sMuxer.stopRecording();
    sMuxer = null;
    // you should not wait here 
   }
  }
 }

2.5.暂停和恢复视频录制

 private void pauseScreenRecord() {
  synchronized(sSync) {
   if (sMuxer != null) {
    sMuxer.pauseRecording();
   }
  }
 }

 private void resumeScreenRecord() {
  synchronized(sSync) {
   if (sMuxer != null) {
    sMuxer.resumeRecording();
   }
  }
 }

希望代码有帮助.这是

Hope the code helps. Here is the original link to the code that I referred to and from which this implementation(Video recording) is also derived from.

3.截取屏幕截图而不是视频

我认为默认情况下,它很容易以位图格式捕获图像.您仍然可以继续使用 MediaProjectionDemo 示例来捕获屏幕截图.

I think by default its easy to capture the image in bitmap format. You can still go ahead with MediaProjectionDemo example to capture screenshot.

:对屏幕截图进行代码加密

a.要根据设备的宽度/高度创建虚拟显示

mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
mVirtualDisplay = sMediaProjection.createVirtualDisplay(SCREENCAP_NAME, mWidth, mHeight, mDensity, VIRTUAL_DISPLAY_FLAGS, mImageReader.getSurface(), null, mHandler);
mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), mHandler);

b.然后根据意图或动作启动屏幕捕获-

startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);

停止媒体投影-

sMediaProjection.stop();

c.然后转换为图片-

//Process the media capture
image = mImageReader.acquireLatestImage();
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * mWidth;
//Create bitmap
bitmap = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
//Write Bitmap to file in some path on the phone
fos = new FileOutputStream(STORE_DIRECTORY + "/myscreen_" + IMAGES_PRODUCED + ".png");
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.close();


Media Projection API有几种实现方式(完整代码). 其他一些可以帮助您发展的链接-


There are several implementations (full code) of Media Projection API available. Some other links that can help you in your development-

  1. 使用MediaProjectionManager进行视频录制-网站

android-ScreenCapture -github 开发人员的观察:)

android-ScreenCapture - github as per android developer's observations :)

屏幕记录器-github

捕获并记录Android使用MediaProjection API的屏幕-网站


希望它能帮助您:)编写愉快的代码和进行屏幕录制!


Hope it helps :) Happy coding and screen recording!

PS:您能告诉我您正在谈论的Microsoft应用程序吗?我没有用过.想尝试一下:)

这篇关于如何使用Android API记录屏幕并拍摄屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆