1

I am new one to development Android application. I have done a camera module, It can output a JPEG stream through Wi-Fi. Due to the file size is not fixed, the module out a total buffer through socket. The structure like this:

                     request cmd
                    --------------->
   Android phone      socketchannel        Camera 20K buffer
                    <---------------
                    response raw data

I set a ByteBuffer to receive JPEG raw data. I can see the JPEG-star TAG(0xff 0xd8) in ByteBuffer start, it display {-1, 40} and JPEG-end TAG {-1, -39} should in the ByteBuffer. I have write a test program by C language on x86 system, the raw data buffer contain at least one frame.

I use String method-indexOf() can not search the JPEG-start/end tag. Because String method only support ASCII 0x00~0x79, support function not support 0x80~0xFF. I also try Pattern/Matcher class but get the same results.

The JPEG raw data like below :

/* JPEG start */
ff d8 ff e1 01 22 45 78 69 66 00 00 49 49 2a 00
/* JPEG end */
43 ac 90 b8 62 3f 0a dd ca e7 9e a3 63 ff d9

As I write pure C language, there is memmem() function can search specific memory pattern in block memory. Does JAVA have the similar method to find extended-ascii in ByteBuffer ?

Below is my code use Pattern/Matcher to find extended ASCII pattern, but still failed:

public static final byte[] jpegEnd = new byte[]{(byte) 0xff, (byte)0xd9};
public static final byte[] jpegStart = {(byte)0xff, (byte)0xd8};
public ByteBuffer bjpegStart = ByteBuffer.allocate(10);
public ByteBuffer bjpegEnd = ByteBuffer.allocate(10);
public String sJpegStart;
public String sJpegEnd;
public String tempStr ;
public ByteBuffer inPut_buf = ByteBuffer.allocate(20500);
private Pattern pattern;
private Matcher matcher;
private int location;

        /*  initial JPEG-end ASCII string   */
    bjpegStart.put(jpegStart, 0, 2);
    try {
        sJpegStart = new String(bjpegStart.array(), "ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    bjpegEnd.put(jpegEnd, 0, 2);
    try {
        sJpegEnd = new String(bjpegEnd.array(), "ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ByteBuffer buf = ByteBuffer.allocate(10);
    buf.put((byte)outPut_cmd);
    buf.flip();
    Arrays.fill(inPut_buf.array(), 0, 20499, (byte) 0);
    SocketChannel socketChannel = SocketChannel.open();
    socketChannel.connect(new InetSocketAddress("192.168.0.1", TCP_SERVER_PORT));
    socketChannel.write(buf);
    if(outPut_cmd==49){
        inPut_buf.limit(20480);
        socketChannel.read(inPut_buf);
        try {
            tempStr = new String(inPut_buf.array(), "ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }

        pattern = Pattern.compile(sJpegEnd);
        matcher = pattern.matcher(tempStr);
        while(matcher.find()){
                location = matcher.start();
        break;
            }
    }
  • This does not have anything to do with ASCII or "extended ASCII" or any other character encoding - you just want to compare byte arrays. You should not try to store arbitrary binary data in a `String`. Your data is not ISO-8859-1 encoded character data. – Jesper Nov 01 '13 at 07:56
  • 1
    http://stackoverflow.com/questions/1507780/searching-for-a-sequence-of-bytes-in-a-binary-file-with-java – auselen Nov 01 '13 at 07:59
  • Dear auselen, thanks for your great help. I can search the JPEG buffer end. Regards. – swordfish_yang Nov 04 '13 at 08:31

0 Answers0