当前位置: 首页 > news >正文

网站后缀gov百度快照查询入口

网站后缀gov,百度快照查询入口,做监控的有哪些网站,网站搭建谷歌seo说明#xff1a; 此MediaProjection 录屏和编码实操主要针对Android12.0系统。通过MediaProjection获取屏幕数据#xff0c;将数据通过mediacodec编码输出H264码流#xff08;使用ffmpeg播放#xff09;#xff0c;存储到sd卡上。 1 MediaProjection录屏与编码简介 这里…说明 此MediaProjection 录屏和编码实操主要针对Android12.0系统。通过MediaProjection获取屏幕数据将数据通过mediacodec编码输出H264码流使用ffmpeg播放存储到sd卡上。 1  MediaProjection录屏与编码简介 这里主要是使用MediaProjection获取屏幕数据将数据通过mediacodec编码输出到存储卡上。这里主要介绍 MediaProjection的基本原理和流程、 MediaCodec编码的简单说明便于对代码有所理解。 1.1 MediaProjection录屏原理和流程 MediaProjection 是 Android 提供的一个用于屏幕捕捉和屏幕录制的功能它允许应用程序在获得用户授权的情况下捕获设备屏幕的内容。这项技术自 Android 5.0Lollipop起引入并在之后的版本中得到广泛应用和发展。 MediaProjection 的主要组件包括 MediaProjectionManager系统服务用于创建和管理 MediaProjection 会话。MediaProjection表示屏幕捕获会话的令牌通过用户的授权获得。VirtualDisplay一个虚拟的显示设备它可以捕获屏幕内容并将其渲染到指定的 Surface 上。 录屏功能的实现流程如下 权限申请APP需要请求用户授权使用屏幕录制功能。这会涉及 AndroidManifest.xml 文件的修改以及添加必要的权限如 WRITE_EXTERNAL_STORAGE 和 RECORD_AUDIO。触发用户授权通过 MediaProjectionManager 创建一个 Intent 来触发系统的屏幕录制授权界面。用户同意授权后应用程序可以在 onActivityResult 中接收到结果。获取 MediaProjection 实例如果用户授权成功则可以通过 MediaProjectionManager 的 getMediaProjection() 方法获取一个 MediaProjection 实例 。创建 VirtualDisplay使用 MediaProjection 实例创建 VirtualDisplay它将捕获屏幕内容并将其显示在 Surface 上。开始录制调用 MediaRecorder 的 start() 方法开始录制屏幕内容。结束录制录制完成后调用 MediaRecorder 的 stop() 和 reset() 方法停止录制并重置 MediaRecorder 状态然后释放 VirtualDisplay 资源。 MediaProjection 录屏的原理主要是通过系统授权捕获屏幕内容并利用虚拟显示设备将内容渲染到录制器上实现屏幕录制的功能。开发者在使用时需要考虑到用户授权、资源管理和异常处理等关键步骤 。 1.2 MediaCodec编码说明 MediaCodec 是 Android 提供的一个音视频编解码器类允许应用程序对音频和视频数据进行编码压缩和解码解压缩。它在 Android 4.1API 级别 16版本中引入广泛应用于处理音视频数据如播放视频、录制音频等。 以下是 MediaCodec 编码的基本步骤 创建 MediaCodec 实例通过调用 MediaCodec.createEncoderByType 方法并传入编码类型如 video/avc 或 audio/mp4a-latm来创建编码器。 配置编码参数通过调用 configure 方法配置编码器传入编码参数如比特率、帧率、编码格式等。 准备输入和输出 Surface为编码器准备输入和输出 Surface。输入 Surface 用于传递待编码的数据输出 Surface 用于接收编码后的数据。 开始编码调用 start 方法启动编码器。 发送输入数据将待编码的数据通过 write 方法发送到编码器的输入队列。 处理输出数据监听输出队列通过 dequeueOutputBuffer 方法获取编码后的数据并进行处理或存储。 停止编码编码完成后调用 stop 方法停止编码器。 释放资源调用 release 方法释放编码器资源。 MediaCodec 支持处理三种数据类型压缩数据、原始音频数据和原始视频数据。这些数据可以通过 ByteBuffer 传输给 MediaCodec 进行处理。对于原始视频数据使用 Surface 作为输入源可以提高编解码器的性能。针对本工程主要通过获得录屏的原始数据通过mediacodec压缩成H264码流。 2 MediaProjection录屏与编码代码完整解读(android Q) 2.1 关于权限部分的处理 关于权限需要在AndroidManifest.xml中添加权限具体如下所示 uses-permission android:nameandroid.permission.MANAGE_EXTERNAL_STORAGEtools:ignoreScopedStorage /uses-permission android:nameandroid.permission.READ_EXTERNAL_STORAGEtools:ignoreScopedStorage /uses-permission android:nameandroid.permission.WRITE_EXTERNAL_STORAGEtools:ignoreScopedStorage /uses-permission android:nameandroid.permission.FOREGROUND_SERVICE/ 这里尤其要注意android.permission.FOREGROUND_SERVICE的添加。关于运行时权限的请求等这里给出一个工具类参考代码具体如下所示 public class Permission {public static final int REQUEST_MANAGE_EXTERNAL_STORAGE 1;//需要申请权限的数组private static final String[] permissions {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CAMERA};//保存真正需要去申请的权限private static final ListString permissionList new ArrayList();public static int RequestCode 100;public static void requestManageExternalStoragePermission(Context context, Activity activity) {if (!Environment.isExternalStorageManager()) {showManageExternalStorageDialog(activity);}}private static void showManageExternalStorageDialog(Activity activity) {AlertDialog dialog new AlertDialog.Builder(activity).setTitle(权限请求).setMessage(请开启文件访问权限否则应用将无法正常使用。).setNegativeButton(取消, null).setPositiveButton(确定, (dialogInterface, i) - {Intent intent new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);activity.startActivityForResult(intent, REQUEST_MANAGE_EXTERNAL_STORAGE);}).create();dialog.show();}public static void checkPermissions(Activity activity) {for (String permission : permissions) {if (ContextCompat.checkSelfPermission(activity, permission) ! PackageManager.PERMISSION_GRANTED) {permissionList.add(permission);}}if (!permissionList.isEmpty()) {requestPermission(activity);}}public static void requestPermission(Activity activity) {ActivityCompat.requestPermissions(activity,permissionList.toArray(new String[0]),RequestCode);} } 2.2 MediaProjection服务的添加 从 Android 12 开始如果应用需要使用 MediaProjection 进行屏幕录制必须将相关的服务声明为前台服务。这是因为屏幕录制涉及到用户隐私因此系统需要确保用户明确知道该服务正在运行。需要在应用的 AndroidManifest.xml 文件中声明服务并添加相应的权限2.1中已经添加和特性具体编写参考如下 manifest xmlns:androidhttp://schemas.android.com/apk/res/android...uses-permission android:nameandroid.permission.FOREGROUND_SERVICE /application ...serviceandroid:name.serviceset.MediaProjectionServiceandroid:exportedtrueandroid:foregroundServiceTypemediaProjection /!-- 其他组件声明 --/application /manifest 添加这些后接下来需要实现.serviceset.MediaProjectionService 的代码具体如下所示 public class MediaProjectionService extends Service {private MediaProjection mMediaProjection;public static int resultCode;public static Intent resultData;public static Notification notification;public static Context context;Overridepublic void onCreate() {super.onCreate();startMediaProjectionForeground();}Overridepublic int onStartCommand(Intent intent, int flags, int startId) {MediaProjectionManager mMediaProjectionManager (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);MediaProjection mediaProjection mMediaProjectionManager.getMediaProjection(resultCode, resultData);H264EncoderThread h264EncoderThread new H264EncoderThread(mediaProjection, 640, 1920);h264EncoderThread.start();return START_NOT_STICKY;}NullableOverridepublic IBinder onBind(Intent intent) {return null;}private void startMediaProjectionForeground() {String channelId CHANNEL_ID_MEDIA_PROJECTION;NotificationManager NOTIFICATION_MANAGER (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);NotificationCompat.Builder notificationBuilder new NotificationCompat.Builder(this,channelId).setSmallIcon(R.mipmap.ic_launcher).setContentTitle(服务已启动);NotificationChannel channel new NotificationChannel(channelId, 屏幕录制, NotificationManager.IMPORTANCE_HIGH);NOTIFICATION_MANAGER.createNotificationChannel(channel);notificationBuilder.setChannelId(channelId);Notification notification notificationBuilder.build();startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION);} } 2.3 编码的处理 关于编码部分主要是MediaCodec的初始化、编码处理部分和文件写入操作代码如下所示 public class H264EncoderThread extends Thread{private MediaProjection mMediaProjection;MediaCodec mediaCodec;private final String TAG H264EncoderThread;public H264EncoderThread(MediaProjection mMediaProjection, int width, int height) {this.mMediaProjection mMediaProjection;MediaFormat format MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, width, height);try {mediaCodec MediaCodec.createEncoderByType(video/avc);format.setInteger(MediaFormat.KEY_FRAME_RATE, 20);format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 30);format.setInteger(MediaFormat.KEY_BIT_RATE, width * height);format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);mediaCodec.configure(format,null,null,CONFIGURE_FLAG_ENCODE);Surface surface mediaCodec.createInputSurface();mMediaProjection.createVirtualDisplay(wangdsh-test, width, height, 2,DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC, surface, null, null);} catch (IOException e) {Log.e(TAG,e.toString());//e.printStackTrace();}}Overridepublic void run() {super.run();mediaCodec.start();MediaCodec.BufferInfo info new MediaCodec.BufferInfo();while (true) {int outIndex mediaCodec.dequeueOutputBuffer(info, 11000);if (outIndex 0) {ByteBuffer byteBuffer mediaCodec.getOutputBuffer(outIndex);byte[] ba new byte[byteBuffer.remaining()];byteBuffer.get(ba);FileUtils.writeBytes(ba);FileUtils.writeContent(ba);mediaCodec.releaseOutputBuffer(outIndex, false);}}} } 其中涉及的FileUtils参考实现如下 public class FileUtils {private static final String TAG FileUtils;public static void writeBytes(byte[] array) {FileOutputStream writer null;try {writer new FileOutputStream(Environment.getExternalStorageDirectory() /codecoutput.h264, true);writer.write(array);writer.write(\n);writer.close();} catch (IOException e) {e.printStackTrace();}}public static String writeContent(byte[] array) {char[] HEX_CHAR_TABLE {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F};StringBuilder sb new StringBuilder();for (byte b : array) {sb.append(HEX_CHAR_TABLE[(b 0xf0) 4]);sb.append(HEX_CHAR_TABLE[b 0x0f]);}Log.d(TAG, writeContent-: sb.toString());try {FileWriter writer new FileWriter(Environment.getExternalStorageDirectory() /codecH264.txt, true);writer.write(sb.toString());writer.write(\n);writer.close();} catch (IOException e) {e.printStackTrace();}return sb.toString();} } 2.4 主流程代码参考实现 这里以 H264encoderMediaProjActivity为例给出一个MediaProjection录屏与编码功能代码的参考实现。具体实现如下 public class H264encoderMediaProjActivity extends AppCompatActivity {private MediaProjectionManager mMediaProjectionManager;Context mContext;private ActivityResultLauncherIntent screenCaptureLauncher;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EdgeToEdge.enable(this);mContext this;setContentView(R.layout.h264_encode_media_projection);ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) - {Insets systemBars insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});Permission.checkPermissions(this);Permission.requestManageExternalStoragePermission(getApplicationContext(), this);mMediaProjectionManager (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);screenCaptureLauncher registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),result - {if (result.getResultCode() Activity.RESULT_OK) {Intent resultData result.getData();MediaProjectionService.resultCode result.getResultCode();MediaProjectionService.resultData resultData;MediaProjectionService.context mContext;Intent SERVICE_INTENT new Intent(this, MediaProjectionService.class);startForegroundService(SERVICE_INTENT);}});Button mButton findViewById(R.id.button);mButton.setOnClickListener(view - {// 创建屏幕录制的 IntentIntent captureIntent mMediaProjectionManager.createScreenCaptureIntent();// 启动屏幕录制请求screenCaptureLauncher.launch(captureIntent);});} } 这里涉及的layout布局文件内容如下 ?xml version1.0 encodingutf-8? androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:idid/maintools:context.MainActivityButtonandroid:layout_widthmatch_parentandroid:layout_height50dpandroid:textstring/startLiveandroid:gravitycenterandroid:idid/buttonapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintLeft_toLeftOfparentapp:layout_constraintRight_toRightOfparentapp:layout_constraintTop_toTopOfparent //androidx.constraintlayout.widget.ConstraintLayout 2.5 MediaProjection录屏与编码 demo实现效果 实际运行效果展示如下 使用ffmpeg对码流进行播放说明编码生成的码流是有效的截图如下所示
http://www.w-s-a.com/news/299698/

