I need to find the sequence of bytes in my image data. I have next code on java, but I need make the same in obj-c. Java:

private static int searchInBuffer(byte[] pBuf, int iBufferLen) {
    for(int i = 0; i<iBufferLen - 7; i++) {
        if (pBuf[i] == 'l' && pBuf[i + 1] == 'i' && pBuf[i + 2] == 'n' && pBuf[i + 3] == 'k')
        return (int)pBuf[i + 4];
    }
    return -1;
}

public static int checkFlagInJpeg(String pFullFileName) {
    int iRes = -1;
    try {
        File f = new File(pFullFileName);
        FileInputStream is = new FileInputStream(f);
        int iBufferSize = 6 * 1024, iCount = 15;
        byte buf[] = new byte[iBufferSize];

        while((is.available() > 0) && (iCount >= 0)) {
            int iRead = is.read(buf),
            iFlag = searchInBuffer(buf, iRead);
            if (iFlag > 0) {
                iRes = iFlag;
                break;
            }
            iCount--;
        }
        is.close();
    }
}

Obj-C (my version):

    UIImage *image = [UIImage imageWithCGImage:[[[self.assets objectAtIndex:indexPath.row] defaultRepresentation] fullScreenImage]];
    NSData *imageData = UIImageJPEGRepresentation(image, 1.0f);

    NSUInteger length = MIN(6*1024, [imageData length]);
    Byte *buffer = (Byte *)malloc(length);
    memcpy(buffer, [imageData bytes], length);

    for (int i=0; i < length - 1; i++) {
        if (buffer[i] == 'l' && buffer[i + 1] == 'i' && buffer[i + 2] == 'n' && buffer[i + 3] == 'k')
            NSLog(@"%c", buffer[i + 4]);
    }
    free(buffer);

I'm still not sure, that I understand all aspects of work with bytes, so I need a help.

UPDATE: The problem was in getting image data. With help of Martin R. I combine to solutions in one and get next working code:

ALAssetRepresentation *repr = [[self.assets objectAtIndex:indexPath.row] defaultRepresentation];
        NSUInteger size = (NSUInteger) repr.size;
        NSMutableData *data = [NSMutableData dataWithLength:size];

        NSError *error;
        [repr getBytes:data.mutableBytes fromOffset:0 length:size error:&error];

        NSData *pattern = [@"link" dataUsingEncoding:NSUTF8StringEncoding];
        NSRange range = [data rangeOfData:pattern options:0 range:NSMakeRange(0, data.length)];

        int iRes = -1;
        if (range.location != NSNotFound) {
            uint8_t flag;
            [data getBytes:&flag range:NSMakeRange(range.location + range.length, 1)];
            iRes = flag;
        }

        NSLog(@"%i", iRes);

It's working perfect! Thank you again!

有帮助吗?

解决方案

NSData has a method rangeOfData:... which you can use to find the pattern:

NSData *pattern = [@"link" dataUsingEncoding:NSUTF8StringEncoding];
NSRange range = [imageData rangeOfData:pattern options:0 range:NSMakeRange(0, imageData.length)];

If the pattern was found, get the next byte:

int iRes = -1;
if (range.location != NSNotFound && range.location + range.length < imageData.length) {
    uint8_t flag;
    [imageData getBytes:&flag range:NSMakeRange(range.location + range.length, 1)];
    iRes = flag;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top