Oh wise ones I seek your knowlege... Below is some legacy code I have for reading captions. Problem is some times it ends up reading in part of the image if there are no captions. It seems to only do this on certain types of jpegs. I've tried many things to detect the binary data. Can you help? I have tried JPEG::JFIF the only module I have found that can do it but it does not read captions from enough formats of jpegs for my needs.
binmode PHOTO_IN; #Set filemode to bin for good measure $/= "\x42\x49\x4D\x03"; #Input Record Delimeter. $cnt=0; #We want 1st of 2 records. while (<PHOTO_IN>) { #Read Photo... if ("$cnt"<1) { # Have Photo Header... $photo_header="$_"; # Save Photo Header... ++$cnt; # Bump count to skip photo. } } $photo_header =~ s/\x1C\x02/XYZZY/g; # Replace hex markers with text. $photo_header =~ s/[\n\r]/\x20/g; # Replace newlines with space. @fields=split(/XYZ/,$photo_header); # Set array using text marker. $n=2; # Start at 2 to skip junk. ITEM: while ( "$fields[$n]" ne "" ) {# Process all array elements. $fields[$n] =~ s/ZY\x78../Caption : /; # Set Caption Text Marker. if ($fields[$n] =~ /^Caption/) { # Save Caption Text for database. $caption=""; $caption=substr($fields[$n],10); #Apply the filters to the caption $caption =~ s/(\s*)$//g; $caption =~ s/'/''/g; $caption =~ s/\n//g; $caption =~ tr/a-zA-Z0-9\`\~\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\}\|\[\ +]\\\:\"\;\'\<\>\?\,\.\/ \t//cd; $caption =~ s/8BIM.*$//g; } $n++; }#endofwhile

In reply to Grabbing Jpeg Captions by devmage

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.