相关文章:

  • 网站建设 服务内容 费用简述网站开发流程
  • 公司制作网站跟企业文化的关系空间制作网站
  • 浙江建设监理协会网站个人网站设计规划书
  • wordpress太卡了贵州seo推广
  • 企业介绍微网站怎么做的手机软件商城免费下载
  • 新手网站设计定价网站开发销售
  • 网站开发公司oa有没有找人做标书的网站
  • 传统门户网站有哪些人武部正规化建设
  • 台州网站制作方案免费无代码开发平台
  • 精通网站建设 pdf微盘学做电商的步骤
  • 想在网上做设计接单有没有网站找一个免费域名的网站
  • 湘潭市网站建设科技有限公司杭州网站建设(推荐乐云践新)
  • 优秀网站评析西双版纳傣族自治州民宿
  • 常用的cms建站系统c2c网站模板
  • wordpress更换图标seo网站建设公司
  • 网站备案 深圳小程序怎么进入公众号
  • 实名认证域名可以做电影网站吗坪山网站设计的公司
  • wdcp怎么上传做好的网站管理咨询公司名称参考
  • 设计师网站pin分销系统小程序开发
  • 高端品牌网站建设兴田德润实惠企业网站建设应该怎么做
  • 做研学的网站优秀软文案例
  • 网站个人简介怎么做建设网站卡盟
  • 影楼做网站安庆建设机械网站
  • 访问网站的原理wix做网站流程
  • 众鱼深圳网站建设设计师网名叫什么好听
  • 中小学生做试卷的网站6网站建设需要注意哪些细节
  • 以个人名义做地方门户网站社保服务个人网站
  • 上海企业做网站设计制作感悟150字
  • asp.netmvc网站开发ps设计网页
  • win2008 挂网站 404官方网站是什么