确定blob在Java中是否有图像 [英] determine if blob has an image in java

查看:77
本文介绍了确定blob在Java中是否有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道而且我很偏执,这会被标记为

I know and i am paranoid that this would be tagged DUPLICATE

但是,我坚持无法解决自己的问题,因此需要您的帮助.

However i am stuck at something which i cannot resolve myself so i need your help.

基本上,我抽象了从Image(任意)读取前8个字节并根据其决定是否属于任何类型(PNG,JPEG,GIF)的概念.

Basically i abstracted concept of reading first 8 bytes from the Image(any) and depending on that decide if it falls under any of types(PNG,JPEG,GIF) .

我正在尝试通过 Java实现这一目标.

package examples;

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintStream;

import org.apache.commons.io.IOUtils;

public class BlobCheck
{
    public static void main(String args[]) throws Exception
    {
    File dir = new File(args[0]);
    File files[] = dir.listFiles();// Here this files will be changed to
                       // Blobs from database and then i will
                       // convert each blob to bytes.
    StringBuffer sb = new StringBuffer();
    StringBuilder chars = new StringBuilder();
    File afile[];
    int j = (afile = files).length;
    for (int i = 0; i < j; i++)
    {
        File file = afile[i];
        FileInputStream fis = new FileInputStream(file);
        byte bytearr[] = IOUtils.toByteArray(fis);
        long count = 0L;
        byte abyte0[];
        int l = (abyte0 = bytearr).length;
        for (int k = 0; k < l; k++)
        {
        byte b = abyte0[k];
        if (count == 8L)
            break;
        sb.append(b);
        chars.append((char) b);
        count++;
        }

        // if ("-1-40-1-320167470".equals(sb.toString()))
        /*
         * if ("-1-40-1".equals(sb.toString())) System.out.println((new
         * StringBuilder
         * (String.valueOf(file.getName()))).append(" is an image file ")
         * .append
         * (sb.toString()).append(" ").append(chars.toString()).toString());
         * else
         */
        System.out.println((new StringBuilder(String.valueOf(file.getName()))).append(" ").append(sb.toString()));
        sb.delete(0, sb.length());
        chars.delete(0, chars.length());
    }

    }
}

现在,我用一堆不同类型的文件(图像,文档,xls等)填充文件夹,然后执行该类,我得到以下输出.

Now,i fill a folder with bunch of different types of files (images,docs,xls,etc..) and excute the class i get the following output.

此处,前8个字节(十进制)的值与DUPLICATE(上图)中给出的值不同.令人惊讶的是,大多数图像具有相同的8个字节,而很少有(突出显示).

Here in this,the first 8 byte(decimal) values are different from what has been given in the DUPLICATE (above).Suprisingly most of the images are having same 8 bytes and few are not(highlighted).

输出:

  • 2.jpg -1-40-1-320167470
  • 2g.gif -1-40-1-320167470
  • 324.png -1-40-1-320167470
  • 4.jpg -1-40-1-320167470
  • 6.jpg -1-40-1-320167470
  • 9.jpg -1-40-1-320167470
  • Logo.jpg -1-40-1-1801465100
  • Lpng.png -1-40-1-1801465100
  • picture.xls -48-4917-32-95-7926-31
  • Thumbs.db -48-4917-32-95-7926-31
  • 2.jpg -1-40-1-320167470
  • 2g.gif -1-40-1-320167470
  • 324.png -1-40-1-320167470
  • 4.jpg -1-40-1-320167470
  • 6.jpg -1-40-1-320167470
  • 9.jpg -1-40-1-320167470
  • Logo.jpg -1-40-1-1801465100
  • Lpng.png -1-40-1-1801465100
  • picture.xls -48-4917-32-95-7926-31
  • Thumbs.db -48-4917-32-95-7926-31

如果我在某个地方出错了,请告诉我!谢谢.

Please let me know if i have gone wrong somewhere! Thanks.

推荐答案

也许您对文件名的扩展名感到困惑?

May be you are getting confused with extension of the filename?

尝试此操作,只需将* .png的名称更改为* .jpeg并使用任何图像编辑器/查看器打开;也不应抱怨格式未被识别.这可能是为什么即使扩展名不同您也获得相同的8个字节的原因.

Try this, just change the name of *.png into *.jpeg and open with any image editor/viewer; and it should not complaint about format not being recognized. This could be a reason about why you are getting same 8 bytes, even though the extension is different.

因为,我观察到的是,许多程序只要能够处理文件并在其窗口中显示,就不会抱怨更改图像文件扩展名.

Becuase, What I have observed that many program would not complaint about changing an image file extension, as long as they could process the file and show in their window.

请使用以下代码并发布输出:

Please use the below code and post the output:

import java.io.*;
import java.net.*;

public class ReadBytes {
    public static void main( String [] args ) throws IOException {

        URL url = new URL("http://your image url");

            // Read the image ...
        InputStream inputStream      = url.openStream();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte [] buffer               = new byte[ 1024 ];

        int n = 0;
        while (-1 != (n = inputStream.read(buffer))) {
           output.write(buffer, 0, n);
        }
        inputStream.close();

        // Here's the content of the image...
        byte [] data = output.toByteArray();

    // Write it to a file just to compare...
    OutputStream out = new FileOutputStream("data.png");
    out.write( data );
    out.close();

    // Print it to stdout 
        for( byte b : data ) {
            System.out.printf("0x%x ", b);
        }
    }
}

这篇关于确定blob在Java中是否有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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