Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 缓冲区图像大小调整_Java_Bufferedimage - Fatal编程技术网

Java 缓冲区图像大小调整

Java 缓冲区图像大小调整,java,bufferedimage,Java,Bufferedimage,我正在尝试调整BuffereImage的大小。我能够存储它并显示在jframe上没有问题,但我似乎无法调整它的大小。任何关于我如何改变它以使其工作并将图像显示为200*200文件的提示都将非常好 private void profPic(){ String path = factory.getString("bottle"); BufferedImage img = ImageIO.read(new File(path)); } public static BufferedI

我正在尝试调整BuffereImage的大小。我能够存储它并显示在jframe上没有问题,但我似乎无法调整它的大小。任何关于我如何改变它以使其工作并将图像显示为200*200文件的提示都将非常好

private void profPic(){
    String path = factory.getString("bottle");
    BufferedImage img = ImageIO.read(new File(path));
}


public static BufferedImage resize(BufferedImage img, int newW, int newH) {  
    int w = img.getWidth();  
    int h = img.getHeight();  
    BufferedImage dimg = new BufferedImage(newW, newH, img.getType());  
    Graphics2D g = dimg.createGraphics();  
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
    RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
    g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);  
    g.dispose();  
    return dimg;  
}  
最新答案

我记不起为什么有效,但我同意,在单独的环境中测试后,最初接受的答案不起作用(为什么我说有效,我也记不起来)。另一方面,这确实起了作用:

public static BufferedImage resize(BufferedImage img, int newW, int newH) { 
    Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
    BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = dimg.createGraphics();
    g2d.drawImage(tmp, 0, 0, null);
    g2d.dispose();

    return dimg;
}  

如果只需在
resize
方法中调整
buffereImage
的大小,则库可以相当轻松地执行此操作:

public static BufferedImage resize(BufferedImage img, int newW, int newH) {
  return Thumbnails.of(img).size(newW, newH).asBufferedImage();
}
上述代码将调整
img
的大小,以适应
newW
newH
的尺寸,同时保持原始图像的纵横比

如果不需要保持纵横比,并且需要精确调整尺寸至给定尺寸,则可以使用
forceSize
方法代替
size
方法:

public static BufferedImage resize(BufferedImage img, int newW, int newH) {
  return Thumbnails.of(img).forceSize(newW, newH).asBufferedImage();
}

使用该方法将不能保证对于调整大小的图像,原始图像的纵横比将保持不变,而且,它通常非常缓慢

Thumbnailator使用一种技术来逐步调整图像的大小,这种技术可以在获得通常具有可比性的图像质量的同时进行调整


免责声明:我是此库的维护者。

此类根据文件调整大小并获取格式名称:

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import org.apache.commons.io.IOUtils;

public class ImageResizer {

public static void main(String as[]) throws IOException{

    File f = new File("C:/Users/samsungrob/Desktop/shuttle.jpg");

    byte[] ba = resize(f, 600, 600);

    IOUtils.write(ba, new FileOutputStream( new File("C:/Users/samsungrob/Desktop/shuttle_resized.jpg") ) );

}




public static byte[] resize(File file,
                            int maxWidth, int maxHeight) throws IOException{
    int scaledWidth = 0, scaledHeight = 0;

    BufferedImage img = ImageIO.read((ImageInputStream) file );

    scaledWidth = maxWidth;
    scaledHeight = (int) (img.getHeight() * ( (double) scaledWidth / img.getWidth() ));

    if (scaledHeight> maxHeight) {
        scaledHeight = maxHeight;
        scaledWidth= (int) (img.getWidth() * ( (double) scaledHeight/ img.getHeight() ));

        if (scaledWidth > maxWidth) {
            scaledWidth = maxWidth;
            scaledHeight = maxHeight;
        }
    }

    Image resized =  img.getScaledInstance( scaledWidth, scaledHeight, Image.SCALE_SMOOTH);

    BufferedImage buffered = new BufferedImage(scaledWidth, scaledHeight, Image.SCALE_REPLICATE);

    buffered.getGraphics().drawImage(resized, 0, 0 , null);

    String formatName = getFormatName( file ) ;

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    ImageIO.write(buffered,
            formatName,
            out);

    return out.toByteArray();
}


private static String getFormatName(ImageInputStream iis) {
    try { 

        // 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) {
    }

    return null;
}

private static String getFormatName(File file) throws IOException {
    return getFormatName( ImageIO.createImageInputStream(file) );
}

private static String getFormatName(InputStream is) throws IOException {
    return getFormatName( ImageIO.createImageInputStream(is) );
}

}

