- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试读取一个 ico 文件,其中包含两个 bitcount = 8
的 ico 图像。我知道 ICONDIRENTRY 格式 ( https://msdn.microsoft.com/en-us/library/ms997538.aspx ) 并且此代码主要与几个特定的 ico 文件不同。下面是我的代码-
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import java.io.FileInputStream;
import java.nio.ByteOrder;
import java.util.ArrayList;
/**
* Created by dsomesh8 on 5/25/2018.
*/
public class Program {
private static ArrayList<IconDirEntry> iconDirEntries;
private static final byte SEED = -67;
private static final byte SEED2 = 107;
private static final String HEADER = "@OB@";
private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static ImageInputStream in;
public static void main(String[] args) {
FileInputStream fis = null;
try {
//C:\Users\dsomesh8\Downloads\Logs\test\Tool.ico
//C:\Users\dsomesh8\Downloads\icons\zen.ico
String filePath = "C:\\Users\\dsomesh8\\Downloads\\Logs\\test\\Tool.ico";
//String filePath="C:\\Users\\tdivya\\Downloads\\test.ico";
fis = new FileInputStream(filePath);
in = ImageIO.createImageInputStream(fis);
ArrayList<IconDirEntry> list=decodeIcon(in);
IconImage nweIcon=new IconImage(list.get(0));
//iconDirEntries = new ArrayList<IconDirEntry>();
//boolean res = ;
} catch (java.io.FileNotFoundException fnfe) {
//WebLogger.debug("Input icon file " + filePath + " is missing");
} catch (java.io.IOException ioe) {
//WebLogger.debug("IO Exception reading the icon file " + filePath);
}
}
private static ArrayList<IconDirEntry> decodeIcon(ImageInputStream in)
{
try
{
in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
in.readShort(); // idReserved field
if(in.readShort() != 1) // idType field
return null;
int imgCount = in.readShort(); //No of icon entries
iconDirEntries = new ArrayList<IconDirEntry>();
System.out.println(imgCount);
for(int i = 0; i < imgCount; i++)
{
IconDirEntry dirEntry = new IconDirEntry(in);
System.out.println(dirEntry.toString());
iconDirEntries.add(dirEntry);
}
}
catch(java.io.IOException ioe)
{
// WebLogger.debug("IOException reading the reserved field of the icon");
return null;
}
return iconDirEntries;
}
}
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
/*
typdef struct
{
BITMAPINFOHEADER icHeader; // DIB header
RGBQUAD icColors[1]; // Color table
BYTE icXOR[1]; // DIB bits for XOR mask
BYTE icAND[1]; // DIB bits for AND mask
} ICONIMAGE, *LPICONIMAGE;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
typedef struct tagRGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;
*/
public class IconImage
{
private int biSize;
private int biWidth;
private int biHeight;
private int biPlanes;
private int biBitCount;
private int biCompression;
private int biSizeImage;
private int biXPelsPerMeter;
private int biYPelsPerMeter;
private int biClrUsed;
private int biClrImportant;
private byte[] rgbQuad;
private byte[] icXOR;
private byte[] icAND;
private RGBQuad[] colors;
private byte[] andMask;
private byte[] xorMask;
private IconDirEntry entry;
private ImageInputStream iis;
public IconImage(IconDirEntry entry)
{
this.entry = entry;
try
{
iis = ImageIO.createImageInputStream(new ByteArrayInputStream(entry.getImageData()));
iis.setByteOrder(java.nio.ByteOrder.LITTLE_ENDIAN);
biSize = iis.readInt();
biWidth = iis.readInt();
biHeight = iis.readInt();
biPlanes = iis.readShort();
biBitCount = iis.readShort();
biCompression = iis.readInt();
biSizeImage = iis.readInt();
biXPelsPerMeter = iis.readInt();
biYPelsPerMeter = iis.readInt();
biClrUsed = iis.readInt();
biClrImportant = iis.readInt();
if(entry.getBitCount() <= 8)
{
int nColors = (int)(Math.pow(2, biBitCount));
colors = new RGBQuad[nColors]; //color table specifying colors uses in the image
for(int i = 0; i < colors.length; i++)
{
colors[i] = new RGBQuad(iis);
}
int bitsPerPixel = biBitCount;
int pixelsPerByte = 8/bitsPerPixel;
int nPixels = biWidth*biHeight/2; //biHeight is twice of actual height
int nBytes = nPixels/pixelsPerByte;
xorMask = new byte[nBytes];
for(int i = 0; i < nBytes; i++)
{
xorMask[i] = (byte)iis.readUnsignedByte();
}
int paddedWidth = 0;
if(biWidth <= 32)
paddedWidth = 32;
else
{
int rem = biWidth%32;
if(rem == 0)
paddedWidth = biWidth;
else
paddedWidth = (biWidth/32 + 1)*32; //Round off to the next multiple of 32
}
int len = paddedWidth*(biHeight/2)/8;
//AND mask is a monochrome DIB, with a color depth of 1 bpp
andMask = new byte[len];
for(int i = 0; i < len; i++)
{
andMask[i] = (byte)iis.readUnsignedByte();
}
}
}
catch(Exception ioe)
{
System.out.println("Exception while reading image details for icon entry");
}
}
public int[] getPixelValues()
{
int nRows = entry.getHeight();
int nCols = entry.getWidth();
int bpp = entry.getBitCount()/8; //Bytes per pixel
int[] pixelValues = new int[nRows*nCols];
for(int row = 0; row < nRows; row++)
{
byte[] rowData = new byte[nCols*bpp];
try
{
iis.readFully(rowData);
}
catch(Exception e)
{
System.out.println("Exception reading the image data for this entry!!!");
}
int curRow = nRows - row; //Moving upwards starting from the last row
int pos = (curRow - 1)*nCols; //Index of first pixel at current row
int iByte = 0; //Iterator for each byte
for(int col = 0; col < nCols; col++)
{
int pixelValue = 0;
pixelValue = (rowData[iByte++] & 0xFF);
if(bpp > 1)
pixelValue += ((rowData[iByte++] & 0xFF) << 8);
if(bpp > 2)
pixelValue += ((rowData[iByte++] & 0xFF) << 16);
if(bpp > 3)
pixelValue += ((rowData[iByte++] & 0xFF) << 24);
else
{
//if (pixelValue == 0)
pixelValue += ((255 & 0xFF) << 24);
}
pixelValues[pos] = pixelValue;
pos++;
}
}
return pixelValues;
}
public BufferedImage getIconGraphics()
{
BufferedImage buffImg = new BufferedImage(entry.getWidth(), entry.getHeight(), BufferedImage.TYPE_INT_ARGB);
final Color TRANSPARENT = new Color(0, 0, 0, 0);
Graphics2D g = buffImg.createGraphics();
for(int y = biHeight/2 - 1; y >= 0; y--)
{
for(int x = 0; x < biWidth; x++)
{
if(isTransparent(x, y))
g.setColor(TRANSPARENT);
else
g.setColor(getRGB(x, y));
g.fillRect(x, entry.getHeight() - y - 1, 1, 1);
}
}
return buffImg;
}
private boolean isTransparent(int x, int y)
{
int paddedWidth = 0;
if(biWidth <= 32)
paddedWidth = 32;
else
{
int rem = biWidth%32;
if(rem == 0)
paddedWidth = biWidth;
else
paddedWidth = (biWidth/32 + 1)*32; //Round off to the next multiple of 32
}
int pixelIndex = (paddedWidth*y) + x;
int andByteIndex = pixelIndex/8;
int andByte = andMask[andByteIndex];
int pos = x%8; //position of bit in the byte, for pixel x,y
int nRightShift = 8 - (pos + 1); //Right shift needed to get the bit to LSB; increment of 1 since x starts from 0
int pixelBit = andByte >> nRightShift;
int andMask = pixelBit & 1;
return (andMask == 1);
}
private Color getRGB(int x, int y)
{
int pixelIndex = (biWidth*y) + x;
int bitsPerPixel = biBitCount;
int pixelsPerByte = 8/bitsPerPixel;
int xorByteIndex = pixelIndex/pixelsPerByte;
int shift = ((pixelsPerByte - (x%pixelsPerByte) - 1)*biBitCount);
int colIdx = (xorMask[xorByteIndex] >> shift) & ((1 << biBitCount) - 1);
int b = colors[colIdx].getBlue();
int g = colors[colIdx].getGreen();
int r = colors[colIdx].getRed();
return new Color(r, g, b);
}
}
import com.sun.imageio.plugins.common.ReaderUtil;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
/*
typedef struct
{
BYTE bWidth; // Width, in pixels, of the image
BYTE bHeight; // Height, in pixels, of the image
BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
BYTE bReserved; // Reserved ( must be 0)
WORD wPlanes; // Color Planes
WORD wBitCount; // Bits per pixel
DWORD dwBytesInRes; // How many bytes in this resource?
DWORD dwImageOffset; // Where in the file is this image?
} ICONDIRENTRY, *LPICONDIRENTRY;
*/
public class IconDirEntry
{
private short width;
private short height;
private short colorCount;
private short reserved;
private int planes;
private int bitCount;
private int bytesInResource;
private int imageOffset;
private byte[] imgData;
public IconDirEntry(ImageInputStream in)
{
try
{
//System.out.println("canDecodeInput-"+canDecodeInput(in));
// bitCount = readBitCountFromImageData(imgData);
width = (short)in.readUnsignedByte();
height = (short)in.readUnsignedByte();
colorCount = new Byte(in.readByte()).shortValue();
reserved = new Byte(in.readByte()).shortValue();
planes = in.readShort();
bitCount = in.readShort();
bytesInResource = in.readInt();
imageOffset = in.readInt();
/*
System.out.println("val : " + width);
System.out.println("val : " + height);
System.out.println("val : " + colorCount);
System.out.println("val : " + reserved);
System.out.println("val : " + planes);
System.out.println("val : " + bitCount);
System.out.println("val : " + bytesInResource);
System.out.println("val : " + imageOffset);
System.out.println("\n");
*/
in.mark();
long curPos = in.getStreamPosition();
int nBytesToSkip = imageOffset - (int)curPos;
in.skipBytes(nBytesToSkip);
imgData = new byte[bytesInResource];
try
{
in.read(imgData);
}
finally
{
in.reset();
}
// Certain icons will not specify the bitCount at the icon entry level.
// For such cases, read the bitCount from the image data
if(bitCount == 0 && imageOffset > 0)
bitCount = readBitCountFromImageData(imgData);
}
catch(Exception e)
{
System.out.println("Exception reading icon entry");
}
}
/*
* Image data structure:
typdef struct
{
BITMAPINFOHEADER icHeader; // DIB header
RGBQUAD icColors[1]; // Color table
BYTE icXOR[1]; // DIB bits for XOR mask
BYTE icAND[1]; // DIB bits for AND mask
} ICONIMAGE, *LPICONIMAGE;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
*
* Read biBitCount
*/
private int readBitCountFromImageData(byte[] imgData) throws IOException
{
ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(imgData));
iis.setByteOrder(java.nio.ByteOrder.LITTLE_ENDIAN);
// These many number of bytes can actually be skipped. Reading for code clarity.
iis.readInt(); // biSize
iis.readInt(); // biWidth
iis.readInt(); // biHeight
iis.readShort(); // biPlanes
int biBitCount = iis.readShort();
return biBitCount;
}
public short getWidth()
{
return width;
}
public short getHeight()
{
return height;
}
public int getBitCount()
{
return bitCount;
}
public byte[] getImageData()
{
return imgData;
}
}
有问题的地方是
iis = ImageIO.createImageInputStream(new ByteArrayInputStream(entry.getImageData()));
虽然实际的位数是 8,但发布这篇文章后我得到的位数是一个大整数。因此,在创建这么大的数组时会抛出以下异常-
“java.lang.OutOfMemoryError:请求的数组大小超出 VM 限制”
失败的 ico 文件是 https://www.dropbox.com/s/euh52s0vc2s2ryf/Tool.ico?dl=0
最佳答案
对于这个图标,图像数据不遵循tagBITMAPINFOHEADER
结构。相反,它们是嵌入的 PNG 图像,您可以在第一个不是常规大小的单词(遵循 tagBITMAPINFOHEADER
结构)时识别它们,而是 PNG 图像的神奇单词。
您可以通过更改 IconImage
的开头来验证这一点的构造函数为
public IconImage(IconDirEntry entry)
{
this.entry = entry;
try
{
final ByteArrayInputStream bais = new ByteArrayInputStream(entry.getImageData());
bais.mark(4);
iis = ImageIO.createImageInputStream(bais);
iis.setByteOrder(java.nio.ByteOrder.LITTLE_ENDIAN);
biSize = iis.readInt();
if(biSize == 0x474e5089) { //PNG instead of tagBITMAPINFOHEADER)
bais.reset();
BufferedImage bi = ImageIO.read(bais);
System.out.println("read embedded PNG "+bi.getWidth()+" x "+bi.getHeight());
return;
}
…
神奇的词是…PNG
第一个字节是 0x89,但当您将其读为小端 int
时,顺序已颠倒值,所以它是 (('G'<<24)|('N'<<16)|('P'<<8)|0x89)
.
我留给您来重组您的代码,以使用通用接口(interface)处理这两种情况......
关于java - 无法从 ico 文件中读取图像,因为宽度和高度都为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50527875/
我要求我的开发人员通过在 .htaccess 文件中添加以下行来设置网站图标的过期日期: ExpiresByType image/ico "access plus 1 years" 但是它没有过期日
除了 PNG 是一种更常见的图像格式这一事实之外,还有其他技术原因支持 favicon.png 与 favicon.ico 吗? 我支持现代浏览器,它们都支持 PNG 最喜欢的图标。 最佳答案 所有现
代码: ico 文件在 IE 中不显示。 有什么办法可以在IE中显示ico文件 最佳答案 看看: http://en.wikipedia.org/wiki/Comparison_of_web_bro
如何摆脱 nginx 中的所有以下错误。我没有 favicon.ico 2012/03/11 17:13:25 [error] 959#0: *116 open() "/usr/local/nginx
Closed. This question is off-topic。它当前不接受答案。 想要改善这个问题吗? Update the question,所以它是用于堆栈溢出的on-topic。 9年前
我如何告诉网页我不想要 ICO,这样它就不会浪费加载时间来寻找它? 如果它被省略,那么它看起来也是! 最佳答案 嗯...一个可能的技巧是将它指向本地位置: 它仍然看起来,但 127.0.0.1 是
使用 converticon.com,我将图标 png 文件转换为 ico 文件。我将以下代码放在我的 head 标签中: 我仔细检查过,文件位于适当的位置。图像具有读写能力。然后我在浏览器上加载了
my routers/default.go,我尝试使用原始的 Go 解决方案,但失败了,这段代码无法编译。我不知道如何用 faviconHandler 替换路由器: func faviconHandl
在网站中,设计师经常忘记制作favicon,而忘记制作favicon的后果就是网站可用性的下降。如果网站没有相应的favicon.ico文件,每当有用户收藏网站/网页时, Web服务器都会返回404
我有一个使用 Spring Security 3 在 Tomcat 上运行的应用程序。我没有为我的网站定义任何图标,但是当我从我的 IDE sometimes 运行我的应用程序时从我的登录页面登录后,
我如何在开发中为 favicon.ico 提供服务?我可以在我的 urlconf 中添加一个路由,但我不希望该路由延续到生产环境。有没有办法在 local_settings.py 中做到这一点? 最佳
我想要一个函数,它可以获取Windows中的任何文件路径(任何文件系统对象——文件、文件夹、驱动器、快捷方式等)并返回关联的.ICO文件(或带有所有图像的图标的一些句柄)尺寸表示)。例如,如果我在 W
位于窗口顶角的 .ico 文件的理想大小是多少? 最佳答案 简短回答:16 x 16 像素。 长答案: .ico 文件实际上可以包含多个颜色深度的图像 - 您可以在单个文件中提供 16x16、32x3
我正在制作一个 favicon.ico 脚本,我需要知道可能的最大位数。 最佳答案 这取决于您使用的颜色数量。 对于 8 位(256 色): 32 * 32 * 8 = 8192 bits 8192
情况看起来是这样的: 我在应用程序中有很多图标,它们的大小不一。 例如,我使用图标作为 DynamicResource: 部分图标为.xaml格式,部分图标为.png格式 我添加了新的图标,例如:
当我使用IIS Express在Visual Studio 2017中运行我的应用程序时,它可以在wwwroot文件夹中找到favicon.ico。 将其部署到服务器上的IIS时,找不到它。 我认为这
我想知道以下是否可行。 我有一个 .ico 文件,其中包含多种尺寸和颜色深度。但是,它还包含一些定制尺寸,将在我的应用程序中使用。 应用程序通过资源 DLL 访问图标。 (本意是DLL由第三方开发者提
如何使用 htaccess 将图像缓存在我的网站上 2 天: 1 x 60 x 60 x 24 x 2 = 172800s 所以我想缓存'png, jpeg, jpg, ico, js'。我怎样才能用
我有一个已导入到我的 Visual Studio 2010 项目中的 .ico 文件。在 .ico 文件中,看起来有许多不同大小的不同图像。 我正在尝试将其中之一设置为我的按钮控件的图标: HICON
我可以在 css 游标属性中使用 .ico 而不会产生任何后果(即完全浏览器兼容性。)还是必须使用 .cur? 如: cursor: url(img/cursor.ico); 最佳答案 cur 文件和
我是一名优秀的程序员,十分优秀!