我有一个png图像,我想调整它.我怎么能这样做?虽然我已经完成了这个我无法理解的片段.
我搜索并找到了一些Java的图像缩放库.但不确定与哪一个相关.我需要从服务器上传的图像生成缩略图.
如果你能分辨哪一个好坏,那就太好了.
我的清单是:
我上传了一个带有struts表单的文件.我将图像作为byte [],我想缩放它.
FormFile file = (FormFile) dynaform.get("file");
byte[] fileData = file.getFileData();
fileData = scale(fileData,200,200);
public byte[] scale(byte[] fileData, int width, int height) {
// TODO
}
Run Code Online (Sandbox Code Playgroud)
任何人都知道一个简单的功能吗?
public byte[] scale(byte[] fileData, int width, int height) {
ByteArrayInputStream in = new ByteArrayInputStream(fileData);
try {
BufferedImage img = ImageIO.read(in);
if(height == 0) {
height = (width * img.getHeight())/ img.getWidth();
}
if(width == 0) {
width = (height * img.getWidth())/ img.getHeight();
}
Image scaledImage = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage imageBuff = new BufferedImage(width, …Run Code Online (Sandbox Code Playgroud) 减少使用iText新创建的PDF文件中JPEG图像大小的最佳做法是什么?(我的目标是在图像质量和文件大小之间进行权衡.)
图像创建如下:
Image image = new Image(ImageDataFactory.create(imagePath))
Run Code Online (Sandbox Code Playgroud)
我想提供一个比例因子,例如0.5,它将一行中的像素数减半.
假设我使用单个3 MB图像生成PDF.我试过image.scale(0.5f, 0.5f),但生成的PDF文件仍然大约3 MB.我预计它会变小.
因此,我猜想嵌入在PDF文件中的源图像不会被触及.但这就是我需要的:应该减少存储在磁盘上的整个PDF文件中的像素总数.
实现这一目标的最简单/推荐方法是什么?
我正在拼命寻找能够为大量文件类型生成缩略图的Java库.像Office Suite文档,Open Office文档,PDF,图像等...缩略图必须是可读的.
从我的理解.net有钩子调用窗口完成这个,但代码必须是平台独立的(因此java)
我找到了一些用于图像缩略图生成的库,有些用于word文档,但似乎没什么好看的.我正在寻找一个人为所有库生成缩略图是否存在?
我有以下代码:
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class JavaApplication
{
public static void main(String[] args) throws Exception
{
File orig = new File ("/home/xxx/Pictures/xxx.jpg");
BufferedImage bm1 = ImageIO.read(orig);
Image scaled = bm1.getScaledInstance(100, 200, BufferedImage.SCALE_SMOOTH);
BufferedImage bm2 = toBufferedImage(scaled);
File resized = new File ("/home/xxx/Pictures/resized.jpg");
ImageIO.write(bm2, "jpg", resized);
}
public static BufferedImage toBufferedImage(Image img)
{
if (img instanceof BufferedImage)
{
return (BufferedImage) img;
}
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
bimage.getGraphics().drawImage(img, 0, 0 , null); …Run Code Online (Sandbox Code Playgroud) 我在获得此代码InputStream并创建图像的地方有此代码:
Part file;
// more code
try {
InputStream is = file.getInputStream();
File f = new File("C:\\ImagenesAlmacen\\QR\\olaKeAse.jpg");
OutputStream os = new FileOutputStream(f);
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
is.close();
} catch (IOException e) {
System.out.println("Error");
}
Run Code Online (Sandbox Code Playgroud)
问题是,如果我从 InputStream
那么如何调整我从中获得的InputStream尺寸,然后创建该尺寸调整后的图像。我想将图像的最大面设置为180px并用该比例调整另一面的大小。
例:
图片= 289px * 206px
调整大小的图片= 180px* 128px
我正在 Java 中进行图像比较。我认为在比较图像之前,最好先处理图像以设置固定大小的图像。是否有任何 Java 功能来调整图像大小。我想将图像重新缩放为 300*225。