10 years, 3 months ago.

How to show the image on android mobile from LinkSprite JPEG Camera via Bluetooth

I used " LinkSprite JPEG Color Camera" to capture a image then transfer this image to the android mobile phone via Bluetooth. I was able to transfer the image data to the mobile phone but cant retrieve the image in android . I used these code to transfer image:

JPEGCamera::processPicture() {
    if (state == PROCESSING) {
        bt.printf("\n\nNew Image \n\n");
        if (address < imageSize) {
            char data[2048];
            int size = readData(data, min(sizeof(data), imageSize - address), address);
            int ret = fwrite(data, size, 1, fp);
            for (int i=0; i<size; i++) bt.printf("%x ",data[i]);
            if (ret > 0)
                address += size;
            if (ret == 0 || address >= imageSize) {
                stopPictures();
                fclose(fp);
                wait(0.1); // ????
                state = ret > 0 ? READY : ERROR;
            }
        }
    }

    return state == PROCESSING || state == READY;
}

The android code to receive

void beginListenForData()
    {
        final Handler handler = new Handler(); 
        final byte delimiter = 10; //This is the ASCII code for a newline character
        
        stopWorker = false;
        readBufferPosition = 0;
        readBuffer = new byte[1024];
        workerThread = new Thread(new Runnable()
        {
            public void run()
            {                
               while(!Thread.currentThread().isInterrupted() && !stopWorker)
               {
                    try 
                    {
                        int bytesAvailable = mmInputStream.available();                        
                        if(bytesAvailable > 0)
                        {
                            byte[] packetBytes = new byte[bytesAvailable];
                            mmInputStream.read(packetBytes);
                            for(int i=0;i<bytesAvailable;i++)
                            {
                                byte b = packetBytes[i];
                                if(b == delimiter)
                                {
                                    byte[] encodedBytes = new byte[readBufferPosition];
                                    System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                    final String data = new String(encodedBytes, "US-ASCII");
                                    readBufferPosition = 0;
                                    
                                    handler.post(new Runnable()
                                    {
                                        public void run()
                                        {
                                            myLabel.setText(data);
                                            Log.d("data",data);
                                        }
                                    });
                                }
                                else
                                {
                                    readBuffer[readBufferPosition++] = b;
                                }
                            }
                        }
                    } 
                    catch (IOException ex) 
                    {
                        stopWorker = true;
                    }
               }
            }
        });

        workerThread.start();
    }

Can anyone tell me how to show this image on android???

Be the first to answer this question.