Java上传图片指定修改图片的宽度和长度。

一、直接上代码

package com.hrtxn.ringtone.project.system.video.util;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * 作者: yushuangyu
 * 时间: 2020年07月30日 18:32
 * 描述:
 */
public class CompressPic {

    public static void main(String arg[])
    {
        String filePath = "C:\\Users\\Administrator\\Pictures\\测试\\3\\03.png"; // 图片的位置

        int height=300;
        int width=200;
        Icon icon = null;
        try
        {
            icon = getFixedIcon(filePath,width,height);
        }
        catch(Exception e)
        {
            System.out.println("exception : " +e);
        }
        System.out.println(" ### " +icon); //生成新图片的位置;
    }

    /**
     * 按宽的比例更改图片的大小
     * @param filePath 图片路径
     * @param width 需要改变图片的宽度
     * @return
     * @throws Exception
     */
    public static Icon getRatioWidth(String filePath, int width) throws Exception{

        File f = new File(filePath);

        BufferedImage bi = ImageIO.read(f);

        double wRatio = (new Integer(width)).doubleValue() / bi.getWidth(); //宽度的比例

        int height = (int)(wRatio * bi.getHeight());  //图片转换后的高度

        Image image = bi.getScaledInstance(width,height,Image.SCALE_SMOOTH); //设置图像的缩放大小

        AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio,wRatio),null);   //设置图像的缩放比例

        image = op.filter(bi,null);

        int lastLength = filePath.lastIndexOf(".");
        String subFilePath = filePath.substring(0,lastLength);  //得到图片输出路径
        String fileType = filePath.substring(lastLength);  //图片类型
        File zoomFile = new File(subFilePath +"_"+ width +"_" + height + fileType);

        Icon ret = null;
        try
        {
            ImageIO.write((BufferedImage)image, "jpg", zoomFile);
            ret = new ImageIcon(zoomFile.getPath());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return ret;
    }
    /**
     * 按高的比例更改图片大小
     * @param filePath 图片路径
     * @param height 需要改变图片的高度
     * @return
     * @throws Exception
     */
    public static Icon getRatioHeight(String filePath, int height) throws Exception{
        File f = new File(filePath);

        BufferedImage bi = ImageIO.read(f);

        double hRatio = (new Integer(height)).doubleValue() / bi.getHeight(); //高度的比例

        int width = (int)(hRatio * bi.getWidth());  //图片转换后的高度

        Image image = bi.getScaledInstance(width,height,Image.SCALE_SMOOTH); //设置图像的缩放大小

        AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(hRatio,hRatio),null);   //设置图像的缩放比例

        image = op.filter(bi,null);

        int lastLength = filePath.lastIndexOf(".");
        String subFilePath = filePath.substring(0,lastLength);  //得到图片输出路径
        String fileType = filePath.substring(lastLength);  //图片类型
        File zoomFile = new File(subFilePath +"_"+ width +"_" + height + fileType);

        Icon ret = null;
        try
        {
            ImageIO.write((BufferedImage)image, "jpg", zoomFile);
            ret = new ImageIcon(zoomFile.getPath());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return ret;
    }

    /**
     * 按输入的任意宽高改变图片的大小
     * @param filePath
     * @param width
     * @param height
     * @return
     * @throws Exception
     */
    public static Icon getFixedIcon(String filePath, int width, int height) throws Exception{
        File f = new File(filePath);

        BufferedImage bi = ImageIO.read(f);

        double wRatio = (new Integer(width)).doubleValue() / bi.getWidth(); //宽度的比例

        double hRatio = (new Integer(height)).doubleValue() / bi.getHeight(); //高度的比例

        Image image = bi.getScaledInstance(width,height,Image.SCALE_SMOOTH); //设置图像的缩放大小

        AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio,hRatio),null);   //设置图像的缩放比例

        image = op.filter(bi,null);

        int lastLength = filePath.lastIndexOf(".");
        String subFilePath = filePath.substring(0,lastLength);  //得到图片输出路径
        String fileType = filePath.substring(lastLength);  //图片类型
        File zoomFile = new File(subFilePath +"_"+ width +"_" + height + fileType);

        Icon ret = null;
        try
        {
            ImageIO.write((BufferedImage)image, "png", zoomFile);
            ret = new ImageIcon(zoomFile.getPath());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return ret;
    }



}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中,您可以使用`BufferedImage`类和``类来修改图片中的指定颜色。下面是一个简单的示例代码,演示如何在Java修改图片中的指定颜色: ```java import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ModifyImageColor { public static void main(String[] args) { try { // 读取原始图片 BufferedImage image = ImageIO.read(new File("input.jpg")); // 定义目标颜色和替换颜色 Color targetColor = Color.RED; Color replaceColor = Color.GREEN; // 获取图片宽度和高度 int width = image.getWidth(); int height = image.getHeight(); // 遍历图片像素 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // 获取当前像素的颜色 Color pixelColor = new Color(image.getRGB(x, y)); // 判断当前像素的颜色是否为目标颜色 if (pixelColor.equals(targetColor)) { // 替换为指定颜色 image.setRGB(x, y, replaceColor.getRGB()); } } } // 保存修改后的图片 ImageIO.write(image, "jpg", new File("output.jpg")); System.out.println("图片颜色修改成功!"); } catch (Exception e) { System.out.println("发生异常:" + e.getMessage()); e.printStackTrace(); } } } ``` 在上述示例中,我们首先使用`ImageIO.read()`方法读取原始图片。然后,我们定义了目标颜色`targetColor`和替换颜色`replaceColor`。 接下来,我们获取图片宽度和高度,并使用嵌套循环遍历每个像素。对于每个像素,我们使用`image.getRGB()`方法获取其颜色,并使用`equals()`方法判断是否为目标颜色。如果是目标颜色,则使用`image.setRGB()`方法将其替换为指定的替换颜色。 最后,我们使用`ImageIO.write()`方法将修改后的图片保存为新的文件。 请注意,上述示例中只演示了如何替换一个指定颜色。如果您需要更复杂的颜色替换操作,可能需要使用其他算法和图像处理技术。 希望以上信息对您有所帮助!如果有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

于双瑜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值