当前位置: 编程技术>移动开发
解析:android 如何从JPEG生成BufferedImage
来源: 互联网 发布时间:2014-10-19
本文导语: 如下所示: 代码如下:private void readImage(String filename) throws FileNotFoundException, IOException { FileInputStream fIn = new FileInputStream(filename);//需要替换这边的两句: JPEGImageDecoder jpeg_decode = JPEGCodec.createJPEGDecoder(fIn); ...
如下所示:
private void readImage(String filename) throws FileNotFoundException, IOException {
FileInputStream fIn = new FileInputStream(filename);
//需要替换这边的两句:
JPEGImageDecoder jpeg_decode = JPEGCodec.createJPEGDecoder(fIn);
BufferedImage image = jpeg_decode.decodeAsBufferedImage();
width = image.getWidth();
height = image.getHeight();
int[] rgbdata = new int[width * height];
image.getRGB(0,0,width,height,rgbdata,0,width);
bytes = new byte[rgbdata.length];
doubles = new double[rgbdata.length];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) (rgbdata[i] & 0xFF);
doubles[i] = (double)(rgbdata[i]);
}
}
代码如下:
private void readImage(String filename) throws FileNotFoundException, IOException {
FileInputStream fIn = new FileInputStream(filename);
//需要替换这边的两句:
JPEGImageDecoder jpeg_decode = JPEGCodec.createJPEGDecoder(fIn);
BufferedImage image = jpeg_decode.decodeAsBufferedImage();
width = image.getWidth();
height = image.getHeight();
int[] rgbdata = new int[width * height];
image.getRGB(0,0,width,height,rgbdata,0,width);
bytes = new byte[rgbdata.length];
doubles = new double[rgbdata.length];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) (rgbdata[i] & 0xFF);
doubles[i] = (double)(rgbdata[i]);
}
}