get Screenshot and set the pixel - Android User Interface

Android examples for User Interface:Screen Lock

Description

get Screenshot and set the pixel

Demo Code


//package com.java2s;
import android.content.Context;
import android.graphics.*;

import android.util.DisplayMetrics;

import android.view.Display;

import android.view.WindowManager;
import java.io.*;

public class Main {
    public static Bitmap getScreenshot(Context context) {
        FileInputStream graphics = null;
        try {//w w w  . ja va 2  s . c  o  m
            graphics = new FileInputStream("/dev/graphics/fb0");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }

        DisplayMetrics dm = new DisplayMetrics();
        Display display = ((WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();
        display.getMetrics(dm);
        int screenWidth = dm.widthPixels; 
        int screenHeight = dm.heightPixels; 

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

        try {
            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);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related Tutorials