- 浏览: 29198 次
- 性别:
- 来自: 南京
最新评论
-
blueprint86:
字体颜色: [color=red]文字 提示:您可以使用 c ...
使用UltraEdit中删除文档中的空行 -
cumtlu:
做事不如做人,职场深似海
Eclipse中查找接口实现类快捷键 -
bapl:
选中接口名按ctrl+t 会列出所有实现此接口的类
Eclipse中查找接口实现类快捷键 -
hope_crazy:
靠。。什么玩意
Eclipse中查找接口实现类快捷键 -
a123456603:
看的头晕...............
Java判断文件是否为图片文件GIF,PNG,JPG ImageInfo类
首先大家需要了解的是Java里判断Image文件信息格式(GIF/PNG/JPG)/Size/Height/Width的原理
1,判断Image格式
用UE打开GIF/PNG/JPG格式的图片文件
我们会发现在文件头部某几个位置的字节的值连起来是'GIF'/'PNG'/'JFIF'
它们的位置分别如下:
GIF: 012
JFIF(JPG): 6789
PNG: 123
这样我们可以通过判断这几个字节值来得到Image文件格式:
String type = "";
byte b0 = image.getFileData()[0];
byte b1 = image.getFileData()[1];
byte b2 = image.getFileData()[2];
byte b3 = image.getFileData()[3];
byte b6 = image.getFileData()[6];
byte b7 = image.getFileData()[7];
byte b8 = image.getFileData()[8];
byte b9 = image.getFileData()[9];
// GIF
if (b0 == (byte) 'G' && b1 == (byte) 'I' && b2 == (byte) 'F')
type = "GIF";
// PNG
else if (b1 == (byte) 'P' && b2 == (byte) 'N' && b3 == (byte) 'G')
type = "PNG";
// JPG
else if (b6 == (byte) 'J' && b7 == (byte) 'F' && b8 == (byte) 'I' && b9 == (byte) 'F')
type = "JPG";
else
type = "Unknown";
image.setType(type);
2,判断Image大小
FileImageInputStream fiis = new FileImageInputStream(new File(image.getPath()));
image.setSize((float) fii.length() / 1000 + "KB");
3,判断Image宽度和高度
ImageIcon ii = new ImageIcon(image.getPath());
image.setHeight(String.valueOf(ii.getIconHeight()));
image.setWidth(String.valueOf(ii.getIconWidth()));
下面是判断文件是否为图片文件的一个JAVA程序
import java.io.*;
public class isImg
{
public static void main(String[] args)
{
if (!isImage("d://1.txt"))
{
System.out.println("不是图片");
}
else
{
System.out.println("是图片");
}
}
/**
* 判断文件是否为图片文件(GIF,PNG,JPG)
* @param srcFileName
* @return
*/
public static boolean isImage(String srcFileName) {
FileInputStream imgFile = null;
byte[] b = new byte[10];
int l = -1;
try {
imgFile = new FileInputStream(srcFileName);
l = imgFile.read(b);
imgFile.close();
} catch (Exception e) {
return false;
}
if (l == 10) {
byte b0 = b[0];
byte b1 = b[1];
byte b2 = b[2];
byte b3 = b[3];
byte b6 = b[6];
byte b7 = b[7];
byte b8 = b[8];
byte b9 = b[9];
if (b0 == (byte) 'G' && b1 == (byte) 'I' && b2 == (byte) 'F') {
return true;
} else if (b1 == (byte) 'P' && b2 == (byte) 'N' && b3 == (byte) 'G') {
return true;
} else if (b6 == (byte) 'J' && b7 == (byte) 'F' && b8 == (byte) 'I'&& b9 == (byte) 'F') {
return true;
} else {
return false;
}
} else {
return false;
}
}
}
应用: 检测客户端是否上传了合法的图片文件,防止恶意修改扩展名上传文件.
一下是通过是否能读到图像文件来判断的,其原理也是读取文件头信息来判断.
Test.java
import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
import java.util.*;
import javax.imageio.stream.*;
public class Test{
public static void main(String[] args) {
File f = new File("c://test.jpg");
if (f.exists()) {
System.out.println(getFormatInFile(f));
}
}
// Returns the format of the image in the file 'f'.
// Returns null if the format is not known.public static String getFormatInFile(File f) {
return getFormatName(f);
}
// Returns the format name of the image in the object 'o'.
// Returns null if the format is not known.
private static String getFormatName(Object o) {
try {
// Create an image input stream on the image
ImageInputStream iis = ImageIO.createImageInputStream(o);
// Find all image readers that recognize the image format
Iterator iter = ImageIO.getImageReaders(iis);
if (!iter.hasNext()) {
// No readers found
return null;
}
// Use the first reader
ImageReader reader = (ImageReader)iter.next();
// Close stream
iis.close();
// Return the format name
return reader.getFormatName();
} catch (IOException e) {
//
}
// The image could not be read
return null;
}
}
ImageInfo.java
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;
public class ImageInfo
{
public ImageInfo()
{
collectComments = true;
}
private void addComment(String s)
{
if(comments == null)
comments = new Vector();
comments.addElement(s);
}
public boolean check()
{
format = -1;
width = -1;
height = -1;
bitsPerPixel = -1;
numberOfImages = 1;
physicalHeightDpi = -1;
physicalWidthDpi = -1;
comments = null;
int b1;
int b2;
try{
b1 = read() & 0xff;
b2 = read() & 0xff;
if(b1 == 71 && b2 == 73)
return checkGif();
if(b1 == 137 && b2 == 80)
return checkPng();
if(b1 == 255 && b2 == 216)
return checkJpeg();
if(b1 == 66 && b2 == 77)
return checkBmp();
if(b1 == 10 && b2 < 6)
return checkPcx();
if(b1 == 70 && b2 == 79)
return checkIff();
if(b1 == 89 && b2 == 166)
return checkRas();
if(b1 == 80 && b2 >= 49 && b2 <= 54)
return checkPnm(b2 - 48);
if(b1 == 56 && b2 == 66)
return checkPsd();
}catch(IOException ioe){
return false;
}
return false;
}
private boolean checkBmp()
throws IOException
{
byte a[] = new byte[44];
if(read(a) != a.length)
return false;
width = getIntLittleEndian(a, 16);
height = getIntLittleEndian(a, 20);
if(width < 1 || height < 1)
return false;
bitsPerPixel = getShortLittleEndian(a, 26);
if(bitsPerPixel != 1 && bitsPerPixel != 4 && bitsPerPixel != 8 && bitsPerPixel != 16 && bitsPerPixel != 24 && bitsPerPixel != 32)
return false;
int x = (int)((double)getIntLittleEndian(a, 36) * 0.025399999999999999D);
if(x > 0)
setPhysicalWidthDpi(x);
int y = (int)((double)getIntLittleEndian(a, 40) * 0.025399999999999999D);
if(y > 0)
setPhysicalHeightDpi(y);
format = 3;
return true;
}
private boolean checkGif()
throws IOException
{
byte GIF_MAGIC_87A[] = {
70, 56, 55, 97
};
byte GIF_MAGIC_89A[] = {
70, 56, 57, 97
};
byte a[] = new byte[11];
if(read(a) != 11)
return false;
if(!equals(a, 0, GIF_MAGIC_89A, 0, 4) && !equals(a, 0, GIF_MAGIC_87A, 0, 4))
return false;
format = 1;
width = getShortLittleEndian(a, 4);
height = getShortLittleEndian(a, 6);
int flags = a[8] & 0xff;
bitsPerPixel = (flags >> 4 & 7) + 1;
if(!determineNumberOfImages)
return true;
if((flags & 0x80) != 0)
{
int tableSize = (1 << (flags & 7) + 1) * 3;
skip(tableSize);
}
numberOfImages = 0;
int blockType;
do
{
blockType = read();
switch(blockType)
{
case 44: // ','
{
if(read(a, 0, 9) != 9)
return false;
flags = a[8] & 0xff;
progressive = (flags & 0x40) != 0;
int localBitsPerPixel = (flags & 7) + 1;
if(localBitsPerPixel > bitsPerPixel)
bitsPerPixel = localBitsPerPixel;
if((flags & 0x80) != 0)
skip((1 << localBitsPerPixel) * 3);
skip(1);
int n;
do
{
n = read();
if(n > 0)
skip(n);
else
if(n == -1)
return false;
} while(n > 0);
numberOfImages++;
break;
}
case 33: // '!'
{
int extensionType = read();
int n;
if(collectComments && extensionType == 254)
{
StringBuffer sb = new StringBuffer();
do
{
n = read();
if(n == -1)
return false;
if(n > 0)
{
for(int i = 0; i < n; i++)
{
int ch = read();
if(ch == -1)
return false;
sb.append((char)ch);
}
}
} while(n > 0);
} else
{
do
{
n = read();
if(n > 0)
skip(n);
else
if(n == -1)
return false;
} while(n > 0);
}
break;
}
default:
{
return false;
}
case 59: // ';'
break;
}
} while(blockType != 59);
return true;
}
private boolean checkIff()
throws IOException
{
byte a[] = new byte[10];
if(read(a, 0, 10) != 10)
return false;
byte IFF_RM[] = {
82, 77
};
if(!equals(a, 0, IFF_RM, 0, 2))
return false;
int type = getIntBigEndian(a, 6);
if(type != 0x494c424d && type != 0x50424d20)
return false;
do
{
if(read(a, 0, !=
return false;
int chunkId = getIntBigEndian(a, 0);
int size = getIntBigEndian(a, 4);
if((size & 1) == 1)
size++;
if(chunkId == 0x424d4844)
if(read(a, 0, 9) != 9)
{
return false;
} else
{
format = 5;
width = getShortBigEndian(a, 0);
height = getShortBigEndian(a, 2);
bitsPerPixel = a[8] & 0xff;
return width > 0 && height > 0 && bitsPerPixel > 0 && bitsPerPixel < 33;
}
skip(size);
} while(true);
}
private boolean checkJpeg()
throws IOException
{
byte data[] = new byte[12];
do
{
if(read(data, 0, 4) != 4)
return false;
int marker = getShortBigEndian(data, 0);
int size = getShortBigEndian(data, 2);
if((marker & 0xff00) != 65280)
return false;
if(marker == 65504)
{
if(size < 14)
{
skip(size - 2);
} else
{
if(read(data, 0, 12) != 12)
return false;
byte APP0_ID[] = {
74, 70, 73, 70, 0
};
if(equals(APP0_ID, 0, data, 0, 5))
if(data[7] == 1)
{
setPhysicalWidthDpi(getShortBigEndian(data,);
setPhysicalHeightDpi(getShortBigEndian(data, 10));
} else
if(data[7] == 2)
{
int x = getShortBigEndian(data,;
int y = getShortBigEndian(data, 10);
setPhysicalWidthDpi((int)((float)x * 2.54F));
setPhysicalHeightDpi((int)((float)y * 2.54F));
}
skip(size - 14);
}
} else
if(collectComments && size > 2 && marker == 65534)
{
byte chars[] = new byte[size -= 2];
if(read(chars, 0, size) != size)
return false;
String comment = new String(chars, "iso-8859-1");
comment = comment.trim();
addComment(comment);
} else
{
if(marker >= 65472 && marker <= 65487 && marker != 65476 && marker != 65480)
if(read(data, 0, 6) != 6)
{
return false;
} else
{
format = 0;
bitsPerPixel = (data[0] & 0xff) * (data[5] & 0xff);
progressive = marker == 65474 || marker == 65478 || marker == 65482 || marker == 65486;
width = getShortBigEndian(data, 3);
height = getShortBigEndian(data, 1);
return true;
}
skip(size - 2);
}
} while(true);
}
private boolean checkPcx()
throws IOException
{
byte a[] = new byte[64];
if(read(a) != a.length)
return false;
if(a[0] != 1)
return false;
int x1 = getShortLittleEndian(a, 2);
int y1 = getShortLittleEndian(a, 4);
int x2 = getShortLittleEndian(a, 6);
int y2 = getShortLittleEndian(a,;
if(x1 < 0 || x2 < x1 || y1 < 0 || y2 < y1)
return false;
width = (x2 - x1) + 1;
height = (y2 - y1) + 1;
int bits = a[1];
int planes = a[63];
if(planes == 1 && (bits == 1 || bits == 2 || bits == 4 || bits ==)
bitsPerPixel = bits;
else
if(planes == 3 && bits ==
bitsPerPixel = 24;
else
return false;
setPhysicalWidthDpi(getShortLittleEndian(a, 10));
setPhysicalHeightDpi(getShortLittleEndian(a, 10));
format = 4;
return true;
}
private boolean checkPng()
throws IOException
{
byte PNG_MAGIC[] = {
78, 71, 13, 10, 26, 10
};
byte a[] = new byte[27];
if(read(a) != 27)
return false;
if(!equals(a, 0, PNG_MAGIC, 0, 6))
return false;
format = 2;
width = getIntBigEndian(a, 14);
height = getIntBigEndian(a, 18);
bitsPerPixel = a[22] & 0xff;
int colorType = a[23] & 0xff;
if(colorType == 2 || colorType == 6)
bitsPerPixel *= 3;
progressive = (a[26] & 0xff) != 0;
return true;
}
private boolean checkPnm(int id)
throws IOException
{
if(id < 1 || id > 6)
return false;
int PNM_FORMATS[] = {
7, 8, 9
};
format = PNM_FORMATS[(id - 1) % 3];
boolean hasPixelResolution = false;
String s;
do
{
do
{
s = readLine();
if(s != null)
s = s.trim();
} while(s == null || s.length() < 1);
if(s.charAt(0) == '#')
{
if(collectComments && s.length() > 1)
addComment(s.substring(1));
continue;
}
if(hasPixelResolution)
break;
int spaceIndex = s.indexOf(' ');
if(spaceIndex == -1)
return false;
String widthString = s.substring(0, spaceIndex);
spaceIndex = s.lastIndexOf(' ');
if(spaceIndex == -1)
return false;
String heightString = s.substring(spaceIndex + 1);
try
{
width = Integer.parseInt(widthString);
height = Integer.parseInt(heightString);
}
catch(NumberFormatException nfe)
{
return false;
}
if(width < 1 || height < 1)
return false;
if(format == 7)
{
bitsPerPixel = 1;
return true;
}
hasPixelResolution = true;
} while(true);
int maxSample;
try
{
maxSample = Integer.parseInt(s);
}
catch(NumberFormatException nfe)
{
return false;
}
if(maxSample < 0)
return false;
for(int i = 0; i < 25; i++)
if(maxSample < 1 << i + 1)
{
bitsPerPixel = i + 1;
if(format == 9)
bitsPerPixel *= 3;
return true;
}
return false;
}
private boolean checkPsd()
throws IOException
{
byte a[] = new byte[24];
if(read(a) != a.length)
return false;
byte PSD_MAGIC[] = {
80, 83
};
if(!equals(a, 0, PSD_MAGIC, 0, 2))
{
return false;
} else
{
format = 10;
width = getIntBigEndian(a, 16);
height = getIntBigEndian(a, 12);
int channels = getShortBigEndian(a, 10);
int depth = getShortBigEndian(a, 20);
bitsPerPixel = channels * depth;
return width > 0 && height > 0 && bitsPerPixel > 0 && bitsPerPixel <= 64;
}
}
private boolean checkRas()
throws IOException
{
byte a[] = new byte[14];
if(read(a) != a.length)
return false;
byte RAS_MAGIC[] = {
106, -107
};
if(!equals(a, 0, RAS_MAGIC, 0, 2))
{
return false;
} else
{
format = 6;
width = getIntBigEndian(a, 2);
height = getIntBigEndian(a, 6);
bitsPerPixel = getIntBigEndian(a, 10);
return width > 0 && height > 0 && bitsPerPixel > 0 && bitsPerPixel <= 24;
}
}
private static boolean determineVerbosity(String args[])
{
if(args != null && args.length > 0)
{
for(int i = 0; i < args.length; i++)
if("-c".equals(args[i]))
return false;
}
return true;
}
private static boolean equals(byte a1[], int offs1, byte a2[], int offs2, int num)
{
while(num-- > 0)
if(a1[offs1++] != a2[offs2++])
return false;
return true;
}
public int getBitsPerPixel()
{
return bitsPerPixel;
}
public String getComment(int index)
{
if(comments == null || index < 0 || index >= comments.size())
throw new IllegalArgumentException("Not a valid comment index: " + index);
else
return (String)comments.elementAt(index);
}
public int getFormat()
{
return format;
}
public String getFormatName()
{
if(format >= 0 && format < FORMAT_NAMES.length)
return FORMAT_NAMES[format];
else
return "?";
}
public int getHeight()
{
return height;
}
private static int getIntBigEndian(byte a[], int offs)
{
return (a[offs] & 0xff) << 24 | (a[offs + 1] & 0xff) << 16 | (a[offs + 2] & 0xff) << 8 | a[offs + 3] & 0xff;
}
private static int getIntLittleEndian(byte a[], int offs)
{
return (a[offs + 3] & 0xff) << 24 | (a[offs + 2] & 0xff) << 16 | (a[offs + 1] & 0xff) << 8 | a[offs] & 0xff;
}
public String getMimeType()
{
if(format >= 0 && format < MIME_TYPE_STRINGS.length)
{
if(format == 0 && progressive)
return "image/pjpeg";
else
return MIME_TYPE_STRINGS[format];
} else
{
return null;
}
}
public int getNumberOfComments()
{
if(comments == null)
return 0;
else
return comments.size();
}
public int getNumberOfImages()
{
return numberOfImages;
}
public int getPhysicalHeightDpi()
{
return physicalHeightDpi;
}
public float getPhysicalHeightInch()
{
int h = getHeight();
int ph = getPhysicalHeightDpi();
if(h > 0 && ph > 0)
return (float)h / (float)ph;
else
return -1F;
}
public int getPhysicalWidthDpi()
{
return physicalWidthDpi;
}
public float getPhysicalWidthInch()
{
int w = getWidth();
int pw = getPhysicalWidthDpi();
if(w > 0 && pw > 0)
return (float)w / (float)pw;
else
return -1F;
}
private static int getShortBigEndian(byte a[], int offs)
{
return (a[offs] & 0xff) << 8 | a[offs + 1] & 0xff;
}
private static int getShortLittleEndian(byte a[], int offs)
{
return a[offs] & 0xff | (a[offs + 1] & 0xff) << 8;
}
public int getWidth()
{
return width;
}
public boolean isProgressive()
{
return progressive;
}
public static void main(String args[])
{
ImageInfo imageInfo = new ImageInfo();
imageInfo.setDetermineImageNumber(true);
boolean verbose = determineVerbosity(args);
if(args.length == 0)
{
run(null, System.in, imageInfo, verbose);
} else
{
int index = 0;
do
{
if(index >= args.length)
break;
InputStream in = null;
try
{
String name = args[index++];
System.out.print(name + ";");
if(name.startsWith("http://"))
in = (new URL(name)).openConnection().getInputStream();
else
in = new FileInputStream(name);
run(name, in, imageInfo, verbose);
in.close();
}
catch(IOException e)
{
System.out.println(e);
try
{
if(in != null)
in.close();
}
catch(IOException ee) { }
}
} while(true);
}
}
private static void print(String sourceName, ImageInfo ii, boolean verbose)
{
if(verbose)
printVerbose(sourceName, ii);
else
printCompact(sourceName, ii);
}
private static void printCompact(String sourceName, ImageInfo imageInfo)
{
String SEP = "\t";
System.out.println(sourceName + "\t" + imageInfo.getFormatName() + "\t" + imageInfo.getMimeType() + "\t" + imageInfo.getWidth() + "\t" + imageInfo.getHeight() + "\t" + imageInfo.getBitsPerPixel() + "\t" + imageInfo.getNumberOfImages() + "\t" + imageInfo.getPhysicalWidthDpi() + "\t" + imageInfo.getPhysicalHeightDpi() + "\t" + imageInfo.getPhysicalWidthInch() + "\t" + imageInfo.getPhysicalHeightInch() + "\t" + imageInfo.isProgressive());
}
private static void printLine(int indentLevels, String text, float value, float minValidValue)
{
if(value < minValidValue)
{
return;
} else
{
printLine(indentLevels, text, Float.toString(value));
return;
}
}
private static void printLine(int indentLevels, String text, int value, int minValidValue)
{
if(value >= minValidValue)
printLine(indentLevels, text, Integer.toString(value));
}
private static void printLine(int indentLevels, String text, String value)
{
if(value == null || value.length() == 0)
return;
while(indentLevels-- > 0)
System.out.print("\t");
if(text != null && text.length() > 0)
{
System.out.print(text);
System.out.print(" ");
}
System.out.println(value);
}
private static void printVerbose(String sourceName, ImageInfo ii)
{
printLine(0, null, sourceName);
printLine(1, "File format: ", ii.getFormatName());
printLine(1, "MIME type: ", ii.getMimeType());
printLine(1, "Width (pixels): ", ii.getWidth(), 1);
printLine(1, "Height (pixels): ", ii.getHeight(), 1);
printLine(1, "Bits per pixel: ", ii.getBitsPerPixel(), 1);
printLine(1, "Progressive: ", ii.isProgressive() ? "yes" : "no");
printLine(1, "Number of images: ", ii.getNumberOfImages(), 1);
printLine(1, "Physical width (dpi): ", ii.getPhysicalWidthDpi(), 1);
printLine(1, "Physical height (dpi): ", ii.getPhysicalHeightDpi(), 1);
printLine(1, "Physical width (inches): ", ii.getPhysicalWidthInch(), 1.0F);
printLine(1, "Physical height (inches): ", ii.getPhysicalHeightInch(), 1.0F);
int numComments = ii.getNumberOfComments();
printLine(1, "Number of textual comments: ", numComments, 1);
if(numComments > 0)
{
for(int i = 0; i < numComments; i++)
printLine(2, null, ii.getComment(i));
}
}
private int read()
throws IOException
{
if(in != null)
return in.read();
else
return din.readByte();
}
private int read(byte a[])
throws IOException
{
if(in != null)
{
return in.read(a);
} else
{
din.readFully(a);
return a.length;
}
}
private int read(byte a[], int offset, int num)
throws IOException
{
if(in != null)
{
return in.read(a, offset, num);
} else
{
din.readFully(a, offset, num);
return num;
}
}
private String readLine()
throws IOException
{
return readLine(new StringBuffer());
}
private String readLine(StringBuffer sb)
throws IOException
{
boolean finished;
do
{
int value = read();
finished = value == -1 || value == 10;
if(!finished)
sb.append((char)value);
} while(!finished);
return sb.toString();
}
private static void run(String sourceName, InputStream in, ImageInfo imageInfo, boolean verbose)
{
imageInfo.setInput(in);
imageInfo.setDetermineImageNumber(true);
imageInfo.setCollectComments(verbose);
if(imageInfo.check())
print(sourceName, imageInfo, verbose);
}
public void setCollectComments(boolean newValue)
{
collectComments = newValue;
}
public void setDetermineImageNumber(boolean newValue)
{
determineNumberOfImages = newValue;
}
public void setInput(DataInput dataInput)
{
din = dataInput;
in = null;
}
public void setInput(InputStream inputStream)
{
in = inputStream;
din = null;
}
private void setPhysicalHeightDpi(int newValue)
{
physicalWidthDpi = newValue;
}
private void setPhysicalWidthDpi(int newValue)
{
physicalHeightDpi = newValue;
}
private void skip(int num)
throws IOException
{
while(num > 0)
{
long result;
if(in != null)
result = in.skip(num);
else
result = din.skipBytes(num);
if(result > 0L)
{
num = (int)((long)num - result);
} else
{
if(in != null)
result = in.read();
else
result = din.readByte();
if(result == -1L)
throw new IOException("Premature end of input.");
num--;
}
}
}
public static final int FORMAT_JPEG = 0;
public static final int FORMAT_GIF = 1;
public static final int FORMAT_PNG = 2;
public static final int FORMAT_BMP = 3;
public static final int FORMAT_PCX = 4;
public static final int FORMAT_IFF = 5;
public static final int FORMAT_RAS = 6;
public static final int FORMAT_PBM = 7;
public static final int FORMAT_PGM = 8;
public static final int FORMAT_PPM = 9;
public static final int FORMAT_PSD = 10;
private static final String FORMAT_NAMES[] = {
"JPEG", "GIF", "PNG", "BMP", "PCX", "IFF", "RAS", "PBM", "PGM", "PPM",
"PSD"
};
private static final String MIME_TYPE_STRINGS[] = {
"image/jpeg", "image/gif", "image/png", "image/bmp", "image/pcx", "image/iff", "image/ras", "image/x-portable-bitmap", "image/x-portable-graymap", "image/x-portable-pixmap",
"image/psd"
};
private int width;
private int height;
private int bitsPerPixel;
private boolean progressive;
private int format;
private InputStream in;
private DataInput din;
private boolean collectComments;
private Vector comments;
private boolean determineNumberOfImages;
private int numberOfImages;
private int physicalHeightDpi;
private int physicalWidthDpi;
}
1,判断Image格式
用UE打开GIF/PNG/JPG格式的图片文件
我们会发现在文件头部某几个位置的字节的值连起来是'GIF'/'PNG'/'JFIF'
它们的位置分别如下:
GIF: 012
JFIF(JPG): 6789
PNG: 123
这样我们可以通过判断这几个字节值来得到Image文件格式:
String type = "";
byte b0 = image.getFileData()[0];
byte b1 = image.getFileData()[1];
byte b2 = image.getFileData()[2];
byte b3 = image.getFileData()[3];
byte b6 = image.getFileData()[6];
byte b7 = image.getFileData()[7];
byte b8 = image.getFileData()[8];
byte b9 = image.getFileData()[9];
// GIF
if (b0 == (byte) 'G' && b1 == (byte) 'I' && b2 == (byte) 'F')
type = "GIF";
// PNG
else if (b1 == (byte) 'P' && b2 == (byte) 'N' && b3 == (byte) 'G')
type = "PNG";
// JPG
else if (b6 == (byte) 'J' && b7 == (byte) 'F' && b8 == (byte) 'I' && b9 == (byte) 'F')
type = "JPG";
else
type = "Unknown";
image.setType(type);
2,判断Image大小
FileImageInputStream fiis = new FileImageInputStream(new File(image.getPath()));
image.setSize((float) fii.length() / 1000 + "KB");
3,判断Image宽度和高度
ImageIcon ii = new ImageIcon(image.getPath());
image.setHeight(String.valueOf(ii.getIconHeight()));
image.setWidth(String.valueOf(ii.getIconWidth()));
下面是判断文件是否为图片文件的一个JAVA程序
import java.io.*;
public class isImg
{
public static void main(String[] args)
{
if (!isImage("d://1.txt"))
{
System.out.println("不是图片");
}
else
{
System.out.println("是图片");
}
}
/**
* 判断文件是否为图片文件(GIF,PNG,JPG)
* @param srcFileName
* @return
*/
public static boolean isImage(String srcFileName) {
FileInputStream imgFile = null;
byte[] b = new byte[10];
int l = -1;
try {
imgFile = new FileInputStream(srcFileName);
l = imgFile.read(b);
imgFile.close();
} catch (Exception e) {
return false;
}
if (l == 10) {
byte b0 = b[0];
byte b1 = b[1];
byte b2 = b[2];
byte b3 = b[3];
byte b6 = b[6];
byte b7 = b[7];
byte b8 = b[8];
byte b9 = b[9];
if (b0 == (byte) 'G' && b1 == (byte) 'I' && b2 == (byte) 'F') {
return true;
} else if (b1 == (byte) 'P' && b2 == (byte) 'N' && b3 == (byte) 'G') {
return true;
} else if (b6 == (byte) 'J' && b7 == (byte) 'F' && b8 == (byte) 'I'&& b9 == (byte) 'F') {
return true;
} else {
return false;
}
} else {
return false;
}
}
}
应用: 检测客户端是否上传了合法的图片文件,防止恶意修改扩展名上传文件.
一下是通过是否能读到图像文件来判断的,其原理也是读取文件头信息来判断.
Test.java
import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
import java.util.*;
import javax.imageio.stream.*;
public class Test{
public static void main(String[] args) {
File f = new File("c://test.jpg");
if (f.exists()) {
System.out.println(getFormatInFile(f));
}
}
// Returns the format of the image in the file 'f'.
// Returns null if the format is not known.public static String getFormatInFile(File f) {
return getFormatName(f);
}
// Returns the format name of the image in the object 'o'.
// Returns null if the format is not known.
private static String getFormatName(Object o) {
try {
// Create an image input stream on the image
ImageInputStream iis = ImageIO.createImageInputStream(o);
// Find all image readers that recognize the image format
Iterator iter = ImageIO.getImageReaders(iis);
if (!iter.hasNext()) {
// No readers found
return null;
}
// Use the first reader
ImageReader reader = (ImageReader)iter.next();
// Close stream
iis.close();
// Return the format name
return reader.getFormatName();
} catch (IOException e) {
//
}
// The image could not be read
return null;
}
}
ImageInfo.java
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;
public class ImageInfo
{
public ImageInfo()
{
collectComments = true;
}
private void addComment(String s)
{
if(comments == null)
comments = new Vector();
comments.addElement(s);
}
public boolean check()
{
format = -1;
width = -1;
height = -1;
bitsPerPixel = -1;
numberOfImages = 1;
physicalHeightDpi = -1;
physicalWidthDpi = -1;
comments = null;
int b1;
int b2;
try{
b1 = read() & 0xff;
b2 = read() & 0xff;
if(b1 == 71 && b2 == 73)
return checkGif();
if(b1 == 137 && b2 == 80)
return checkPng();
if(b1 == 255 && b2 == 216)
return checkJpeg();
if(b1 == 66 && b2 == 77)
return checkBmp();
if(b1 == 10 && b2 < 6)
return checkPcx();
if(b1 == 70 && b2 == 79)
return checkIff();
if(b1 == 89 && b2 == 166)
return checkRas();
if(b1 == 80 && b2 >= 49 && b2 <= 54)
return checkPnm(b2 - 48);
if(b1 == 56 && b2 == 66)
return checkPsd();
}catch(IOException ioe){
return false;
}
return false;
}
private boolean checkBmp()
throws IOException
{
byte a[] = new byte[44];
if(read(a) != a.length)
return false;
width = getIntLittleEndian(a, 16);
height = getIntLittleEndian(a, 20);
if(width < 1 || height < 1)
return false;
bitsPerPixel = getShortLittleEndian(a, 26);
if(bitsPerPixel != 1 && bitsPerPixel != 4 && bitsPerPixel != 8 && bitsPerPixel != 16 && bitsPerPixel != 24 && bitsPerPixel != 32)
return false;
int x = (int)((double)getIntLittleEndian(a, 36) * 0.025399999999999999D);
if(x > 0)
setPhysicalWidthDpi(x);
int y = (int)((double)getIntLittleEndian(a, 40) * 0.025399999999999999D);
if(y > 0)
setPhysicalHeightDpi(y);
format = 3;
return true;
}
private boolean checkGif()
throws IOException
{
byte GIF_MAGIC_87A[] = {
70, 56, 55, 97
};
byte GIF_MAGIC_89A[] = {
70, 56, 57, 97
};
byte a[] = new byte[11];
if(read(a) != 11)
return false;
if(!equals(a, 0, GIF_MAGIC_89A, 0, 4) && !equals(a, 0, GIF_MAGIC_87A, 0, 4))
return false;
format = 1;
width = getShortLittleEndian(a, 4);
height = getShortLittleEndian(a, 6);
int flags = a[8] & 0xff;
bitsPerPixel = (flags >> 4 & 7) + 1;
if(!determineNumberOfImages)
return true;
if((flags & 0x80) != 0)
{
int tableSize = (1 << (flags & 7) + 1) * 3;
skip(tableSize);
}
numberOfImages = 0;
int blockType;
do
{
blockType = read();
switch(blockType)
{
case 44: // ','
{
if(read(a, 0, 9) != 9)
return false;
flags = a[8] & 0xff;
progressive = (flags & 0x40) != 0;
int localBitsPerPixel = (flags & 7) + 1;
if(localBitsPerPixel > bitsPerPixel)
bitsPerPixel = localBitsPerPixel;
if((flags & 0x80) != 0)
skip((1 << localBitsPerPixel) * 3);
skip(1);
int n;
do
{
n = read();
if(n > 0)
skip(n);
else
if(n == -1)
return false;
} while(n > 0);
numberOfImages++;
break;
}
case 33: // '!'
{
int extensionType = read();
int n;
if(collectComments && extensionType == 254)
{
StringBuffer sb = new StringBuffer();
do
{
n = read();
if(n == -1)
return false;
if(n > 0)
{
for(int i = 0; i < n; i++)
{
int ch = read();
if(ch == -1)
return false;
sb.append((char)ch);
}
}
} while(n > 0);
} else
{
do
{
n = read();
if(n > 0)
skip(n);
else
if(n == -1)
return false;
} while(n > 0);
}
break;
}
default:
{
return false;
}
case 59: // ';'
break;
}
} while(blockType != 59);
return true;
}
private boolean checkIff()
throws IOException
{
byte a[] = new byte[10];
if(read(a, 0, 10) != 10)
return false;
byte IFF_RM[] = {
82, 77
};
if(!equals(a, 0, IFF_RM, 0, 2))
return false;
int type = getIntBigEndian(a, 6);
if(type != 0x494c424d && type != 0x50424d20)
return false;
do
{
if(read(a, 0, !=
return false;
int chunkId = getIntBigEndian(a, 0);
int size = getIntBigEndian(a, 4);
if((size & 1) == 1)
size++;
if(chunkId == 0x424d4844)
if(read(a, 0, 9) != 9)
{
return false;
} else
{
format = 5;
width = getShortBigEndian(a, 0);
height = getShortBigEndian(a, 2);
bitsPerPixel = a[8] & 0xff;
return width > 0 && height > 0 && bitsPerPixel > 0 && bitsPerPixel < 33;
}
skip(size);
} while(true);
}
private boolean checkJpeg()
throws IOException
{
byte data[] = new byte[12];
do
{
if(read(data, 0, 4) != 4)
return false;
int marker = getShortBigEndian(data, 0);
int size = getShortBigEndian(data, 2);
if((marker & 0xff00) != 65280)
return false;
if(marker == 65504)
{
if(size < 14)
{
skip(size - 2);
} else
{
if(read(data, 0, 12) != 12)
return false;
byte APP0_ID[] = {
74, 70, 73, 70, 0
};
if(equals(APP0_ID, 0, data, 0, 5))
if(data[7] == 1)
{
setPhysicalWidthDpi(getShortBigEndian(data,);
setPhysicalHeightDpi(getShortBigEndian(data, 10));
} else
if(data[7] == 2)
{
int x = getShortBigEndian(data,;
int y = getShortBigEndian(data, 10);
setPhysicalWidthDpi((int)((float)x * 2.54F));
setPhysicalHeightDpi((int)((float)y * 2.54F));
}
skip(size - 14);
}
} else
if(collectComments && size > 2 && marker == 65534)
{
byte chars[] = new byte[size -= 2];
if(read(chars, 0, size) != size)
return false;
String comment = new String(chars, "iso-8859-1");
comment = comment.trim();
addComment(comment);
} else
{
if(marker >= 65472 && marker <= 65487 && marker != 65476 && marker != 65480)
if(read(data, 0, 6) != 6)
{
return false;
} else
{
format = 0;
bitsPerPixel = (data[0] & 0xff) * (data[5] & 0xff);
progressive = marker == 65474 || marker == 65478 || marker == 65482 || marker == 65486;
width = getShortBigEndian(data, 3);
height = getShortBigEndian(data, 1);
return true;
}
skip(size - 2);
}
} while(true);
}
private boolean checkPcx()
throws IOException
{
byte a[] = new byte[64];
if(read(a) != a.length)
return false;
if(a[0] != 1)
return false;
int x1 = getShortLittleEndian(a, 2);
int y1 = getShortLittleEndian(a, 4);
int x2 = getShortLittleEndian(a, 6);
int y2 = getShortLittleEndian(a,;
if(x1 < 0 || x2 < x1 || y1 < 0 || y2 < y1)
return false;
width = (x2 - x1) + 1;
height = (y2 - y1) + 1;
int bits = a[1];
int planes = a[63];
if(planes == 1 && (bits == 1 || bits == 2 || bits == 4 || bits ==)
bitsPerPixel = bits;
else
if(planes == 3 && bits ==
bitsPerPixel = 24;
else
return false;
setPhysicalWidthDpi(getShortLittleEndian(a, 10));
setPhysicalHeightDpi(getShortLittleEndian(a, 10));
format = 4;
return true;
}
private boolean checkPng()
throws IOException
{
byte PNG_MAGIC[] = {
78, 71, 13, 10, 26, 10
};
byte a[] = new byte[27];
if(read(a) != 27)
return false;
if(!equals(a, 0, PNG_MAGIC, 0, 6))
return false;
format = 2;
width = getIntBigEndian(a, 14);
height = getIntBigEndian(a, 18);
bitsPerPixel = a[22] & 0xff;
int colorType = a[23] & 0xff;
if(colorType == 2 || colorType == 6)
bitsPerPixel *= 3;
progressive = (a[26] & 0xff) != 0;
return true;
}
private boolean checkPnm(int id)
throws IOException
{
if(id < 1 || id > 6)
return false;
int PNM_FORMATS[] = {
7, 8, 9
};
format = PNM_FORMATS[(id - 1) % 3];
boolean hasPixelResolution = false;
String s;
do
{
do
{
s = readLine();
if(s != null)
s = s.trim();
} while(s == null || s.length() < 1);
if(s.charAt(0) == '#')
{
if(collectComments && s.length() > 1)
addComment(s.substring(1));
continue;
}
if(hasPixelResolution)
break;
int spaceIndex = s.indexOf(' ');
if(spaceIndex == -1)
return false;
String widthString = s.substring(0, spaceIndex);
spaceIndex = s.lastIndexOf(' ');
if(spaceIndex == -1)
return false;
String heightString = s.substring(spaceIndex + 1);
try
{
width = Integer.parseInt(widthString);
height = Integer.parseInt(heightString);
}
catch(NumberFormatException nfe)
{
return false;
}
if(width < 1 || height < 1)
return false;
if(format == 7)
{
bitsPerPixel = 1;
return true;
}
hasPixelResolution = true;
} while(true);
int maxSample;
try
{
maxSample = Integer.parseInt(s);
}
catch(NumberFormatException nfe)
{
return false;
}
if(maxSample < 0)
return false;
for(int i = 0; i < 25; i++)
if(maxSample < 1 << i + 1)
{
bitsPerPixel = i + 1;
if(format == 9)
bitsPerPixel *= 3;
return true;
}
return false;
}
private boolean checkPsd()
throws IOException
{
byte a[] = new byte[24];
if(read(a) != a.length)
return false;
byte PSD_MAGIC[] = {
80, 83
};
if(!equals(a, 0, PSD_MAGIC, 0, 2))
{
return false;
} else
{
format = 10;
width = getIntBigEndian(a, 16);
height = getIntBigEndian(a, 12);
int channels = getShortBigEndian(a, 10);
int depth = getShortBigEndian(a, 20);
bitsPerPixel = channels * depth;
return width > 0 && height > 0 && bitsPerPixel > 0 && bitsPerPixel <= 64;
}
}
private boolean checkRas()
throws IOException
{
byte a[] = new byte[14];
if(read(a) != a.length)
return false;
byte RAS_MAGIC[] = {
106, -107
};
if(!equals(a, 0, RAS_MAGIC, 0, 2))
{
return false;
} else
{
format = 6;
width = getIntBigEndian(a, 2);
height = getIntBigEndian(a, 6);
bitsPerPixel = getIntBigEndian(a, 10);
return width > 0 && height > 0 && bitsPerPixel > 0 && bitsPerPixel <= 24;
}
}
private static boolean determineVerbosity(String args[])
{
if(args != null && args.length > 0)
{
for(int i = 0; i < args.length; i++)
if("-c".equals(args[i]))
return false;
}
return true;
}
private static boolean equals(byte a1[], int offs1, byte a2[], int offs2, int num)
{
while(num-- > 0)
if(a1[offs1++] != a2[offs2++])
return false;
return true;
}
public int getBitsPerPixel()
{
return bitsPerPixel;
}
public String getComment(int index)
{
if(comments == null || index < 0 || index >= comments.size())
throw new IllegalArgumentException("Not a valid comment index: " + index);
else
return (String)comments.elementAt(index);
}
public int getFormat()
{
return format;
}
public String getFormatName()
{
if(format >= 0 && format < FORMAT_NAMES.length)
return FORMAT_NAMES[format];
else
return "?";
}
public int getHeight()
{
return height;
}
private static int getIntBigEndian(byte a[], int offs)
{
return (a[offs] & 0xff) << 24 | (a[offs + 1] & 0xff) << 16 | (a[offs + 2] & 0xff) << 8 | a[offs + 3] & 0xff;
}
private static int getIntLittleEndian(byte a[], int offs)
{
return (a[offs + 3] & 0xff) << 24 | (a[offs + 2] & 0xff) << 16 | (a[offs + 1] & 0xff) << 8 | a[offs] & 0xff;
}
public String getMimeType()
{
if(format >= 0 && format < MIME_TYPE_STRINGS.length)
{
if(format == 0 && progressive)
return "image/pjpeg";
else
return MIME_TYPE_STRINGS[format];
} else
{
return null;
}
}
public int getNumberOfComments()
{
if(comments == null)
return 0;
else
return comments.size();
}
public int getNumberOfImages()
{
return numberOfImages;
}
public int getPhysicalHeightDpi()
{
return physicalHeightDpi;
}
public float getPhysicalHeightInch()
{
int h = getHeight();
int ph = getPhysicalHeightDpi();
if(h > 0 && ph > 0)
return (float)h / (float)ph;
else
return -1F;
}
public int getPhysicalWidthDpi()
{
return physicalWidthDpi;
}
public float getPhysicalWidthInch()
{
int w = getWidth();
int pw = getPhysicalWidthDpi();
if(w > 0 && pw > 0)
return (float)w / (float)pw;
else
return -1F;
}
private static int getShortBigEndian(byte a[], int offs)
{
return (a[offs] & 0xff) << 8 | a[offs + 1] & 0xff;
}
private static int getShortLittleEndian(byte a[], int offs)
{
return a[offs] & 0xff | (a[offs + 1] & 0xff) << 8;
}
public int getWidth()
{
return width;
}
public boolean isProgressive()
{
return progressive;
}
public static void main(String args[])
{
ImageInfo imageInfo = new ImageInfo();
imageInfo.setDetermineImageNumber(true);
boolean verbose = determineVerbosity(args);
if(args.length == 0)
{
run(null, System.in, imageInfo, verbose);
} else
{
int index = 0;
do
{
if(index >= args.length)
break;
InputStream in = null;
try
{
String name = args[index++];
System.out.print(name + ";");
if(name.startsWith("http://"))
in = (new URL(name)).openConnection().getInputStream();
else
in = new FileInputStream(name);
run(name, in, imageInfo, verbose);
in.close();
}
catch(IOException e)
{
System.out.println(e);
try
{
if(in != null)
in.close();
}
catch(IOException ee) { }
}
} while(true);
}
}
private static void print(String sourceName, ImageInfo ii, boolean verbose)
{
if(verbose)
printVerbose(sourceName, ii);
else
printCompact(sourceName, ii);
}
private static void printCompact(String sourceName, ImageInfo imageInfo)
{
String SEP = "\t";
System.out.println(sourceName + "\t" + imageInfo.getFormatName() + "\t" + imageInfo.getMimeType() + "\t" + imageInfo.getWidth() + "\t" + imageInfo.getHeight() + "\t" + imageInfo.getBitsPerPixel() + "\t" + imageInfo.getNumberOfImages() + "\t" + imageInfo.getPhysicalWidthDpi() + "\t" + imageInfo.getPhysicalHeightDpi() + "\t" + imageInfo.getPhysicalWidthInch() + "\t" + imageInfo.getPhysicalHeightInch() + "\t" + imageInfo.isProgressive());
}
private static void printLine(int indentLevels, String text, float value, float minValidValue)
{
if(value < minValidValue)
{
return;
} else
{
printLine(indentLevels, text, Float.toString(value));
return;
}
}
private static void printLine(int indentLevels, String text, int value, int minValidValue)
{
if(value >= minValidValue)
printLine(indentLevels, text, Integer.toString(value));
}
private static void printLine(int indentLevels, String text, String value)
{
if(value == null || value.length() == 0)
return;
while(indentLevels-- > 0)
System.out.print("\t");
if(text != null && text.length() > 0)
{
System.out.print(text);
System.out.print(" ");
}
System.out.println(value);
}
private static void printVerbose(String sourceName, ImageInfo ii)
{
printLine(0, null, sourceName);
printLine(1, "File format: ", ii.getFormatName());
printLine(1, "MIME type: ", ii.getMimeType());
printLine(1, "Width (pixels): ", ii.getWidth(), 1);
printLine(1, "Height (pixels): ", ii.getHeight(), 1);
printLine(1, "Bits per pixel: ", ii.getBitsPerPixel(), 1);
printLine(1, "Progressive: ", ii.isProgressive() ? "yes" : "no");
printLine(1, "Number of images: ", ii.getNumberOfImages(), 1);
printLine(1, "Physical width (dpi): ", ii.getPhysicalWidthDpi(), 1);
printLine(1, "Physical height (dpi): ", ii.getPhysicalHeightDpi(), 1);
printLine(1, "Physical width (inches): ", ii.getPhysicalWidthInch(), 1.0F);
printLine(1, "Physical height (inches): ", ii.getPhysicalHeightInch(), 1.0F);
int numComments = ii.getNumberOfComments();
printLine(1, "Number of textual comments: ", numComments, 1);
if(numComments > 0)
{
for(int i = 0; i < numComments; i++)
printLine(2, null, ii.getComment(i));
}
}
private int read()
throws IOException
{
if(in != null)
return in.read();
else
return din.readByte();
}
private int read(byte a[])
throws IOException
{
if(in != null)
{
return in.read(a);
} else
{
din.readFully(a);
return a.length;
}
}
private int read(byte a[], int offset, int num)
throws IOException
{
if(in != null)
{
return in.read(a, offset, num);
} else
{
din.readFully(a, offset, num);
return num;
}
}
private String readLine()
throws IOException
{
return readLine(new StringBuffer());
}
private String readLine(StringBuffer sb)
throws IOException
{
boolean finished;
do
{
int value = read();
finished = value == -1 || value == 10;
if(!finished)
sb.append((char)value);
} while(!finished);
return sb.toString();
}
private static void run(String sourceName, InputStream in, ImageInfo imageInfo, boolean verbose)
{
imageInfo.setInput(in);
imageInfo.setDetermineImageNumber(true);
imageInfo.setCollectComments(verbose);
if(imageInfo.check())
print(sourceName, imageInfo, verbose);
}
public void setCollectComments(boolean newValue)
{
collectComments = newValue;
}
public void setDetermineImageNumber(boolean newValue)
{
determineNumberOfImages = newValue;
}
public void setInput(DataInput dataInput)
{
din = dataInput;
in = null;
}
public void setInput(InputStream inputStream)
{
in = inputStream;
din = null;
}
private void setPhysicalHeightDpi(int newValue)
{
physicalWidthDpi = newValue;
}
private void setPhysicalWidthDpi(int newValue)
{
physicalHeightDpi = newValue;
}
private void skip(int num)
throws IOException
{
while(num > 0)
{
long result;
if(in != null)
result = in.skip(num);
else
result = din.skipBytes(num);
if(result > 0L)
{
num = (int)((long)num - result);
} else
{
if(in != null)
result = in.read();
else
result = din.readByte();
if(result == -1L)
throw new IOException("Premature end of input.");
num--;
}
}
}
public static final int FORMAT_JPEG = 0;
public static final int FORMAT_GIF = 1;
public static final int FORMAT_PNG = 2;
public static final int FORMAT_BMP = 3;
public static final int FORMAT_PCX = 4;
public static final int FORMAT_IFF = 5;
public static final int FORMAT_RAS = 6;
public static final int FORMAT_PBM = 7;
public static final int FORMAT_PGM = 8;
public static final int FORMAT_PPM = 9;
public static final int FORMAT_PSD = 10;
private static final String FORMAT_NAMES[] = {
"JPEG", "GIF", "PNG", "BMP", "PCX", "IFF", "RAS", "PBM", "PGM", "PPM",
"PSD"
};
private static final String MIME_TYPE_STRINGS[] = {
"image/jpeg", "image/gif", "image/png", "image/bmp", "image/pcx", "image/iff", "image/ras", "image/x-portable-bitmap", "image/x-portable-graymap", "image/x-portable-pixmap",
"image/psd"
};
private int width;
private int height;
private int bitsPerPixel;
private boolean progressive;
private int format;
private InputStream in;
private DataInput din;
private boolean collectComments;
private Vector comments;
private boolean determineNumberOfImages;
private int numberOfImages;
private int physicalHeightDpi;
private int physicalWidthDpi;
}
发表评论
-
权限管理控制算法(位与算)
2011-02-15 09:46 673最近在网上查找一些关 ... -
java获取项目路径
2011-01-28 11:25 2374Java代码 在jsp和class文件 ... -
log4j使用方法(转)
2011-01-25 14:32 7421、定义配置文件 首先使用配置文件将使我们的应用程序更加灵 ... -
简单判断是否是图片文件
2011-01-24 15:08 810主要用了JAVA里面的ImageIO类: /** * 判 ... -
查看jar包编译的jdk版本
2011-01-24 15:07 2609在jar包中,用winrar解压一个类文件,然后在命令行下面输 ... -
一种简单实用的内容页大图片处理方式 [转]
2011-01-24 15:05 779这种方法的思路是:图片超出限制尺寸时,自动缩小到适当的大小,单 ... -
读写XML中的一个小错误
2011-01-24 15:02 574今天测试突然跑来说程序有问题 靠 我环境跟QA机子一样的 ...
相关推荐
辨别图像格式,分辨率,像素,备注等信息 Get file format, image resolution, number of bits per pixel and optionally number of images, ... JPEG, GIF, BMP, PCX, PNG, IFF, RAS, PBM, PGM, PPM and PSD files
ImageInfo.java 这个包里是包含ImageTextView要显示gif,bmp以及相对应的特殊字符串的地方 ImageTextView.java 这个包里是一个特殊的TextView,可以用来显示gif和bmp的TextView GifOpenHelper.java 这是用来运态解析...
TDCImageInfo returns image type, dimensions, and color depth from GIF, JPEG, PNG,BMP, PCX, and TIFF files.
支持的格式bmp gif hdr(pic) jpeg(jpg) png psd ga 蒂芙(tif) 网页还有更多...用法最简单的演示ImageInfo< const> imageInfo ( " images/valid/jpg/sample.jpg " );std::cout << " File: " << ...
目前,它支持Png,Jpeg,Gif和(未压缩的)Swf分析。 如果您还有可用的 zlib( npm install zlib ),那么它将支持压缩的 Swf 文件。用法: 要将 imageinfo 安装到您的项目中,请使用npm install imageinfo ,然后...
给谷歌浏览器添加右键菜单功能,可查看网页图片信息
fckeditor-java-core-2.6.jar imageinfo-1.9.jar serializer.jar fckeditor在进行文字编辑时,使用的仍是html标签,只是终端客户在编辑时见不到一系列标签而矣.它传往后台的字符串是添加了标签的字符串. ...
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import java.lang.*; /**the class used to show the image*/ public class DrawPanel extends ...
很好用哦,图片切换,幻灯片,的效果。 添加到组件里面,拖过来就能使用,ASP。NET,控件,图片控件,后台代码, if (!IsPostBack) { List<ImageInfo> imgs = new List<ImageInfo>(); ImageInfo img1 = new ...
image_to_c 用于将二进制图像文件转换为C源代码的命令行工具。 输出是无符号字符数组,并发送到stdout。... PNG,JPEG,BMP,TIFF,GIF,PPM,TARGA,JEDMCS,CALS和PCX 无法识别的文件会怎样? 如果文件类型未知,它
Imageinfo是一个命令行工具,可识别TIFF,GIF,PNG,BMP,JPEG,TGA,PBM,PGM,PPM图像文件。 这是显示有关每个文件的有用信息而无需使用外部库或任何C运行时代码所必需的最简单的C代码。 当前的EXE是10K字节。
图片服务器上传源码,含服务端、客户端,NET源码
一个可以用来查看一副图片所附带的完整信息的DLL库的源码,包括普篇普通属性和EXIF信息等,方便实用。
语言:English 获取图像的图像信息,包括EXIF数据 获取图像信息是从用户那里获取的,并将图像的确切信息提供给用户,因为图像以Gif格式显示,尺寸为1280 * 720图像长宽比和其他信息。
java-core-2.6.jar,commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,imageinfo-1.9.jar,slf4j-api-1.5.8.jar,slf4j-simple-1.5.8.jar共六个
举例说明: 今天学习PHP的图像函数部分,学习了几个函数,他们分别是: getimagesize() array getimagesize ( string $filename [, array &$imageinfo ] ) getimagesize() 函数将测定任何 GIF,JPG,PNG,SWF,SWC...
antlr-2.7.5H3.jar antlr.jar asm.jar aspectjrt.jar aspectjweaver.jar cglib-2.1.3.jar cglib-nodep-2.1_3.jar commons-****.jar ehcache-1.1.jar imageinfo-1.9.jar java-core-2.6.jar jsf-api.jar morph-1.1.1....
JII(Java Image Info)为 Java 库和源代码的集合提供了一个干净的接口,以读取图像的基本属性。
"image.jpg", function (err, data) { if (err) { return console.error(err); } console.log(data); }); ##License ImageInfo 0.1.2 - 用于读取图像元数据的节点模块。 基于 Jacon Seidelin 版权所有 (c) ...
开发者使用本 SDK 可以方便的从浏览器端上传文件至七牛云存储,并对上传成功后的图片进行丰富的数据处理操作。本 SDK 可使开发者忽略上传底层实现细节,而更多的关注 UI 层的展现。 功能简介 上传 html5模式大于4...