| | |
| | | package cc.mrbird.febs.mall.test; |
| | | |
| | | import com.sun.image.codec.jpeg.JPEGCodec; |
| | | import com.sun.image.codec.jpeg.JPEGEncodeParam; |
| | | import com.sun.image.codec.jpeg.JPEGImageEncoder; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | |
| | | public class PixelateImage { |
| | | |
| | | public static void main(String[] args) { |
| | | handleDpi(new File("D:\\image\\inputDpi.png"),50,50); |
| | | getPixel("D:\\image\\input.png"); |
| | | try { |
| | | resizeImage("D:\\image\\input.png","D:\\image\\output.png",360);//将图片压缩至100宽 |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 功能:获取图片像素 |
| | | * * @param filePath 图片路径 |
| | | */ |
| | | public static void getPixel(String filePath){ |
| | | File file = new File(filePath); |
| | | BufferedImage bi = null; |
| | | try { |
| | | bi = ImageIO.read(file); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | int width = bi.getWidth(); // 像素 |
| | | int height = bi.getHeight(); // 像素 |
| | | System.out.println("width=" + width + ",height=" + height + "."); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @param inputPath 源图片路径 |
| | | * @param outputPath 修改大小后图片路径 |
| | | * @param scaleSize 图片的修改比例,目标宽度 |
| | | */ |
| | | public static void resizeImage(String inputPath, String outputPath,int scaleSize) throws IOException { |
| | | |
| | | File srcFile = new File(inputPath); |
| | | Image srcImg = ImageIO.read(srcFile); |
| | | BufferedImage bi = null; |
| | | try { |
| | | bi = ImageIO.read(srcFile); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | float width = bi.getWidth(); // 像素 |
| | | float height = bi.getHeight(); // 像素 |
| | | float scale = width/scaleSize; |
| | | BufferedImage buffImg = null; |
| | | buffImg = new BufferedImage(scaleSize, (int)(height/scale), BufferedImage.TYPE_INT_RGB); |
| | | //使用TYPE_INT_RGB修改的图片会变色 |
| | | buffImg.getGraphics().drawImage( |
| | | srcImg.getScaledInstance(scaleSize, (int)(height/scale), Image.SCALE_SMOOTH), 0, |
| | | 0, null); |
| | | |
| | | ImageIO.write(buffImg, "JPEG", new File(outputPath)); |
| | | } |
| | | |
| | | /** |
| | | * 改变图片DPI |
| | | * |
| | | * @param file |
| | | * @param xDensity |
| | | * @param yDensity |
| | | */ |
| | | public static void handleDpi(File file, int xDensity, int yDensity) { |
| | | try { |
| | | BufferedImage image = ImageIO.read(file); |
| | | JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(file)); |
| | | JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image); |
| | | jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH); |
| | | jpegEncoder.setJPEGEncodeParam(jpegEncodeParam); |
| | | jpegEncodeParam.setQuality(0.1f, false); |
| | | jpegEncodeParam.setXDensity(xDensity); |
| | | jpegEncodeParam.setYDensity(yDensity); |
| | | jpegEncoder.encode(image, jpegEncodeParam); |
| | | image.flush(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | //package cc.mrbird.febs.mall.test; |
| | | // |
| | | //import com.sun.image.codec.jpeg.JPEGCodec; |
| | | //import com.sun.image.codec.jpeg.JPEGEncodeParam; |
| | | //import com.sun.image.codec.jpeg.JPEGImageEncoder; |
| | | // |
| | | //import javax.imageio.ImageIO; |
| | | //import java.awt.*; |
| | | //import java.awt.image.BufferedImage; |
| | | //import java.io.File; |
| | | //import java.io.FileOutputStream; |
| | | //import java.io.IOException; |
| | | // |
| | | //public class PixelateImage { |
| | | // |
| | | // public static void main(String[] args) { |
| | | // handleDpi(new File("D:\\image\\inputDpi.png"),50,50); |
| | | // getPixel("D:\\image\\input.png"); |
| | | // try { |
| | | // resizeImage("D:\\image\\input.png","D:\\image\\output.png",360);//将图片压缩至100宽 |
| | | // } catch (IOException e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | // |
| | | // /** |
| | | // * 功能:获取图片像素 |
| | | // * * @param filePath 图片路径 |
| | | // */ |
| | | // public static void getPixel(String filePath){ |
| | | // File file = new File(filePath); |
| | | // BufferedImage bi = null; |
| | | // try { |
| | | // bi = ImageIO.read(file); |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // int width = bi.getWidth(); // 像素 |
| | | // int height = bi.getHeight(); // 像素 |
| | | // System.out.println("width=" + width + ",height=" + height + "."); |
| | | // } |
| | | // |
| | | // |
| | | // /** |
| | | // * @param inputPath 源图片路径 |
| | | // * @param outputPath 修改大小后图片路径 |
| | | // * @param scaleSize 图片的修改比例,目标宽度 |
| | | // */ |
| | | // public static void resizeImage(String inputPath, String outputPath,int scaleSize) throws IOException { |
| | | // |
| | | // File srcFile = new File(inputPath); |
| | | // Image srcImg = ImageIO.read(srcFile); |
| | | // BufferedImage bi = null; |
| | | // try { |
| | | // bi = ImageIO.read(srcFile); |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // float width = bi.getWidth(); // 像素 |
| | | // float height = bi.getHeight(); // 像素 |
| | | // float scale = width/scaleSize; |
| | | // BufferedImage buffImg = null; |
| | | // buffImg = new BufferedImage(scaleSize, (int)(height/scale), BufferedImage.TYPE_INT_RGB); |
| | | // //使用TYPE_INT_RGB修改的图片会变色 |
| | | // buffImg.getGraphics().drawImage( |
| | | // srcImg.getScaledInstance(scaleSize, (int)(height/scale), Image.SCALE_SMOOTH), 0, |
| | | // 0, null); |
| | | // |
| | | // ImageIO.write(buffImg, "JPEG", new File(outputPath)); |
| | | // } |
| | | // |
| | | // /** |
| | | // * 改变图片DPI |
| | | // * |
| | | // * @param file |
| | | // * @param xDensity |
| | | // * @param yDensity |
| | | // */ |
| | | // public static void handleDpi(File file, int xDensity, int yDensity) { |
| | | // try { |
| | | // BufferedImage image = ImageIO.read(file); |
| | | // JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(file)); |
| | | // JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image); |
| | | // jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH); |
| | | // jpegEncoder.setJPEGEncodeParam(jpegEncodeParam); |
| | | // jpegEncodeParam.setQuality(0.1f, false); |
| | | // jpegEncodeParam.setXDensity(xDensity); |
| | | // jpegEncodeParam.setYDensity(yDensity); |
| | | // jpegEncoder.encode(image, jpegEncodeParam); |
| | | // image.flush(); |
| | | // } catch (IOException e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | //} |