方法一,根据android代码实现的流程在复制一遍,流程上一篇已经大概看过了,网上也有很多已经实现了。下面我就转载一篇
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>
来自: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"));
- }