最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java圖片批量壓縮像素的實現(xiàn)方法

 更新時間:2021年12月09日 11:51:33   作者:java李楊勇  
我們開發(fā)中經常會遇到原圖清晰度高,考慮到效率問題,我們不可能拿原圖進行顯示,服務端一般都要對圖片進行壓縮處理,然后發(fā)送給客戶端顯示,這篇文章主要給大家介紹了關于Java圖片批量壓縮像素的實現(xiàn)方法,需要的朋友可以參考下

圖片壓縮大法

為了防止用戶流量的丟失,即使在5g 即將來臨的情況下,壓縮算法依舊是很有必要的,額跑題了,不好意思,今天介紹的不是壓縮算法,講啥呢?主要講講如何通過 java 將圖片進行壓縮,盡可能的控制壓縮損比,不僅僅是為了減少存儲,其目的是快速呈現(xiàn)給用戶,只有良好的體驗,才會在當今這個急躁的年代減少流量的損失。

最近因為公司要需要xxx認證上傳測試用例功能的具體截圖、發(fā)現(xiàn)有大小限制、所以就進行了圖片壓縮,簡單記錄一下。

壓縮前大?。?/h2>

壓縮后大?。?/h2>

具體代碼實現(xiàn):

main方法測試:

 public static void main(String[] args) throws IOException {

        String modpath = "C:\\Users\\Administrator\\Desktop\\鯤鵬認證\\test\\";

        getFiles("C:\\Users\\Administrator\\Desktop\\鯤鵬認證\\測試用例清單", modpath, 160);//將圖片壓縮至100寬

    }

文件大小處理

/**

     * @param srcPath 原圖片路徑

     * @param desPath 轉換大小后圖片路徑

     * @param width   轉換后圖片寬度

     * @param height  轉換后圖片高度

     */

    public static void resizeImage(String srcPath, String desPath, int width, int height) throws IOException {

        File srcFile = new File(srcPath);

        Image srcImg = ImageIO.read(srcFile);

        BufferedImage buffImg = null;

        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        //使用TYPE_INT_RGB修改的圖片會變色

        buffImg.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

        String filePath="";

        if (srcFile.getName().contains("#")) {

             filePath = srcFile.getName().replace("#", "");

        }else{

            filePath=srcFile.getName();

        }

        ImageIO.write(buffImg, "PNG", new File(desPath + filePath));

    }

獲取目錄文件信息

/**

     * @param scaleSize 圖片的修改比例,目標寬度

     */

    public static void getFiles(String path, String modPath, int scaleSize) throws IOException {

        ArrayList<String> files = new ArrayList<String>();

        File file = new File(path);

        File[] tempList = file.listFiles();

        //循環(huán)讀取目錄下圖片

        for (int i = 0; i < tempList.length; i++) {

            String filePath = tempList[i].getName();

            if (tempList[i].isFile()) {

                System.out.println("文件:" + filePath + "-" + tempList[i].getAbsolutePath().replaceAll("\\\\", "/"));

                String[] imagePath = tempList[i].getAbsolutePath().replaceAll("\\\\", "/").split("/");

                String imageNumber = null;

                FileUtil.resizeImage(tempList[i].getAbsolutePath().replaceAll("\\\\", "/"), modPath, 160, 160);

                files.add(tempList[i].toString());

            }

            if (tempList[i].isDirectory()) {

                  System.out.println("文件夾:" + tempList[i]);

            }

        }

        System.out.println(path + "下文件數量:" + files.size());

    }

控制臺目錄壓縮成功保存到盤符:

附:利用Graphics類如何進行壓縮圖像

Graphics類提供基本繪圖方法,Graphics類提供基本的幾何圖形繪制方法,主要有:畫線段、畫矩形、畫圓、畫帶顏色的圖形、畫橢圓、畫圓弧、畫多邊形、畫字符串等。 這里不做一一贅述, 進重點介紹一下,利用Graphics類如何進行壓縮圖像。不多說直接上代碼。

	/**

	 * compressImage

	 * 

	 * @param imageByte

	 *            Image source array

	 * @param ppi

	 * @return

	 */

	public static byte[] compressImage(byte[] imageByte, int ppi) {

		byte[] smallImage = null;

		int width = 0, height = 0;

 

		if (imageByte == null)

			return null;

 

		ByteArrayInputStream byteInput = new ByteArrayInputStream(imageByte);

		try {

			Image image = ImageIO.read(byteInput);

			int w = image.getWidth(null);

			int h = image.getHeight(null);

			// adjust weight and height to avoid image distortion

			double scale = 0;

			scale = Math.min((float) ppi / w, (float) ppi / h);

			width = (int) (w * scale);

			width -= width % 4;

			height = (int) (h * scale);

 

			if (scale >= (double) 1)

				return imageByte;

 

			BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

			buffImg.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

			ByteArrayOutputStream out = new ByteArrayOutputStream();

			ImageIO.write(buffImg, "png", out);

			smallImage = out.toByteArray();

			return smallImage;

 

		} catch (IOException e) {

			log.error(e.getMessage());

			throw new RSServerInternalException("");

		}

	}

其實,關鍵點就兩處

BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

buffImg.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

總結

到此這篇關于Java圖片批量壓縮像素實現(xiàn)的文章就介紹到這了,更多相關Java圖片批量壓縮像素內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

越西县| 于都县| 资源县| 全椒县| 湾仔区| 开封市| 西和县| 莱州市| 小金县| 玉门市| 调兵山市| 博湖县| 牡丹江市| 武汉市| 阿尔山市| 沈丘县| 四子王旗| 景谷| 曲靖市| 乃东县| 方正县| 庆阳市| 开平市| 天全县| 虎林市| 乐业县| 建阳市| 游戏| 东台市| 永清县| 乐都县| 桐梓县| 井冈山市| 东乡族自治县| 无极县| 筠连县| 天台县| 会宁县| 沙洋县| 高平市| 宾川县|