经历一个发誓的问题。这是我用于android客户端应用程序的代码。我不知道,因为我想将此捕获的图像发送到C#服务器。有人可以指导我或提供代码吗?刚刚放弃了在GOOGLE中搜索此类内容! -_-
如果有人在这方面帮助我,我会请您!

“我正在使用的Android代码”

package net.simplifiedcoding.androidcameraapp;
import java.io.BufferedWriter;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.InetAddress;

import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.graphics.Bitmap.CompressFormat;
import android.view.View;


public class MainActivity extends ActionBarActivity {


public void send_picture(View view) {


}

private Socket socket;

private  static final int SERVERPORT = 1234;
private  static  final String SERVER_IP = "My IP Address";

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(new ClientThread()).start();

    Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );
    btnCamera = (Button) findViewById(R.id.btnCamera);

    capturedImage= (ImageView) findViewById(R.id.capturedImage);

    btnCamera.setTypeface(font);

    btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openCamera();
        }
    });


    }

    public void onClick(View view) {
    try {
        ImageView imageView=(ImageView) findViewById(R.id.capturedImage);
        Bitmap bmp=((BitmapDrawable)imageView.getDrawable()).getBitmap();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG,0,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) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    }
   class  ClientThread implements Runnable {
    @Override
    public void run(){
        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
            socket = new Socket(serverAddr, SERVERPORT);
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}



private Button btnCamera;
private ImageView capturedImage;





private void openCamera() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK) {
        Bitmap bp = (Bitmap) data.getExtras().get("data");
        capturedImage.setImageBitmap(bp);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}


这是xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/camera_button"
    android:id="@+id/btnCamera"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/button"
    android:textColor="#ffd3ffe5"
    android:textStyle="bold" />

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="370dp"
    android:id="@+id/capturedImage"
    android:layout_above="@+id/btnCamera">

</ImageView>

最佳答案

首先尝试将捕获的位图转换为字节数组,例如:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();


然后,您可以将字节数组写入由OutputStream接收的socket.getOutputStream()

07-24 09:21