如何识别byte []的内容是jpeg? [英] How to identify contents of a byte[] is a jpeg?

查看:85
本文介绍了如何识别byte []的内容是jpeg?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小字节数组(25K以下),我接收并解码为更大的消息信封的一部分。有时这是一个图像,当它是图像,有时它是一个jpg。我没有除字节数组之外的上下文信息,并且需要识别它是否是图像,以及图像是否为jpg类型。

I have a small byte array (under 25K) that I receive and decode as part of a larger message envelope. Sometimes this is an image, and when it is and image, sometimes it is a jpg. I have no context information other than the byte array, and need to identify both if it IS an image, and if the image is of type jpg.

是否有一些神奇的数字或魔术字节存在于开头,结尾或某些偏移处,我可以看一下来识别它?

Is there some magic number, or magic bytes that exist at the beginning, end or at some offset that I can look at to identify it?

我的代码示例如下(来自内存,而不是c / p):

An example of my code looks like this (from memory, not c/p):

byte[] messageBytesAfterDecode = retrieveBytesFromEnvelope();
if(null != messageBytesAfterDecode && messageBytesAfterDecode > 0){
    if(areTheseBytesAJpeg(messageBytesAfterDecode)){
        doSomethingWithAJpeg(messageBytesAfterDecode)
    }else{
        flagEnvelopeAsHavingBadContentInTheField();
    }
}

我真的需要进入

areTheseBytesAJpeg(byte[] mBytes){}

方法,甚至指向详细说明的规范的指针。我希望有一个非常快速的方法来做出这个决定,因为我真的不想将它们读成图像等。

method, or even a pointer to a spec that details it. I'm hoping there is a very quick way to make this determination, since I don't really want to read them into an Image, etc.

推荐答案

来自维基百科:


JPEG图像文件以FF D8开头,以FF D9结束。

JPEG image files begin with FF D8 and end with FF D9.

http:// en.wikipedia.org/wiki/Magic_number_(programming)

这篇关于如何识别byte []的内容是jpeg?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