任图片在客户端与服务端交互流自由游走~~

    xiaoxiao2021-08-17  106

    一直困扰我的问题,今天终于解决,很感谢中的大神提供的思路,现在就来整理一下。 首先在客户端本地获取到图片。然后将图片转化为byte流。在服务端接收并转化为byte流,注意!这里需要借助ByteArrayOutputStream,不然转化出来的图片是不完整或者会转化失败。

    OK,接下来上代码:

    服务端

    public class ImageViewText { private static Socket s; public static void main(String[] args){ try { ServerSocket server = new ServerSocket(6666); System.out.println("123"); s = server.accept(); System.out.println("你好"); InputStream input = s.getInputStream(); DataInputStream dos = new DataInputStream(input); ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int length = 0; while((length = dos.read(data,0,data.length))>0){ bytestream.write(data,0,length); } // dos.read(data,0,data.length); data = bytestream.toByteArray(); dos.close(); s.close(); server.close(); try { buff2Image(data,"C://123.png"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(data); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } static void buff2Image(byte[] b,String tagSrc) throws Exception { FileOutputStream fout = new FileOutputStream(tagSrc); //将字节写入文件 fout.write(b); fout.close(); } }

    客户端: public class MainActivity extends AppCompatActivity { private BottomNavigationBar bottomNavigationBar; private CustomListView listView; private static List lists; private Button button; private static String string_byte;; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(“*“, “onCreate开始执行”); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(“image/*”); startActivityForResult(intent,1); } }); }

    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode==RESULT_OK){ Uri uri = data.getData(); ContentResolver contentResolver = getContentResolver(); try { final Bitmap bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri));

    // string_byte = bitmap_Byte(bitmap); // Toast.makeText(this, string_byte, Toast.LENGTH_SHORT).show(); new Thread(new Runnable() { @Override public void run() { Socket socket = null; try { socket = new Socket(*,**); DataOutputStream out = new DataOutputStream(socket.getOutputStream()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); //读取图片到ByteArrayOutputStream bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] bytes = baos.toByteArray(); out.write(bytes); out.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }).start();

    } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } public String bitmap_Byte(Bitmap bit){ String isoString=""; ByteArrayOutputStream bos=new ByteArrayOutputStream(); bit.compress(Bitmap.CompressFormat.JPEG, 100, bos);//参数100表示不压缩 byte[] bytes=bos.toByteArray(); try { isoString = new String(bytes,"ISO-8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return isoString;

    // return Base64.encodeToString(bytes, Base64.DEFAULT); }

    至此,结束。

    转载请注明原文地址: https://ju.6miu.com/read-676498.html

    最新回复(0)