I'm sending a Bitmap using Socket
where Client is my .apk and Server is a Delphi executable.
The Bitmap is sent with success, but when is received on Server, Bitmap
is empty with 0KB of size.
So, i want any suggestion or solution if possible, for solve this trouble.
Here is my code until now:
Android (client)
public class MainActivity extends Activity {
Socket clientSocket;
private static final int SERVERPORT = 60;
private static final String SERVER_IP = "192.168.25.227";
byte[] tmpbytes = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new ClientThread()).start();
}
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void getBytes() throws IOException {
try
{
Bitmap bmp = takeScreenshot();
int bytes = bmp.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bmp.copyPixelsToBuffer(buffer);
byte[] array = buffer.array();
int start=0;
int len=array.length;
if (len < 0)
throw new IllegalArgumentException("Negative length not allowed");
if (start < 0 || start >= array.length)
throw new IndexOutOfBoundsException("Out of bounds: " + start);
OutputStream out = clientSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(len);
if (len > 0) {
dos.write(array, start, len);
}
}
catch (UnknownHostException e) {
//System.out.println(e.toString());
}
catch (IOException e) {
//System.out.println(e.toString());
}
catch (Exception e1) {
//Log.e("clients", e1.toString());
//Toast.makeText(MainActivity.this, e1.toString(), Toast.LENGTH_LONG).show();
System.out.println(e1.toString());
}
}
class ClientThread implements Runnable {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
clientSocket = new Socket(serverAddr, SERVERPORT);
//new Thread(new CommsThread()).start();
Thread.sleep(1000);
getBytes();
} catch (Exception e1) {
//Log.e("clients", e1.toString());
//Toast.makeText(MainActivity.this, e1.toString(), Toast.LENGTH_LONG).show();
System.out.println(e1.toString());
}
}
}
}
Delphi (server)
var
Form1: TForm1;
stSize: integer;
Stream: TMemoryStream;
bmp: TBitmap;
FSize: Integer;
writing: Boolean;
procedure TForm1.FormCreate(Sender: TObject);
begin
Stream:= TMemoryStream.Create;
writing:= False;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
BytesReceived: Longint;
CopyBuffer: Pointer; { buffer for copying }
ChunkSize: Integer;
TempSize: Integer;
const
MaxChunkSize: Longint = 8192; { copy in 8K chunks }
begin
If FSize=0 then
begin
If Socket.ReceiveLength>SizeOf(TempSize) then
begin
Socket.ReceiveBuf(TempSize,SizeOf(TempSize));
Stream.SetSize(TempSize);
FSize:= TempSize //Threadsafe code!
End;
End;
If (FSize>0) and not(writing) then
begin
GetMem(CopyBuffer, MaxChunkSize); { allocate the buffer }
writing:= True;
While Socket.ReceiveLength>0 do
Begin
ChunkSize:= Socket.ReceiveLength;
If ChunkSize > MaxChunkSize then ChunkSize:= MaxChunkSize;
BytesReceived:= Socket.ReceiveBuf(CopyBuffer^,ChunkSize);
Stream.Write(CopyBuffer^, BytesReceived); { ...write chunk }
Dec(FSize,BytesReceived);
End;
end;
If FSize=0 then begin
Stream.Position := 0;
bmp:=TBitmap.Create;
bmp.LoadFromStream(Stream);
Image1.Picture.Graphic := bmp;
Stream.SetSize(0);
bmp.Free;
FSize:= 0;
end;
FreeMem(CopyBuffer, MaxChunkSize); { allocate the buffer }
Writing:= False;
end;
:
after suggestion of Remy Lebeau, my changes done still dont't works :-(
Delphi
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
BytesReceived: Longint;
CopyBuffer: Pointer;
ChunkSize: Integer;
TempSize: Integer;
const
MaxChunkSize: Longint = 8192;
begin
If FSize=0 then
begin
If Socket.ReceiveLength>SizeOf(TempSize) then
begin
Socket.ReceiveBuf(TempSize,SizeOf(TempSize));
TempSize := ntohl(TempSize); // Changed to ntohl
Stream.SetSize(TempSize);
FSize:= TempSize
End;
End;
If (FSize>0) and not(writing) then
begin
GetMem(CopyBuffer, MaxChunkSize);
writing:= True;
While Socket.ReceiveLength>0 do
Begin
ChunkSize:= Socket.ReceiveLength;
If ChunkSize > MaxChunkSize then ChunkSize:= MaxChunkSize;
BytesReceived:= Socket.ReceiveBuf(CopyBuffer^,ChunkSize);
Stream.Write(CopyBuffer^, BytesReceived);
Dec(FSize,BytesReceived);
End;
end;
If FSize=0 then begin
Stream.Position := 0;
png:=TPngImage.Create; // Changed to TPNGImage here
png.LoadFromStream(Stream);
Image1.Picture.Assing(png);
Stream.SetSize(0);
png.Free;
FSize:= 0;
end;
FreeMem(CopyBuffer, MaxChunkSize);
Writing:= False;
end;
Android
public void getBytes() throws IOException {
try
{
Bitmap bmp = takeScreenshot();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] array = bos.toByteArray();
OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(array.length);
dos.write(array, 0, array.length);
}
catch (UnknownHostException e) {
//System.out.println(e.toString());
}
catch (IOException e) {
//System.out.println(e.toString());
}
catch (Exception e1) {
//Log.e("clients", e1.toString());
//Toast.makeText(MainActivity.this, e1.toString(), Toast.LENGTH_LONG).show();
System.out.println(e1.toString());
}
}