- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的导师向我发送了这段代码,它应该是正在进行的项目的组成部分。问题是,它不起作用。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.Kernel;
import java.awt.image.ConvolveOp;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import javax.swing.*;
public class ImageUtil {
public static void resize(File originalFile, File resizedFile, int newWidth, float quality) throws IOException{
if(quality <0||quality>1){
throw new IllegalArgumentException ("quality has to be between 0 and 1");
}
ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null;
int iWidth = i.getWidth (null);
int iHeight = i.getHeight(null);
if (iWidth > iHeight){
resizedImage = i.getScaledInstance(newWidth,(newWidth*iHeight)/iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth*iWidth)/iHeight,newWidth, Image.SCALE_SMOOTH);
}
Image temp = new ImageIcon (resizedImage).getImage();
BufferedImage bufferedImage = new BufferedImage (temp.getWidth(null), temp.getHeight(null),
BufferedImage.TYPE_INT_RGB);
// copy to a buffered image
Graphics g = bufferedImage.createGraphics();
g.setColor(Color.white);
g.fillRect(0,0,temp.getWidth(null), temp.getHeight(null)); // causea el error de ejecución?
g.drawImage(temp,0,0,null);
g.dispose();
float softenFactor = 0.05f;
float [] softenArray = {0, softenFactor,0, softenFactor, 1-(softenFactor*4), softenFactor,0, softenFactor,0};
Kernel kernel = new Kernel (3,3,softenArray);
ConvolveOp cOp = new ConvolveOp (kernel, ConvolveOp.EDGE_NO_OP,null);
bufferedImage = cOp.filter (bufferedImage,null);
FileOutputStream out = new FileOutputStream(resizedFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
param.setQuality(quality,true);
encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);
}
public static void main(String args[]) throws IOException {
File originalImage= new File ("/images/goya.jpg");
resize(originalImage,new File("/images/goyanuevotamanio.jpg"),350, 0.2f);
resize(originalImage, new File("/images/goyanuevotamanio.jpg"), 350, 1f);
}
}
他说代码应该能够调整高度和宽度,但他的调整大小方法:
public static void resize(File originalFile, File resizedFile, int newWidth, float quality)
缺少 newHeight
参数。
抛出的 RuntimeException 表示:
Exception in thread "main" java.lang.IllegalArgumentException: **Width (-1) and height (-1) cannot be <= 0**
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:999)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:312)
at ImageUtil.resize(ImageUtil.java:38)
at ImageUtil.main(ImageUtil.java:72)
Java Result: 1
如何更改此设置?
最佳答案
在不分析代码来查看问题所在的情况下,我可以说,如果目的是保持图像的宽高比,则不需要新的高度参数。它始终是宽度的固定比例。
更新:
我已经针对我自己的测试图像运行了代码,它确实按预期工作。当程序找不到您指定的源图像时,很可能会出现您收到的错误。当我指定不存在的源图像时,我收到相同的错误。
尝试对源图像使用绝对路径(包括驱动器号等)。
更新2
在该行后添加以下代码..
ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
if(ii.getImageLoadStatus() == MediaTracker.COMPLETE)
{
System.out.print("Load image ok\n");
}
else
{
System.out.print("Load image failed\n");
}
这将告诉您程序是否无法加载源图像。
关于java - 使图像 resize() 的代码起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/764794/
我有一个 View ,不是窗口的大小,也不是窗口本身,当它调整大小时,我想比较调整大小的开始值和结束值。然而,JQ-UI 的 resize ui 对象只包含以前的状态,而不是原始状态,所以它只是按像素
我有一个简单的可调整大小 jQueryUI 元素。 element.resizable({ start: function(event, ui) {...}, resize: func
我有几个嵌套的可调整大小的 div,需要调整大小以适应子大小。一切正常,直到我尝试调整其中一个外部 div 的大小(垂直)然后它卡住了。 这是一个 fiddle . $(function () { $
CNN 模型将大小为 (112x112) 的图像张量作为输入,并给出 (1x512) 大小的张量作为输出。 使用 Opencv 函数 cv2.resize() 或在 pytorch 中使用 Trans
经过大量搜索和研究,我找不到任何答案。 我将非常感谢可以帮助我的人。 在 SWT Java 中, 我希望我的窗口不能调整大小,所以我这样定义它: shell = new Shell(SWT.CLOSE
$(window).bind('resize') 和 $(window).resize() 有什么区别? 我看到bind嵌套在resize之上。它有什么影响?请参阅下面的代码。 我知道.resize(
使用 Vaadin 14.1.19 和 Vaadin“我的入门元素”元素,我尝试创建一个多行 TextArea。乍一看它工作正常,但是当调整 TextArea 大小时,它不会调整行数。这是我的代码:
我有一个使用 Ext.js 的侧面板。在浏览器调整大小之前,侧面板中的其中一个面板很好。浏览器调整大小时,它会切断面板的组件。 如何在浏览器调整大小时调整面板大小? { xtype:
我需要一种可靠的方法来检测 $(window).on('resize') 是否已由用户交互或 jQuery .trigger 调用触发。 我尝试了以下方法,但我似乎没有在函数调用中取回参数: $(wi
我有一个可调整大小的 div,现在我想在调整此框大小/用户完成调整大小时编辑 cookie。 (我有cookie插件) 我该怎么做? PS:我有多个具有不同 id 的 .div。 我的代码不起作用
这些游标类型有什么区别?我在 Chrome/windows 中看到了所有 3 个相同的光标 .e-resize {cursor: e-resize;} .ew-resize {cursor: ew-r
我使用了一个指令,它调用了一个返回一些值的方法,该方法运行良好。但我的问题是它反复调用它,而我想要的是它应该只在调整大小完成时调用该方法。我采取了这种代码形式 https://codepen.io/f
为了检查这种意外行为,我只是将 TextArea 直接放入 PrimaryStage 包含的 Scene 中:在应用程序启动时,TextArea 完全适合窗口(正如预期的那样)。 但是如果我移动窗口的
我在我的项目中使用 jquery-ui-resizable 插件。 默认情况下,当您制作 DOM 对象 jquery-ui-resizable 时,可调整大小的句柄将出现在右下角,我需要在左下角显示可
我正在 MonoTouch 中使用来自 AV Foundation 的 AVPlayer API 开发视频播放器(但 objective-c 中的解决方案也可能不错)。我正在尝试实现全屏模式。 为了显
我只是想让我的响应式 div 示例像这里一样工作 http://voormedia.com/blog/2012/11/responsive-background-images-with-fixed-o
我正在制作一个包含一个 .html 文件和一个 .css 文件的页面。 我已将 bootstrap 4 CDN 合并到我的 html 头中。 网格在中等屏幕尺寸下显示正确,但在我的手机上查看时它没有调
所以我有一个最近出现的非常具体的问题(就在我们计划的明天发布日之前),我不确定如何解决它。我用我有限的前端技能构建了我们的 HTML 模板网站,我们对此非常满意。但是,我似乎无法解决这个问题。 问题:
据我了解,有类instance.method(parameters)=class.method(instance,parameters),所以只是记法上的区别。但是 np.resize(ndarray
window.resize() 和 window.on('resize' , function()) 有什么区别在 jquery 中? 最佳答案 来自 jQuery页面.resize(): This
我是一名优秀的程序员,十分优秀!