[英]Shrink an image to half its height and width in java
我正在嘗試將圖像縮小到其高度和寬度的一半。 到目前為止,這就是我所擁有的。 我不知道從那里去哪里。 一種實現方法是簡單地將原始圖像中的一組像素替換為新縮小圖像中的單個像素,該像素是原始組中整個像素的平均顏色。 我還可以創建一個新數組,其高度和寬度是作為參數傳入的圖像的高度和寬度的一半。 然后,在確定顏色值時,將新像素插入新圖像。
public class ImageManipulation
{
public static void main(String[] args) throws FileNotFoundException
{
Pixel[][] image = readImage("griff.ppm");
flipVertical(image);
writeImage(image,"manipulatedImage.ppm");
}
public static void grayscale(Pixel[][] imageArr)
{
int height = imageArr.length;
int width = imageArr[0].length;
for(int row = 0; row < height; row++)
{
for(int col = 0; col < width; col++)
{
Pixel p = imageArr[row][col];
int grayValue = (p.getRed() + p.getBlue() + p.getGreen())/3;
p.setBlue(grayValue);
p.setGreen(grayValue);
p.setRed(grayValue);
imageArr[row][col] = p;
}
}
}
public static void shrink (Pixel[][] imageArr)
{
int height = imageArr.length/2;
int width = imageArr[0].length/2;
不,您不需要自己編寫所有代碼:)
public BufferedImage shrink(File source, int w, int h) {
int dstWidth = w / 2;
int dstHeight = h / 2;
BufferedImage originalImage = ImageIO.read(source);
BufferedImage resizedImage = new BufferedImage(
dstWidth
, dstHeight
, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, dstWidth, dstHeight, null);
g.dispose();
return resizedImage;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.