java PC端获取android手机截图使用ddmlib

21 篇文章 0 订阅

http://blogs.360.cn/360qtest/2014/02/28/%E9%80%9A%E8%BF%87ddmlib%E5%AE%9E%E7%8E%B0%E6%88%AA%E5%9B%BE%E4%BB%A5%E5%8F%8A%E8%8E%B7%E5%8F%96%E6%89%8B%E6%9C%BA%E7%9B%B8%E5%85%B3%E4%BF%A1%E6%81%AF/

ddmlib在sdk tools\libs文件夹下  ddmlib.jar

AndroidDebugBridge.init(false) ;这句话需要调用,否则会报错

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;


import javax.imageio.ImageIO;


import com.android.ddmlib.AdbCommandRejectedException;
import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.RawImage;
import com.android.ddmlib.TimeoutException;




public class Main {


	private static void waitDeviceList(AndroidDebugBridge bridge) {   
	    int count = 0;   
	    while (bridge.hasInitialDeviceList() == false) {    
	       try {   
	       Thread.sleep(100); // 如果没有获得设备列表,则等待   
	       count++;   
	       } catch (InterruptedException e) {}   
	       if (count > 300) {    // 设定时间超过300×100 ms的时候为连接超时   
	       System.err.print("Time out");   
	       break;   
	       }   
	    }   
	}  
	
	public static void main(String[] args) {
		IDevice device;   
		AndroidDebugBridge.init(false) ;
		AndroidDebugBridge bridge = AndroidDebugBridge.createBridge("E:\\android-sdk\\platform-tools\\adb.exe" , false);   
		waitDeviceList(bridge);   
		  
		IDevice devices[] = bridge.getDevices();   
		device = devices[0];
		try {
			RawImage rawScreen = device.getScreenshot() ;
			
			if (rawScreen != null) {
				Boolean landscape = false;
				int width2 = landscape ? rawScreen.height : rawScreen.width;
				int height2 = landscape ? rawScreen.width : rawScreen.height;
				BufferedImage image = null ;
				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);
					}
				}
				try {


					ImageIO.write((RenderedImage) image, "PNG", new File("f:\\2.png"));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (AdbCommandRejectedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


下面是android上获取截图的办法
https://github.com/Yhzhtk/note/issues/7



FileInputStream graphics = null;
            try {
                    graphics = new FileInputStream(“/dev/graphics/fb0”);
            } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return null;
            }

            DataInputStream dStream = new DataInputStream(graphics);
            dStream.readFully(piex);
            dStream.close();

            int[] colors = new int[screenHeight * screenWidth];
            // 将rgb转为色值
            for (int m = 0; m < colors.length; m++) {
                    int r = (piex[m * 4] & 0xFF);
                    int g = (piex[m * 4 + 1] & 0xFF);
                    int b = (piex[m * 4 + 2] & 0xFF);
                    int a = (piex[m * 4 + 3] & 0xFF);
                    colors[m] = (a << 24) + (r << 16) + (g << 8) + b;
            }

            Bitmap bitmap = Bitmap.createBitmap(colors, screenWidth, screenHeight,
                            Bitmap.Config.ARGB_8888);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值