位图通过Wi-Fi最佳尺寸

时间:2014-05-04 16:35:19

标签: android bitmap bytearray android-wifi

我正在开发一个应用程序,它可以将一个Android移动设备记录的相机帧和音频发送到另一个 使用UDP与DatagramPacket类 我设法发送录制的音频并在另一侧播放 我也设法通过

捕获相机帧
    Camera.PreviewCallback previewCalBac = new PreviewCallback() {

        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            if (data != null) {
//              mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                Log.d("CAMERA", "CHANGED" + data.length);
                startStreamingVideo(data);
            }
        }
    };

并且发送操作就像这样

private void startStreamingVideo(byte[] data) {
        Log.d(VIDEO_CLIENT_TAG, "Starting the video stream");
        if(data!=null){
        currentlySendingVideo = true;
        startVStreaming(data);}
        else{
            Log.d(VIDEO_CLIENT_TAG, "NULL DATA");
        }
    }

    private void startVStreaming(final byte[] data) {
        Log.d(VIDEO_CLIENT_TAG,
                "Starting the background thread to stream the video data");
        Thread streamThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.d(VIDEO_CLIENT_TAG, "Creating the datagram socket");
                    DatagramSocket socket = new DatagramSocket();
                    Log.d(VIDEO_CLIENT_TAG, "Creating the buffer of size "+ data.length);
                    byte[] buffer = new byte[4096];
                    Log.d(VIDEO_CLIENT_TAG, "Connecting to "+ ipAddress.getText().toString() + ":" + VPORT);
                    final InetAddress serverAddress = InetAddress.getByName(ipAddress.getText().toString());
                    Log.d(VIDEO_CLIENT_TAG, "Connected to "+ ipAddress.getText().toString() + ":" + VPORT);
                    Log.d(VIDEO_CLIENT_TAG,"Creating the reuseable DatagramPacket");
                    DatagramPacket packet;
                    Log.d(VIDEO_CLIENT_TAG, "Creating the VideoRecord");
//                  recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,RECORDING_RATE, CHANNEL, FORMAT, data.length);
//                  Log.d(VIDEO_CLIENT_TAG, "VideoRecord recording...");
//                  recorder.startRecording();
                    while (currentlySendingVideo == true) {
                        // read the data into the buffer
                        Log.d(VIDEO_CLIENT_TAG, "Here0");
//                      int read = recorder.read(data, 0, data.length);
                        Log.d(VIDEO_CLIENT_TAG, "Here");
                        // place contents of buffer into the packet
                        packet = new DatagramPacket(data, 4096,serverAddress, VPORT);
                        Log.d(VIDEO_CLIENT_TAG, "Here1");
                        // send the packet
                        socket.send(packet);
                        Log.d(VIDEO_CLIENT_TAG, "Here2");
                    }
                    Log.d(VIDEO_CLIENT_TAG, "VideoRecord finished recording");
                } catch (Exception e) {
                    Log.e(VIDEO_CLIENT_TAG, "HERE Exception: " + e);
                }
            }
        });
        // start the thread
        streamThread.start();
    }

服务器端我可以知道我正在接收数据包 问题是位图图像byte []太大而无法发送 我需要将其缩放到最佳大小,以便我可以传输它并以最少的浪费字节接收它。 解决这个问题的任何方法?

1 个答案:

答案 0 :(得分:0)

不是UDP,不是。您需要在UDP之上的协议来处理> 64K的消息。别无选择。