假设我有一个144x144像素的图像,其ppi(每英寸像素)为144。所以我的图像实际上是一个1x1英寸的正方形。现在,我想将其像素密度降低到72 ppi,但仍保留其1x1平方英寸的物理尺寸。为了实现这一点,现在有必要使图像的像素大小为72x72 px。我想仅通过以下可用输入来实现此目的:


图片
目标ppi


这非常类似于在ImageMagick中执行的操作,如下所示

convert -units PixelsPerInch original_144by144.jpg -resample resampled_72 72by72.jpg


上面的ImageMagick命令在进行重新采样时在内部保留了图像的物理尺寸。

我想用Java做到这一点。有什么建议吗?

最佳答案

我只是想让人们意识到,每英寸像素数(PPI)与每英寸点数(DPI)不同。即使这些术语经常用于指代同一事物,但它们还是不同的Read here for more insight

我个人只是通过Java应用程序使用ImageMagick(免费和open source),因为ImageMagick可以很好地用作命令行应用程序,例如:

String imageMagickLocation = "D:\\ImageMagick-7.0.8-Q16\\magick.exe";
String sourceImagePath = "C:\\Users\\DevilsHnd\\Pictures\\MyImage.png";
String destinationPath = "C:\\Users\\DevilsHnd\\Pictures\\New_MyImage.png";
int desiredPPI = 72;
String commandLineString = imageMagickLocation + " convert -units PixelsPerInch \""
                         + sourceImagePath + "\" -resample " + desiredPPI + " \""
                         + destinationPath + "\"";

List<String> list = runCMD(commandLineString);

/* Display any results from the call to the runCMD()
   method. If ImageMagick is successful then there
   should be nothing (the List should be empty).  */
if (!list.isEmpty()) {
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }
}


下面提供的runCMD()方法允许您的应用程序像通过Windows“命令提示符”窗口那样运行命令行应用程序:

/**
 * This method is specifically designed for running the Microsoft Windows CMD
 * command prompt and having the results that would normally be displayed within
 * a Command Prompt Window placed into a string List Interface instead.<br><br>
 * <p>
 * <b>Example Usage:</b><pre>
 *       {@code
 *          List<String> list = runCMD("/C dir");
 *          for (int i = 0; i < list.size(); i++) {
 *              System.out.println(list.get(i));
 *          }
 *       } </pre>
 *
 * @param commandString (String) The command string to pass to the Command
 *                      Prompt. You do not need to place "cmd" within your
 *                      command string because it is applied automatically.
 *                      As a matter of fact if you do it is automatically
 *                      removed.<br>
 *
 * @return (List&lt;String&gt;) A string List containing the results of
 *         the processed command.
 */
public List<String> runCMD(String commandString) {
    if (commandString.toLowerCase().startsWith("cmd ")) {
        commandString = commandString.substring(4);
    }
    List<String> result = new ArrayList<>();
    try {
        Process p = Runtime.getRuntime().exec("cmd /C " + commandString);
        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(p.getInputStream()))) {
            String line;
            while ((line = in.readLine()) != null) {
                result.add(line);
            }
        }
        p.destroy(); // Kill the process
        return result;
    }
    catch (IOException e) {
        JOptionPane.showMessageDialog(null, "<html>IO Error during processing of runCMD()!"
                                    + "<br><br>" + e.getMessage() + "</html>",
                                      "runCMD() Method Error", JOptionPane.WARNING_MESSAGE);
        return null;
    }
}


使用ImageMagick将图像转换为每英寸72像素(PPI)的速度相对较快,但是根据差异,将PPI设置为高于原始源PPI可能需要更长的时间。不管您做什么,都不要做疯狂的事情,例如将图像转换为2000 PPI,除非您拥有拥有大量内存的超级计算机(ImageMagick会尝试这样做)。事实上,您可能希望安装防护措施以防止转换不合理的PPI值。

ImageMagick的典型命令行操作是对特定图像进行重新采样以说72 PPI,这将是:

D:\ImagMagick\magick.exe convert -units PixelsPerInch "C:\Pictures\MyImageName1.png" -resample 72 "C:\Pictures\MyImageName2.png"

                            O R

D:\ImagMagick\magick.exe convert -units PixelsPerInch "C:\Pictures\MyImageName1.png" -resample 72 "C:\Pictures\MyImageName2.jpg"


注意两个转换调用之间目标文件扩展名的更改。源文件路径和目标文件路径和/或文件名都用引号引起来,以防万一它们中包含一个或多个空格。

另外,您可以轻松地修改内容以执行批处理Image文件转换。我相信ImageMagick也通过命令行具有批处理功能。

关于java - 如何在Java中重新采样图像以保持其物理尺寸?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56833107/

10-13 02:21