Java 在C+中解码Android CameraPreview字节+;服务器

Java 在C+中解码Android CameraPreview字节+;服务器,java,android,c++,sockets,opencv,Java,Android,C++,Sockets,Opencv,我正在使用此代码从我的Android手机获取CameraPreview- private Camera.PreviewCallback mPreviewCallback = new PreviewCallback() { @Override public void onPreviewFrame(byte[] data, Camera camera) { synchronized (mQueue) { if (mQueue.size() ==

我正在使用此代码从我的Android手机获取
CameraPreview
-

private Camera.PreviewCallback mPreviewCallback = new PreviewCallback() {
    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        synchronized (mQueue) {
            if (mQueue.size() == MAX_BUFFER) {
                mQueue.poll();
            }
            mQueue.add(data);
        }
    }
};
<> >将其发送到我的C++服务器,通过<代码>套接字< /代码> -/p>
mSocket = new Socket();
mSocket.connect(new InetSocketAddress(mIP, mPort), 10000);
BufferedOutputStream outputStream = new BufferedOutputStream(mSocket.getOutputStream());
BufferedInputStream inputStream = new BufferedInputStream(mSocket.getInputStream());
outputStream.write(mCameraPreview.getImageBuffer());
outputStream.flush();
我从网站上获得了这些代码,但服务器(我的Windows PC)是用
C++
编码的,而在网站上,服务器代码是用
Java
编码的。我想解码
CameraPreview
字节,并在我的OpenCV窗口中显示它们。我可以接收字节,也可以显示它们,但我需要帮助将字节转换为图像。有人能帮我吗

对于那些不想访问该站点获取服务器Java代码的人,下面是代码-

在服务器端,使用缓冲区队列缓存传入数据:

public int-fillBuffer(字节[]数据,int-off,int-len,LinkedList-YUVQueue){
mTotalLength+=len;
MByteArrayOutstream.write(数据、关闭、len);
if(mTotalLength==mFrameLength){
已同步(YUVQueue){
添加(mByteArrayOutputStream.toByteArray());
mByteArrayOutputStream.reset();
}
mTotalLength=0;
System.out.println(“接收文件”);
}
返回0;
}
关于StackOverflow,我们可以使用以下代码来解码NV21数据:

public static int[]convertYUVtoRGB(字节[]yuv,int-width,int-height)
抛出NullPointerException,IllegalArgumentException{
int[]out=新int[宽度*高度];
int sz=宽度*高度;
int i,j;
int Y,Cr=0,Cb=0;
对于(j=0;j>1;
对于(i=0;i>1)*2;
Cb=yuv[cOff];
if(Cb<0)
Cb+=127;
其他的
Cb-=128;
Cr=yuv[cOff+1];
if(Cr<0)
Cr+=127;
其他的
Cr-=128;
}
int R=Y+Cr+(Cr>>2)+(Cr>>3)+(Cr>>5);
if(R<0)
R=0;
如果(R>255),则为else
R=255;
int G=Y-(Cb>>2)+(Cb>>4)+(Cb>>5)-(Cr>>1)
+(Cr>>3)+(Cr>>4)+(Cr>>5);
if(G<0)
G=0;
如果(G>255),则为else
G=255;
intb=Y+Cb+(Cb>>1)+(Cb>>2)+(Cb>>6);
if(B<0)
B=0;
如果(B>255),则为else
B=255;
输出[pixPtr++]=0xff000000+(B
public int fillBuffer(byte[] data, int off, int len, LinkedList<byte[]> YUVQueue) {
    mTotalLength += len;
    mByteArrayOutputStream.write(data, off, len);
    if (mTotalLength == mFrameLength) {
        synchronized (YUVQueue) {
            YUVQueue.add(mByteArrayOutputStream.toByteArray());
            mByteArrayOutputStream.reset();
        }
        mTotalLength = 0;         
        System.out.println("received file");
    }
    return 0;
}
public static int[] convertYUVtoRGB(byte[] yuv, int width, int height)
        throws NullPointerException, IllegalArgumentException {        
    int[] out = new int[width * height];
    int sz = width * height;

    int i, j;
    int Y, Cr = 0, Cb = 0;
    for (j = 0; j < height; j++) {
        int pixPtr = j * width;
        final int jDiv2 = j >> 1;
        for (i = 0; i < width; i++) {
            Y = yuv[pixPtr];
            if (Y < 0)
                Y += 255;
            if ((i & 0x1) != 1) {
                final int cOff = sz + jDiv2 * width + (i >> 1) * 2;
                Cb = yuv[cOff];
                if (Cb < 0)
                    Cb += 127;
                else
                    Cb -= 128;
                Cr = yuv[cOff + 1];
                if (Cr < 0)
                    Cr += 127;
                else
                    Cr -= 128;
            }
            int R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
            if (R < 0)
                R = 0;
            else if (R > 255)
                R = 255;
            int G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1)
                    + (Cr >> 3) + (Cr >> 4) + (Cr >> 5);
            if (G < 0)
                G = 0;
            else if (G > 255)
                G = 255;
            int B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
            if (B < 0)
                B = 0;
            else if (B > 255)
                B = 255;
            out[pixPtr++] = 0xff000000 + (B << 16) + (G << 8) + R;
        }
    }
    return out;
}