我正在尝试读取图像,将其放大到 80*60,然后通过双线性插值方法将结果图像缩小 5 倍。但是我收到此错误:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 4800 。任何人都可以帮助我吗?
这就是我所做的:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class BiInterpolationTest {
public static int zh;
public static int zw;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int[][] savedImage;
File f = new File ("F:\\Java\\Gray Scale Images\\3.jpg");
savedImage = readimage(f);
BufferedImage grayImage = new BufferedImage(savedImage.length, savedImage[0].length, BufferedImage.TYPE_BYTE_GRAY);
for (int i =0 ; i<savedImage.length ; i ++){
for (int j=0 ; j<savedImage[0].length ; j++){
int rgb = savedImage[i][j];
rgb = (rgb<<16)|(rgb<<8)|(rgb);
grayImage.setRGB(i, j, rgb);
BufferedImage zoomin =ScaledImage(grayImage, 80,60);
zh = zoomin.getHeight();
zw = zoomin.getWidth();
byte[] tempArr;
tempArr = extractBytes(zoomin);
// TODO Auto-generated catch block
byte [] zoomout;
zoomout = bilineraInterpolation(tempArr , 5);
InputStream in = new ByteArrayInputStream(zoomout);
BufferedImage bImageInterpolated;
try {
bImageInterpolated = ImageIO.read(in);
ImageIO.write(bImageInterpolated, "jpg", new File(
"F:/new-darksouls.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static BufferedImage ScaledImage(Image img, int w , int h){
BufferedImage resizedImage = new BufferedImage(w , h , BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2 = resizedImage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img, 0, 0, w, h, null);
g2.dispose();
return resizedImage;
}
/////////////////////////////////////
public static byte[] extractBytes (BufferedImage Image) throws IOException {
BufferedImage bufferedImage = new BufferedImage(Image.getHeight(), Image.getWidth(), BufferedImage.TYPE_BYTE_GRAY);
// get DataBufferBytes from Raster
WritableRaster raster = bufferedImage .getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
return ( data.getData() );
}
public static int[][] readimage(File filename){
BufferedImage img;
try {
img = ImageIO.read(filename);
// Gray_scaled Image output
int width = img.getWidth();
int height = img.getHeight();
ImagePro.fw=width;
ImagePro.fh = height;
int [][] readimageVal = new int [width][height];
for (int i = 0; i<height ; i++){
for (int j =0 ; j<width ; j++){
Color c = new Color(img.getRGB(j, i));
int r= (int)(c.getRed() * 0.299)&0xff;
int g = (int)(c.getGreen() * 0.587)&0xff;
int b = (int)(c.getBlue() *0.114)&0xff;
int avg = ((r+b+g));
readimageVal[j][i] = avg;
}
}
return readimageVal;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static byte[] bilineraInterpolation(byte[] ImgData, int ratio) {
int wf = zw*ratio;
int hf = zh*ratio;
byte[] tempArr = new byte[wf*hf] ;
int A, B, C, D, x, y, index, g ;
float x_ratio = ((float)(zw))/wf ;
float y_ratio = ((float)(zh))/hf ;
float x_diff, y_diff ;
int in = 0 ;
for (int i=0;i<hf;i++) {
for (int j=0;j<wf;j++) {
x = (int)(x_ratio * j) ;
y = (int)(y_ratio * i) ;
x_diff = (x_ratio * j) - x ;
y_diff = (y_ratio * i) - y ;
index = y*zw+x ;
// range is 0 to 255 thus bitwise AND with 0xff
A = ImgData[index] & 0xff ;
B = ImgData[index+1] & 0xff ;
C = ImgData[index+zw] & 0xff ;
D = ImgData[index+zw+1] & 0xff ;
g = (int)(
A*(1-x_diff)*(1-y_diff) + B*(x_diff)*(1-y_diff) +
C*(y_diff)*(1-x_diff) + D*(x_diff*y_diff)
) ;
tempArr[in++] = (byte) g ;
}
}
return tempArr ;
}
}