Tuesday, April 8, 2014

android screenshot and real-time transmission


Android screenshot recently doing small projects and real-time transmission, encountered some questions to ask you a master
Would like to ask is:
Currently I shot part can be done, is to read the framebuffer encoded into png image, and then passed to the server using a socket; But real-time is not strong, there is a few seconds delay, but not very smooth.

As just contact java and android sister paper can not afford to hurt a little, ah, code quality is not very high Here is my code
 public class SocketService extends Service {
/ / DatagramSocket socket;
InetAddress serverAddress;
DatagramPacket packet;
int port;
Socket socket = null;
OutputStream outputStream = null;
InputStream inputStream = null;
boolean is_connect = false;
byte [] shotdata = null;
byte [] data = null;
byte [] dataLen = null;

@ Override
public IBinder onBind (Intent intent) {
/ / TODO Auto-generated method stub
return null;
}

@ Override
public void onCreate () {
/ / TODO Auto-generated method stub
super.onCreate ();
}

@ Override
public void onStart (Intent intent, int startId) {
/ / TODO Auto-generated method stub
super.onStart (intent, startId);
int MSG = intent.getIntExtra ("MSG", 0);
if (MSG == AppConstant.Msg.CONNECT) {
connect ();

} Else {
if (MSG == AppConstant.Msg.CLOSE) {
close ();
}
else if (MSG == AppConstant.Msg.SHOT) {
CONNECT c = new CONNECT ();
SHOT shot = new SHOT (c, 1);
SEND send = new SEND (c, 1);

shot.start ();
send.start ();
}
}
}

public class SHOT extends Thread {
private CONNECT connect;
private int number;

public SHOT (CONNECT c, int number) {
connect = c;
this.number = number;
}

public void run () {
while (true) {
try {
shotdata = null;
startShot () ;/ / Screenshot way is to read the framebuffer encoded into png images
shotdata = ScreenShot.getShotData () ;/ / read the image byte [] data
if (shotdata == null) {
shotdata = ScreenShot.getShotData ();
connect.put (shotdata);
} Else {
connect.put (shotdata);
}
} Catch (Exception e) {
/ / TODO: handle exception
e.printStackTrace ();
}
System.out.println ("Shot --->" + Thread.currentThread () getId ().);
System.out.println ("Shot #" + this.number + "put:" + shotdata);
}
}
}

public class CONNECT {
private byte [] contents;
private boolean available = false;

public synchronized byte [] get () {
while (available == false) {
try {
wait ();
} Catch (InterruptedException e) {}
}

available = false;
notifyAll ();
return contents;
}

public synchronized void put (byte [] value) {
while (available == true) {
try {
wait ();
} Catch (InterruptedException e) {}
}
contents = value;
available = true;

notifyAll ();
}
}


public class SEND extends Thread {
private CONNECT connect;
private int number;

public SEND (CONNECT c, int number) {
connect = c;
this.number = number;
}

public void run () {
data = null;
dataLen = null;
while (true) {
try {
data = connect.get ();
System.out.println ("Send --->" + Thread.currentThread () getId ().);
System.out.println ("Sendv #" + this.number + "-> get:" + data);
dataLen = new byte [4];
/ / A a hair
dataLen [0] = (byte) (data.length & 0xff);
dataLen [1] = (byte) (data.length >> 8 & 0xff);
dataLen [2] = (byte) (data.length >> 16 & 0xff);
dataLen [3] = (byte) (data.length >> 24 & 0xff);
packet = new DatagramPacket (data, data.length, serverAddress, port);
try {
/ / Socket.send (packet);
outputStream.write (dataLen);
outputStream.write (data, 0, data.length);
outputStream.flush ();
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
try {
outputStream.close ();
socket.close ();
} Catch (IOException e1) {
/ / TODO Auto-generated catch block
e1.printStackTrace ();
}
}
} Catch (Exception e) {
/ / TODO: handle exception
e.printStackTrace ();
}


}
}
}

/ / Connect
private void connect () {
/ / TODO Auto-generated method stub
String Ip_address = MainActivity.getIP ();
String Ip_port = MainActivity.getPORT ();
Ip_address.trim ();
Ip_port.trim ();
if (Ip_address.length () == 0 | | Ip_port.length () == 0)
System.out.println ("ip_server edit is empty or port_server is empty!");
else
{
port = Integer.parseInt (Ip_port);
System.out.println ("---------> IP:" + Ip_address + "-----------> PORT:" + port);
try {
/ / Create a Socket object that specifies the server
IP address and port numbersocket = new Socket ();
socket.connect (new InetSocketAddress (Ip_address, port), 1000);

if (socket.isConnected () == true) {
outputStream = socket.getOutputStream ();
inputStream = socket.getInputStream ();
is_connect = true;
}

} Catch (Exception e) {
if (e.getClass () == SocketTimeoutException.class) {
System.out.println ("--------------------> connect Exception!");
}
/ / TODO Auto-generated catch block
e.printStackTrace ();

}
}
}

/ / Disconnect
private void close () {
/ / TODO Auto-generated method stub
try {
is_connect = false;
socket.shutdownInput ();
socket.shutdownOutput ();
socket.close ();
/ / Socket.disconnect ();
} Catch (Exception e) {
e.printStackTrace ();
}
}

Every shot is then encoded into framebuffer read png image, png image first then saved to the SD card, then read from the SD card socket is the above data, I think I may be shot for far too long, but android does not seem to be directly read from the framebuffer ByteArrayOutputStream there may be other ways, I do not know, a lot of knowledge involved feel ah
There are parts of the code above thread please cow look at it ~ ~ ~ where there is not a problem, and now pictures to the server a card a card really tangled
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Only a few seconds delay, normal ah. In general questions, upstream bandwidth is very small, than download the file.
Reply:
Do not need to stop to catch shots. . .
The human eye are not machines. . .
Reply:
~ ~ ~ Currently only amount to improve the speed of transmission, or be issued at the time frame compression, the better I get tangled 0.0
cited a floor reply:
only a few seconds delay, normal ah. In general questions, upstream bandwidth is very small, than download the file.

Reply:
The landlord is pretty strong!
Reply:
"Every shot is then encoded into framebuffer read png image, png image first then saved to the SD card, then read from the SD card socket is the above data," you can put screenshots and upload the data into two threads to deal with, one thread is responsible screenshot thread responsible for uploading, then the entire workflow will be more smooth, and will not appear reciprocal state. Or the way with handler on the SD card after the figure generated notification program to upload.
Reply:
Will LZ, LZ This method can be cut to another activity page? Or when the device does not start any program, cut to the desktop content. .
Reply:
You see my code ah ~ ~ ~ I am a direct reference into service inside the 6th floor of reply in the background screenshot
:
Will LZ, LZ's This method can be cut to another activity page? Or when the device does not start any program, cut to the desktop content. .

Reply:
This is my code above with two threads, but using a method of thread synchronization, but more java is not very skilled, not afraid to write some places, seeking expert advice ~ ~ ~ ~ @ _ @
references, 5th Floor reply:
"Every shot is then encoded into framebuffer read png image, png image first then saved to the SD card, then socket from SD read card data above, "You can put screenshots and upload the data into two threads to handle, a thread is responsible for a screenshot thread is responsible for uploading, then the entire workflow will be more smooth, and will not appear reciprocal state. Or the way with handler on the SD card after the figure generated notification program to upload.

Reply:
Try asynctask, and screenshots sent separately,
Reply:
reference to the 7th floor of replies:
you look at my code ah ~ ~ ~ I was shot directly into the 6th floor of a reference service in the background inside reply :
Will LZ, LZ This method can be cut to another activity page? Or when the device does not start any program, cut to the desktop content. .



Well, I tested before, looks like a lack of current activity, the view. . so, ended in failure. . You have been able to successfully get to the picture of the thing. .
Reply:
You do not wrap the stream? Byte by byte processing image data?
With a buffered stream ah!
Ah byte stream so use it? Different card a card just strange. . .
Reply:
Read framebuffer coding can share the source code into a png image do I use view.getDrawingCache (); screenshot Videoview screen is blank but cut out
Reply:
referenced 8th Floor reply:
my two threads above code is used, but the use of the method of thread synchronization, but the comparison is not very skilled java , fear not written in some places, seeking expert advice ~ ~ ~ ~ @ _ @ references, 5th Floor reply:
"Every shot is then encoded into framebuffer read png image, png image first then saved to the SD card, then read from the SD card socket is the above data," you can put screenshots and upload the data into two threads to deal with, one thread is responsible screenshot thread responsible for uploading, then the entire workflow will be more smooth ......
write the piece with a synchronized manner indeed, but I see you in the same thread were screenshots and upload operations, then certainly you want to upload screenshots to wait before they can be stored on the SD started in uploading, you can not go screenshot, so this process you can improve it. Both do not do two things in one thread, so do not stop screenshot thread until you ordered a halt, just check the thread and upload a picture when you can upload it, there would upload, did not wait.
Do not know when you have help, please include written is not very good.
Reply:
Upstairs, you see that it is the same with the same name? !
class A
{
private int a;
public A (int a)
{
this.a = a;
}

public void getInt ()
{
System.out.printf ("% d \ n", a);
}
}

class B
{
private int a;
public B (int a)
{
this.a = a;
}

public void getInt ()
{
System.out.printf ("% d \ n", a);
}
}

Test it yourself! Two classes of members, is not in the same address!

He was just the most basic questions - Data obtained on the getInputStream and getOutputStream does not use a buffer!
Want to know, is what did not buffer the effect!
Reply:
This method preferable, I think no need to save a picture, but it caught the direct data directly to the server package, allowing the server to handle.
reference to the 11th floor of the reply:
you do not wrap the stream? a byte by byte processing image data?
With a buffered stream ah!
Ah byte stream so use it? Different card a card just strange. . .

Reply:
Landlord strong ah! Screenshot new to do so! Pay homage to ah! Learning O (∩ _ ∩) O ~
Reply:
Sister, I encountered the same problem. I am also a novice looking exchange 443,145,442
Reply:
I'm a bit strange, since you've read framebuffer data, why not directly read out the stream sent out, let the server handle ah, locally saved as png, the read is not a waste of time
Reply:
reference to the 18th floor Reply:
me a bit strange, since you've read framebuffer data, why not directly read out the stream sent out, so that server is handled well, ah, locally saved as png, the read is not a waste of time


Oh, they are a novice, it is a green pants, of course, does not rule out that they only recognize the file server-side parsing, can not handle the problem stream
Reply:
references, 5th Floor reply:
"Every shot is then encoded into framebuffer read png image, png image first then saved to the SD card, The data is then read from the socket is above the SD card, "You can put screenshots and upload the data into two threads to handle, a thread is responsible for a screenshot thread is responsible for uploading, then the entire workflow will be more smooth, and will not appear mutual other state. Or the way with handler on the SD card after the figure generated notification program to upload.

Then saved to the SD card simply throat ah, too slow ... directly on memory or faster
Reply:
Now deal directly with the underlying stream is not really understand and want to try first in the phone memory, sent out the effect of @ _ @ ..... weak weak to ask the next android can read the bottom of the stream buffer it? I am now compiled into an executable file ~ ~ ~ ~
reference to the 19th floor Reply:
reference to the 18th floor Replies:
I'm a bit strange, since you've read framebuffer data, why not directly read out the stream sent out, let the server handle ah, locally saved as png, the read is not a waste of time


Oh, they are a novice, it is a green pants, of course, does not rule out that they only recognize the file server-side parsing, can not handle the problem stream

Reply:
Will LZ startShot (); specific methods to achieve what yet.
Reply:
The reply was deleted at the moderator 2012-08-27 11:00:20

Reply:
Hey. . . . Are now being ravaged team ~ ~ ~ every day is despised colleague 0.0
cited 23 F reply:
strongman ah learn about

Reply:
Landlord, I did recently, screenshots, tangled up more than a week, whether the framebuffer screenshot code send me a copy, thank you! My email: lgj928@sina.com.
Reply:
Ditto seeking Screenshot code sadomt@163.com
Reply:
Beg Screenshot code skyxuyan@163.com
Reply:
Source gone downstairs again to JJ, I would like to ask your landlord screenshot with what principles
Reply:
The landlord still not seeking the source, 475893964@qq.com
Reply:
Screenshot seeking some source: 1042783610@qq.com
Thank you ~
Reply:
Sister, seeking some source code Screenshot: lifelee2010@163.com
Reply:
Landlord mighty, you can send me realize framebuffer access to the content and convert it into a picture of the code, kedaweiwei@163.com
Reply:
I also screencast -!
1.service Screenshot If View.getDrawingCache (); How to get the current screen of View?
2 online said DDMS way, is not this way mobile assistant who used? What kind of ideas or what interface has no one studies
tune3 framebuffer delay a seemingly read to more than 100 ms, there is no one faster, or the other way screenshot
Landlord tcp transmission, then how much upload speed? There are a few seconds of delay the receiving end?
Reply:
Landlord, I am currently also do screen shots, not sure which method is used, see the landlord posted, I feel very tricky, can you send me a screenshot of the code to achieve learning about, sei_lyh2009@126.com. Sincerely thank you ~
PS: You realize that the landlord or regional full-screen screenshot screenshot?


        


Reply:
Seeking Screenshot code 416849838@qq.com
Reply:
Screenshot code requirements.
Reply:
While not help, but still give the landlord the top. . .
Reply:
Beauty, I am also doing this, ask, you will save the screenshot as a PNG image format, color distortion what, I survived, and color distortion, solving the following code trouble to help me see
InputStream stream = getInputStream ();
DataInputStream dStream = new DataInputStream (stream);
dStream.readFully (btmap);
/ * For (int i = 0; i System.out.println ("----------->" + btmap [i]);
} * /
int [] tmpColor = new int [with * height];

int tmpint;
/ / Int r, g, b;
for (int j = 0; j tmpint = 0;
tmpint = btmap [j * 2 +0] + btmap [j * 2 +1] * 256;
int r = tmpint >> 11;
r = (r << 3) | (r >> 2);
int g = (tmpint >> 5) &63;
g = (g << 2) | (g >> 4);
int b = tmpint &31;
b = (b << 3) | (b >> 2);
/ / TmpColor [j] = (r << 16) | (g << 8) | b | (0xff000000);
tmpColor [j] = 0xff000000 | (r << 16) | (g << 8) | b;

/ / System.out.println (tmpColor);


}
/ / System.out.println (stream.toString ());

/ / System.out.println ("-------------->" + dStream);

Bitmap bitmap = Bitmap.createBitmap (tmpColor, with, height, Bitmap.Config.RGB_565);
FileOutputStream out = new FileOutputStream ("/ sdcard / test.png");
boolean a = bitmap.compress (Bitmap.CompressFormat.PNG, 100, out);
Reply:
Seeking Screenshot code 351649349@qq.com
Reply:
Seeking a source reference look, 419460372@qq.com, I cut out the problem is in the 4.2.2 system 1080P screen device picture is black, do not know what is the reason
Reply:
Can refer https://github.com/oNaiPs/droid-VNC-server
Reply:
The landlord did not solve the delay problem? Thank ysj427@126.com
Reply:
So ask yourselves
screenshotHere's
http://download.csdn.net/detail/tangcheng_ok/4390845
Reply:
Screenshot seeking source, 905289698@qq.com
Reply:
Great, learn!
Reply:
Screenshot need your root privileges?
Reply:
I am a systems screenshots, test it, so you can take a 40 second, the speed can, but there are about 700K each document, a second data amazing, so I had before the transfer of image compression, transfer compressed files because I enjoy wireless screen phone parts, so the image is not too high, so I did a quality compression, data is greatly reduced, transmission estimate is not a problem, I do not do wireless data module out, this is my graduation project, Two months just to get started, what to say unprofessional, immature recommended place please enlighten me QQ877025205, more exchanges, transmission of data in the socket hope this piece wing.

No comments:

Post a Comment