读/ dev / graphics / fb0数据只能得到黑屏?

时间:2014-07-18 03:05:36

标签: java android graphics screen capture

我想使用fb0文件来捕获相机(SurfaceView)和一些按钮。但是,我只能阅读黑屏。

我已获得dev/grapgics/fb0的root权限和rwxrwxrwx权限。 我不知道如何解决它。

这是我的截屏代码:

public class Screenshot {

    final static String FB0FILE1 = "/dev/graphics/fb0";
    final static String FB0FILE2 = "/dev/fb0";

    static File fbFile;
    static FileInputStream graphics = null;
    static int screenWidth = 720;
    static int screenHeight = 1280;
    static byte[] piex;

    public static void init(Activity context) {
        fbFile = new File(FB0FILE1);
        if (!fbFile.exists()) {
            File nFile = new File(FB0FILE2);
            if (nFile.exists()) {
                fbFile = nFile;
            }
        }

        try {
            Process sh = Runtime.getRuntime().exec("su", null, null);

            OutputStream os = sh.getOutputStream();
            os.write(("chmod 777 " + fbFile.getAbsolutePath()).getBytes());
            os.flush();
            os.close();
            sh.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }

        DisplayMetrics dm = new DisplayMetrics();
        Display display = context.getWindowManager().getDefaultDisplay();
        display.getMetrics(dm);
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;

        PixelFormat pixelFormat = new PixelFormat();
        PixelFormat.getPixelFormatInfo(PixelFormat.RGBA_8888, pixelFormat);
        int deepth = pixelFormat.bytesPerPixel;
        piex = new byte[screenHeight * screenWidth * deepth];
    }

    @SuppressLint("SdCardPath")
    public static void testShot() {
        long start = System.currentTimeMillis();
        try {
            Bitmap bm = getScreenBitmap();
            saveMyBitmap(bm, "/sdcard/" + System.currentTimeMillis() + ".png");
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        Log.i("Screenshot", "time cost:" + (end - start));
    }

    public static void saveMyBitmap(Bitmap bitmap, String bitName)
            throws IOException {
        File f = new File(bitName);
        f.createNewFile();
        FileOutputStream fOut = new FileOutputStream(f);

        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    }

    public synchronized static Bitmap getScreenBitmap() throws IOException {
        try {
            graphics = new FileInputStream(fbFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }

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

        int[] colors = new int[screenHeight * screenWidth];

        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;
        }

        return Bitmap.createBitmap(colors, screenWidth, screenHeight,
                Bitmap.Config.ARGB_8888);
    }
}

0 个答案:

没有答案