尝试imgscalr库。我找到的最好的库-非常快,质量好,使用简单

BufferedImage thumbnail = Scalr.resize(image, 150);
不推荐的链接:

Apache2许可证


看看这个,它有助于:

BufferedImage bImage = ImageIO.read(new File(C:\image.jpg);

BufferedImage thumbnail = Scalr.resize(bImage,  Scalr.Method.SPEED, Scalr.Mode.FIT_TO_WIDTH,
                                       750, 150, Scalr.OP_ANTIALIAS);

下面是一些我用来调整BuffereImage大小的代码,没有虚饰,非常快:

public static BufferedImage scale(BufferedImage src, int w, int h)
{
    BufferedImage img = 
            new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    int x, y;
    int ww = src.getWidth();
    int hh = src.getHeight();
    int[] ys = new int[h];
    for (y = 0; y < h; y++)
        ys[y] = y * hh / h;
    for (x = 0; x < w; x++) {
        int newX = x * ww / w;
        for (y = 0; y < h; y++) {
            int col = src.getRGB(newX, ys[y]);
            img.setRGB(x, y, col);
        }
    }
    return img;
}
publicstaticbufferedimage比例(BufferedImage src,intw,inth)
{
BuffereImage img=
新的BuffereImage(w,h,BuffereImage.TYPE_INT_RGB);
int x,y;
int ww=src.getWidth();
int hh=src.getHeight();
int[]ys=新的int[h];
对于(y=0;y
如果您只想使用“平衡”平滑,这是imgscalr中实际发生情况的简化版本:

/**
 * Takes a BufferedImage and resizes it according to the provided targetSize
 *
 * @param src the source BufferedImage
 * @param targetSize maximum height (if portrait) or width (if landscape)
 * @return a resized version of the provided BufferedImage
 */
private BufferedImage resize(BufferedImage src, int targetSize) {
    if (targetSize <= 0) {
        return src; //this can't be resized
    }
    int targetWidth = targetSize;
    int targetHeight = targetSize;
    float ratio = ((float) src.getHeight() / (float) src.getWidth());
    if (ratio <= 1) { //square or landscape-oriented image
        targetHeight = (int) Math.ceil((float) targetWidth * ratio);
    } else { //portrait image
        targetWidth = Math.round((float) targetHeight / ratio);
    }
    BufferedImage bi = new BufferedImage(targetWidth, targetHeight, src.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bi.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); //produces a balanced resizing (fast and decent quality)
    g2d.drawImage(src, 0, 0, targetWidth, targetHeight, null);
    g2d.dispose();
    return bi;
}
/**
*获取BuffereImage并根据提供的targetSize调整其大小
*
*@param src源缓冲区映像
*@param targetSize最大高度(如果为纵向)或宽度(如果为横向)
*@返回所提供BuffereImage的已调整大小的版本
*/
专用BuffereImage resize(BuffereImage src,int targetSize){

如果(未编译的targetSize-getScaledInstance返回的图像不是BuffereImage。仍然不安全-在我的Macbook上,例如,我得到一个ClassCastException-这不是一个保证的转换。@I82很多写一次,在任何地方运行…啊哈。为什么这在不起作用时被列为正确答案。为什么
键入\u INT\u ARGB
而不是
img.getType()
?谢谢!非常好用!这个链接可以连接到各种可疑的互联网位置。答案很好。;)我会更新我的答案,但你的评论给了我太多的乐趣^^^^ Thumbnailator真棒。!很棒的方法,但比例不正确,当我将高度因子除以2的平方根时,效果很好。=)只需补充一下,这是Imgscalr库->