Android屏幕截图实现
方法一,根据android代码实现的流程在复制一遍,流程上一篇已经大概看过了,网上也有很多已经实现了。下面我就转载一篇
来自http://blog.csdn.net/hk_256
1. Activity文件
- package com.arvinhe.testscreenshot;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Matrix;
- import android.os.Bundle;
- import android.util.DisplayMetrics;
- import android.view.Display;
- import android.view.Surface;
- import android.view.View;
- import android.view.WindowManager;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- public class TestScreenShotActivity extends Activity implements OnClickListener{
- private ImageView img_display;
- private Button bt_screenshot;
- private Display mDisplay;
- private DisplayMetrics mDisplayMetrics;
- private Matrix mDisplayMatrix;
- private Bitmap mScreenBitmap;
- private WindowManager mWindowManager;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- bt_screenshot = (Button)findViewById(R.id.bt_screenshot);
- img_display = (ImageView)findViewById(R.id.img_display);
- bt_screenshot.setOnClickListener(this);
- mDisplayMatrix = new Matrix();
- mWindowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
- mDisplay = mWindowManager.getDefaultDisplay();
- mDisplayMetrics = new DisplayMetrics();
- mDisplay.getRealMetrics(mDisplayMetrics);
- }
- @Override
- public void onClick(View v) {
- if(v.equals(bt_screenshot)){
- mDisplay.getRealMetrics(mDisplayMetrics);
- float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels};
- float degrees = getDegreesForRotation(mDisplay.getRotation());
- boolean requiresRotation = (degrees > 0);
- if (requiresRotation) {
- // Get the dimensions of the device in its native orientation
- mDisplayMatrix.reset();
- mDisplayMatrix.preRotate(-degrees);
- mDisplayMatrix.mapPoints(dims);
- dims[0] = Math.abs(dims[0]);
- dims[1] = Math.abs(dims[1]);
- }
- mScreenBitmap = Surface.screenshot((int) dims[0], (int) dims[1]);
- if (requiresRotation) {
- // Rotate the screenshot to the current orientation
- Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels,
- mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888);
- Canvas c = new Canvas(ss);
- c.translate(ss.getWidth() / 2, ss.getHeight() / 2);
- c.rotate(degrees);
- c.translate(-dims[0] / 2, -dims[1] / 2);
- c.drawBitmap(mScreenBitmap, 0, 0, null);
- c.setBitmap(null);
- mScreenBitmap = ss;
- }
- // If we couldn't take the screenshot, notify the user
- if (mScreenBitmap == null) {
- return;
- }
- // Optimizations
- mScreenBitmap.setHasAlpha(false);
- mScreenBitmap.prepareToDraw();
- img_display.setImageBitmap(mScreenBitmap);
- }
- }
- /**
- * @return the current display rotation in degrees
- */
- private float getDegreesForRotation(int value) {
- switch (value) {
- case Surface.ROTATION_90:
- return 360f - 90f;
- case Surface.ROTATION_180:
- return 360f - 180f;
- case Surface.ROTATION_270:
- return 360f - 270f;
- }
- return 0f;
- }
- }
2. AndroidManifest.xml文件
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.arvinhe.testscreenshot"
- android:versionCode="1"
- android:versionName="1.0"
- android:sharedUserId="android.uid.system">
- <uses-sdk android:minSdkVersion="15" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name=".TestScreenShotActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
3. Layout文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <Button
- android:id="@+id/bt_screenshot"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Screen Shot"
- />
- <ImageView
- android:id="@+id/img_display"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/ic_launcher"/>
- </LinearLayout>
方法2,根据DDMS里的截图功能实现,也有现成的。
来自:http://blog.csdn.net/ericahdu/article/details/5533091
我们有时候只是需要截图,没必要连DDMS一起开,所以剥离了截图的代码,当然,并不是原生的啊,是根据原理自己写的,供大家参考
第一步,准备库包
我们既然是按照DDMS的方法截图,就需要用到ddmlib.jar这个包,它位于android的SDK目录的tools/lib下,我们需要把它加入到我们
的Eclipse工程的build path下。
第二步,建立连接,获取设备
有了ddmlib,我们就可以使用里面的 AndroidDebugBridge 类来获取已经同步的设备的列表并建立连接
- IDevice device;
- AndroidDebugBridge bridge = AndroidDebugBridge.createBridge();
- waitDeviceList(bridge);
- IDevice devices[] = bridge.getDevices();
- device = devices[0];
上面的代码用到了一个waitDeviceList(bridge),主要是为了多次尝试连接,代码如下
- private static void waitDeviceList(AndroidDebugBridge bridge) {
- int count = 0;
- while (bridge.hasInitialDeviceList() == false) {
- try {
- Thread.sleep(100); // 如果没有获得设备列表,则等待
- ount++;
- } catch (InterruptedException e) {}
- if (count > 300) { // 设定时间超过300×100 ms的时候为连接超时
- System.err.print("Time out");
- break;
- }
- }
- }
这样我们就可以获得一个设备的类,IDevice,其中有一个getScreenshot()方法获得屏幕截图,类型为RawImage
- RawImage rawScreen = device.getScreenshot();
后面的方法就和Android无关了,纯粹的转换,Rawimage转换到bufferedimage,再保存
- if(rawScreen != null){
- BufferedImage image = null;
- int width2 = landscape ? rawScreen.height : rawScreen.width;
- int height2 = landscape ? rawScreen.width : rawScreen.height;
- if (image == null) {
- image = new BufferedImage(width2, height2,
- BufferedImage.TYPE_INT_RGB);
- } else {
- if (image.getHeight() != height2 || image.getWidth() != width2) {
- image = new BufferedImage(width2, height2,
- BufferedImage.TYPE_INT_RGB);
- }
- }
- int index = 0;
- int indexInc = rawScreen.bpp >> 3;
- for (int y = 0; y < rawScreen.height; y++) {
- for (int x = 0; x < rawScreen.width; x++, index += indexInc) {
- int value = rawScreen.getARGB(index);
- if (landscape)
- image.setRGB(y, rawScreen.width - x - 1, value);
- else
- image.setRGB(x, y, value);
- }
- }
- ImageIO.write((RenderedImage)image,"PNG",new File("D:/temp.jpg"));
- }
Android屏幕截图实现相关推荐
- android 屏幕截图检测,Android 屏幕截图
Android 屏幕截图 04/08/2020 本文内容 如果你的 App Center 测试报表在 Android 上一致地显示了空白或打乱的屏幕截图,则通常是因为你的应用程序已禁用屏幕截图. 此 ...
- 从FrameBuffer中获取Android屏幕截图
我们知道,DDMS可以很容易的获取Android 手机 的屏幕截图,那么它是怎么做到的呢? 其实,android手机上有一个叫做FrameBuffer的设备,图像信息都是通过FrameBuffer写到 ...
- android 屏幕截图并缩小 以及存取方法
图片缩小并由Bitmap转换为Imageview方法 static Bitmap ViewToBit(Activity activity){ float scaleWidth = 1, ...
- Android 屏幕截图
转自:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=96116&extra=page%3D1&ordertype ...
- Android 系统(227)---Android屏幕截图
屏幕截图,亲测可用,基于Activity /*** 屏幕截图* @param activity* @return*/public static Bitmap screenShot(AppCompatA ...
- Android屏幕截图,View截图(干货)
不多说废话,直接上代码 屏幕截图,亲测可用,基于Activity /*** 屏幕截图* @param activity* @return*/public static Bitmap screenSho ...
- Android 屏幕截图黑屏问题解决方法
以下两种截屏方法可能由于尺寸没有设置默认过大系统不予处理. public static Bitmap convertViewToBitmap(View view, int bitmapWidth, i ...
- Android读取/dev/graphics/fb0 屏幕截图
Android屏幕截图有很多方式这里只使用其中一种截图 主要是读取/dev/graphics/fb0,进行转换,复杂点就在如何把读取的数据进行转换. 可以参考一下这篇文章:http://blog.ch ...
- 如何以编程方式在Android上截屏?
如何通过代码而不是通过任何程序来截屏电话屏幕的选定区域? #1楼 Mualig的回答很好,但是我遇到了Ewoks描述的相同问题,但我没有得到背景知识. 因此,有时足够好,有时我会在黑色背景上出现黑色文 ...
最新文章
- Appium自动化测试-iOS真机运行
- 以下哪一个不属于python语言的特点-以下不属于python语言特点的是( )_学小易找答案...
- python语言的翻译方式是什么-python自动翻译实现方法
- Vue.js 插件开发详解
- Silverlight 游戏开发小技巧:轨迹跟随效果
- DL之DeepLabv2:DeepLab v2算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略
- FIFO分枝_限界算法
- Python安装FrankMocap实现3D人体姿态估计
- mysql not in 多列,MySQL中的多列外键?
- 关于idea的git账号与电脑的git账号不一致的问题。已解决!
- (转)Eclipse在线配置Hibernate Tools
- [SCSS] Pure CSS for multiline truncation with ellipsis
- 吴恩达神经网络和深度学习-学习笔记-44-anchor box
- 【Java程序设计】输入输出
- spss统计分析基础教程(上)--自学
- 使用_beginthread创建线程
- setlocale()函数详解——C语言
- GTD时间管理工具Omnifocus 3 Mac中文版
- OS学习笔记-4(清华大学慕课)mooc实验一
- thinkphp 框架 打开word文件的步骤
热门文章
- 基于matlab的ofdm系统仿真及性能分析,基于MATLAB的OFDM系统性能仿真
- MySQL连接Navicat
- Pycharm 金融Python实战二:用Python编写一个金融计算器——编写函数 调用命令 实例年金现值 利率换算 净现值法 投资回报期 内部收益率及其法则(带程序和结果)
- android开发的超级玛丽游戏
- server sql top速度变慢解决方案_SQL Server:执行计划教会我如何创建索引?(解决SQL加了TOP之后变很慢的问题)...
- Leetcode——507. Perfect Number
- CAD图纸可以进行哪些格式的转换呢?
- 国内CMS技术发展的外在表现形式
- iOS 非越狱下的代码注入
- 框架的概念框架(Framework)