80,351
社区成员
发帖
与我相关
我的任务
分享
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class SearchActivity extends AppCompatActivity {
public Bitmap bm = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
TextView textView1 = (TextView) findViewById(R.id.result_text_1);
ImageView picture1 = (ImageView) findViewById(R.id.result_pic_1);
TextView textView2 = (TextView) findViewById(R.id.result_text_2);
ImageView picture2 = (ImageView) findViewById(R.id.result_pic_2);
TextView textView3 = (TextView) findViewById(R.id.result_text_3);
ImageView picture3 = (ImageView) findViewById(R.id.result_pic_3);
Button send = (Button)findViewById(R.id.send);
textView1.setText("try_1:hhh");
textView2.setText("try2:我买了一个iphonexxx");
textView3.setText("try3:ttt");
Intent intent = this.getIntent();
byte buffer[] = intent.getByteArrayExtra("image");
bm = BitmapFactory.decodeByteArray(buffer,0,buffer.length); // 解码文件
Log.w("TAG", "size: " + bm.getByteCount() + " width: " + bm.getWidth() + " heigth:" + bm.getHeight()); // 输出图像数据
picture1.setImageBitmap(bm);
picture2.setImageBitmap(bm);
picture3.setImageBitmap(bm);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new SendPicture().execute(bm);
}
});
//Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
//picture1.setImageBitmap(bitmap);
}
class SendPicture extends AsyncTask<Bitmap,Void,Bitmap>{
private static final String SOCKET_IP = "10.30.73.82";
private static final int SOCKET_PORT = 8000;
private Bitmap bmp;
private DataOutputStream dataOutputStream = null;
private ByteArrayOutputStream out = null;
private DataInputStream dataInputStream = null;
private ByteArrayInputStream in = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(Bitmap... bitmaps) {
Bitmap bitmap = bitmaps[0];
try{
Socket socket = new Socket(SOCKET_IP,SOCKET_PORT);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,out);
byte[] sendBytes = out.toByteArray();
int size = sendBytes.length;
dataOutputStream.writeInt(size);
dataOutputStream.flush();
dataOutputStream.write(sendBytes);
dataOutputStream.flush();
//dataOutputStream.close();
dataInputStream = new DataInputStream(socket.getInputStream());
try {
size = dataInputStream.readInt();
}catch (IOException e){
e.printStackTrace();
}
byte[] receiveBytes = new byte[size];
int hasReceived = 0;
while (hasReceived <size){
hasReceived+=dataInputStream.read(receiveBytes,hasReceived,size-hasReceived);
}
bmp = BitmapFactory.decodeByteArray(receiveBytes, 0,receiveBytes.length);
}catch (Exception e){
e.printStackTrace();
}
return bmp;
}
@Override
protected void onPostExecute(Bitmap bmp) {
textView1.setText("try_1:hhh");
}
}
}