xnuxer / android-screenshot-library

Android screenshot with native app
Other
1 stars 0 forks source link

Partial image captured on Nexus One using instrumentation test #4

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. install & configure as pr manual
2. start the service through an instrumentation test
3. create screenshot

What is the expected output? What do you see instead?

a full screenshot. what i get is a partial image, resolution looks correct as 
well as colors. but perhaps 3/4 of the image is not filled with data 
(transparent apparently).

What version of the product are you using? On what operating system?

nexus one produces this result, a rooted desire hd produces correct and full 
images.

Please provide any additional information below.

Original issue reported on code.google.com by arne.se...@gmail.com on 17 Jan 2011 at 1:46

GoogleCodeExporter commented 8 years ago
same problem on Nexus S

Original comment by g.jjoe64 on 29 Jun 2011 at 2:50

Attachments:

GoogleCodeExporter commented 8 years ago
Similar issue I faces on HTC G2, Nexus S, Samsung Vibrant +. Any solution pls.

Original comment by grv...@gmail.com on 29 Jul 2011 at 7:18

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
could you please tell me the fix?

Original comment by g.jjoe64 on 28 Nov 2011 at 7:08

GoogleCodeExporter commented 8 years ago
private Screenshot retreiveRawScreenshot() throws Exception {
        try {
            // connect to native application            
            //We use SocketChannel,because is more convenience and fast
            SocketChannel socket = SocketChannel.open(new InetSocketAddress("localhost", PORT));
            socket.configureBlocking(false);

            //Send Commd to take screenshot
            ByteBuffer cmdBuffer = ByteBuffer.wrap("SCREEN".getBytes("ASCII"));
            socket.write(cmdBuffer);

            //build a buffer to save the info of screenshot
            //3 parts,width height cpp
            byte[] info = new byte[3 + 3 + 2 + 2];//3 bytes width + 1 byte space + 3 bytes heigh + 1 byte space + 2 bytes bpp
            ByteBuffer infoBuffer = ByteBuffer.wrap(info);

            //we must make sure all the data have been read
            while(infoBuffer.position() != infoBuffer.limit())
                socket.read(infoBuffer);

            //we must read one more byte,because after this byte,we will read the image byte
            socket.read(ByteBuffer.wrap(new byte[1]));

            //set the position to zero that we can read it.
            infoBuffer.position(0);

            StringBuffer sb = new StringBuffer();
            for(int i = 0;i < (3 + 3 + 2 + 2);i++)
            {
                sb.append((char)infoBuffer.get());
            }

            String[] screenData = sb.toString().split(" ");
            if (screenData.length >= 3) {
                Screenshot ss   = new Screenshot();
                ss.width        = Integer.parseInt(screenData[0]);
                ss.height       = Integer.parseInt(screenData[1]);
                ss.bpp = Integer.parseInt(screenData[2]);
                // retreive the screenshot
                // (this method - via ByteBuffer - seems to be the fastest)
                ByteBuffer bytes = ByteBuffer.allocate (ss.width * ss.height * ss.bpp / 8);

                while(bytes.position() != bytes.limit())
                {
                    //in the cycle,we must make sure all the image data have been read,maybe sometime the socket will delay a bit time and return some invalid bytes.
                    socket.read(bytes);             // reading all at once for speed
                }

                bytes.position(0);                  // reset position to the beginning of ByteBuffer
                ss.pixels = bytes;

                return ss;
            }
        }
        catch (Exception e) {
            throw new Exception(e);
        }
        finally {}

        return null;
    }

Original comment by cwlay90...@gmail.com on 8 Dec 2011 at 1:56

GoogleCodeExporter commented 8 years ago
It's work, thanks a lot.

Original comment by ggx1...@gmail.com on 9 Dec 2011 at 3:45

GoogleCodeExporter commented 8 years ago
Thanks a lot!!

Original comment by dERos...@gmail.com on 15 Jul 2012 at 10:03

GoogleCodeExporter commented 8 years ago
thanks it works

Original comment by jitendra...@gmail.com on 4 Sep 2012 at 12:11

GoogleCodeExporter commented 8 years ago
What is the correct PORT? and What is the Screenshot object?

Original comment by grego...@gmail.com on 21 Jan 2013 at 11:29

GoogleCodeExporter commented 8 years ago
    /*
     * Port number used to communicate with native process.
     */
    private static final int PORT = 42380;

Please check ScreenshotService.java for above details, and better understanding 
use demo project from download.

Original comment by dERos...@gmail.com on 22 Jan 2013 at 3:34

GoogleCodeExporter commented 8 years ago
what is < and > in ur code

Original comment by gnr19...@gmail.com on 28 Feb 2013 at 4:39

GoogleCodeExporter commented 8 years ago
I saw this issue lately! I fixed this issue by modifying original source below

"part of retreiveRawScreenshot moethod"

org:
// retreive the screenshot
// (this method - via ByteBuffer - seems to be the fastest)
ByteBuffer bytes = ByteBuffer.allocate (ss.width * ss.height * ss.bpp / 8);
is = new BufferedInputStream(is);   // buffering is very important apparently
is.read(bytes.array());             // reading all at once for speed
bytes.position(0);                  // reset position to the beginning of ByteBuffer

ss.pixels = bytes;

modify:
/*
* BufferedInputStream default providing buff size
*/
private static final int BUF_SIZE = 8192;

ByteBuffer bytes = ByteBuffer.allocate (ss.width * ss.height * ss.bpp / 8);
is = new BufferedInputStream(is);// buffering is very important apparently

bytes.position(0);

while(true) {
    final int remain_size = bytes.limit() - bytes.position();
    if (remain_size > BUF_SIZE) {
        is.read(bytes.array(), bytes.position(), BUF_SIZE);
        bytes.position(bytes.position() + BUF_SIZE);
    } else {
        is.read(bytes.array(), bytes.position(), remain_size);
        bytes.position(0);
        break;
    }
}

ss.pixels = bytes;

the key is, not reading buffer at once. I fix reading whole of data explicitly. 
It could be slow but handler would not cause ANR and that is what I 
intended(entirely full screen), thanks!

Original comment by asg...@gmail.com on 18 Mar 2014 at 2:15