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

Java實現(xiàn)圖片切割功能

 更新時間:2022年01月20日 15:20:48   作者:天瞳月  
這篇文章主要為大家詳細介紹了Java實現(xiàn)圖片切割功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java實現(xiàn)圖片切割功能的具體代碼,供大家參考,具體內容如下

工具類

package com.xudaolong.Utils;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;


/**
?* 圖像裁剪以及壓縮處理工具類
?* <p>
?* 主要針對動態(tài)的GIF格式圖片裁剪之后,只出現(xiàn)一幀動態(tài)效果的現(xiàn)象提供解決方案
?* <p>
?* 提供依賴三方包解決方案(針對GIF格式數(shù)據(jù)特征一一解析,進行編碼解碼操作)
?* 提供基于JDK Image I/O 的解決方案(JDK探索失敗)
?*/
public class ImageCutterUtil {

? ? public enum IMAGE_FORMAT {
? ? ? ? BMP("bmp"),
? ? ? ? JPG("jpg"),
? ? ? ? WBMP("wbmp"),
? ? ? ? JPEG("jpeg"),
? ? ? ? PNG("png"),
? ? ? ? GIF("gif");

? ? ? ? private String value;

? ? ? ? IMAGE_FORMAT(String value) {
? ? ? ? ? ? this.value = value;
? ? ? ? }

? ? ? ? public String getValue() {
? ? ? ? ? ? return value;
? ? ? ? }

? ? ? ? public void setValue(String value) {
? ? ? ? ? ? this.value = value;
? ? ? ? }
? ? }


? ? /**
? ? ?* 獲取圖片格式
? ? ?*
? ? ?* @param file 圖片文件
? ? ?* @return 圖片格式
? ? ?*/
? ? public static String getImageFormatName(File file) throws IOException {
? ? ? ? String formatName = null;

? ? ? ? ImageInputStream iis = ImageIO.createImageInputStream(file);
? ? ? ? Iterator<ImageReader> imageReader = ImageIO.getImageReaders(iis);
? ? ? ? if (imageReader.hasNext()) {
? ? ? ? ? ? ImageReader reader = imageReader.next();
? ? ? ? ? ? formatName = reader.getFormatName();
? ? ? ? }

? ? ? ? return formatName;
? ? }

? ? /*********************** 基于JDK 解決方案 ? ? ********************************/

? ? /**
? ? ?* 讀取圖片
? ? ?*
? ? ?* @param file 圖片文件
? ? ?* @return 圖片數(shù)據(jù)
? ? ?* @throws IOException
? ? ?*/
? ? public static BufferedImage[] readerImage(File file) throws IOException {
? ? ? ? BufferedImage sourceImage = ImageIO.read(file);
? ? ? ? BufferedImage[] images = null;
? ? ? ? ImageInputStream iis = ImageIO.createImageInputStream(file);
? ? ? ? Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(iis);
? ? ? ? if (imageReaders.hasNext()) {
? ? ? ? ? ? ImageReader reader = imageReaders.next();
? ? ? ? ? ? reader.setInput(iis);
? ? ? ? ? ? int imageNumber = reader.getNumImages(true);
? ? ? ? ? ? images = new BufferedImage[imageNumber];
? ? ? ? ? ? for (int i = 0; i < imageNumber; i++) {
? ? ? ? ? ? ? ? BufferedImage image = reader.read(i);
? ? ? ? ? ? ? ? if (sourceImage.getWidth() > image.getWidth() || sourceImage.getHeight() > image.getHeight()) {
? ? ? ? ? ? ? ? ? ? image = zoom(image, sourceImage.getWidth(), sourceImage.getHeight());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? images[i] = image;
? ? ? ? ? ? }
? ? ? ? ? ? reader.dispose();
? ? ? ? ? ? iis.close();
? ? ? ? }
? ? ? ? return images;
? ? }

? ? /**
? ? ?* 根據(jù)要求處理圖片
? ? ?*
? ? ?* @param images 圖片數(shù)組
? ? ?* @param x ? ? ?橫向起始位置
? ? ?* @param y ? ? ?縱向起始位置
? ? ?* @param width ?寬度
? ? ?* @param height 寬度
? ? ?* @return 處理后的圖片數(shù)組
? ? ?* @throws Exception
? ? ?*/
? ? public static BufferedImage[] processImage(BufferedImage[] images, int x, int y, int width, int height) throws Exception {
? ? ? ? if (null == images) {
? ? ? ? ? ? return images;
? ? ? ? }
? ? ? ? BufferedImage[] oldImages = images;
? ? ? ? images = new BufferedImage[images.length];
? ? ? ? for (int i = 0; i < oldImages.length; i++) {
? ? ? ? ? ? BufferedImage image = oldImages[i];
? ? ? ? ? ? images[i] = image.getSubimage(x, y, width, height);
? ? ? ? }
? ? ? ? return images;
? ? }

? ? /**
? ? ?* 寫入處理后的圖片到file
? ? ?* <p>
? ? ?* 圖片后綴根據(jù)圖片格式生成
? ? ?*
? ? ?* @param images ? ? 處理后的圖片數(shù)據(jù)
? ? ?* @param formatName 圖片格式
? ? ?* @param file ? ? ? 寫入文件對象
? ? ?* @throws Exception
? ? ?*/
? ? public static void writerImage(BufferedImage[] images, String formatName, File file) throws Exception {
? ? ? ? Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByFormatName(formatName);
? ? ? ? if (imageWriters.hasNext()) {
? ? ? ? ? ? ImageWriter writer = imageWriters.next();
? ? ? ? ? ? String fileName = file.getName();
? ? ? ? ? ? int index = fileName.lastIndexOf(".");
? ? ? ? ? ? if (index > 0) {
? ? ? ? ? ? ? ? fileName = fileName.substring(0, index + 1) + formatName;
? ? ? ? ? ? }
? ? ? ? ? ? String pathPrefix = getFilePrefixPath(file.getPath());
? ? ? ? ? ? File outFile = new File(pathPrefix + fileName);
? ? ? ? ? ? ImageOutputStream ios = ImageIO.createImageOutputStream(outFile);
? ? ? ? ? ? writer.setOutput(ios);

? ? ? ? ? ? if (writer.canWriteSequence()) {
? ? ? ? ? ? ? ? writer.prepareWriteSequence(null);
? ? ? ? ? ? ? ? for (int i = 0; i < images.length; i++) {
? ? ? ? ? ? ? ? ? ? BufferedImage childImage = images[i];
? ? ? ? ? ? ? ? ? ? IIOImage image = new IIOImage(childImage, null, null);
? ? ? ? ? ? ? ? ? ? writer.writeToSequence(image, null);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? writer.endWriteSequence();
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? for (int i = 0; i < images.length; i++) {
? ? ? ? ? ? ? ? ? ? writer.write(images[i]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }

? ? ? ? ? ? writer.dispose();
? ? ? ? ? ? ios.close();
? ? ? ? }
? ? }

? ? /**
? ? ?* 剪切格式圖片
? ? ?* <p>
? ? ?* 基于JDK Image I/O解決方案
? ? ?*
? ? ?* @param sourceFile 待剪切圖片文件對象
? ? ?* @param destFile ? 裁剪后保存文件對象
? ? ?* @param x ? ? ? ? ?剪切橫向起始位置
? ? ?* @param y ? ? ? ? ?剪切縱向起始位置
? ? ?* @param width ? ? ?剪切寬度
? ? ?* @param height ? ? 剪切寬度
? ? ?* @throws Exception
? ? ?*/
? ? public static void cutImage(File sourceFile, File destFile, int x, int y, int width, int height) throws Exception {
? ? ? ? // 讀取圖片信息
? ? ? ? BufferedImage[] images = readerImage(sourceFile);
? ? ? ? // 處理圖片
? ? ? ? images = processImage(images, x, y, width, height);
? ? ? ? // 獲取文件后綴
? ? ? ? String formatName = getImageFormatName(sourceFile);

? ? ? ? destFile = new File(getPathWithoutSuffix(destFile.getPath()) + formatName);

? ? ? ? // 寫入處理后的圖片到文件
? ? ? ? writerImage(images, formatName, destFile);
? ? }


? ? /**
? ? ?* 獲取系統(tǒng)支持的圖片格式
? ? ?*/
? ? public static void getOSSupportsStandardImageFormat() {
? ? ? ? String[] readerFormatName = ImageIO.getReaderFormatNames();
? ? ? ? String[] readerSuffixName = ImageIO.getReaderFileSuffixes();
? ? ? ? String[] readerMIMEType = ImageIO.getReaderMIMETypes();
? ? ? ? System.out.println("========================= OS supports reader ========================");
? ? ? ? System.out.println("OS supports reader format name : ?" + Arrays.asList(readerFormatName));
? ? ? ? System.out.println("OS supports reader suffix name : ?" + Arrays.asList(readerSuffixName));
? ? ? ? System.out.println("OS supports reader MIME type : ?" + Arrays.asList(readerMIMEType));

? ? ? ? String[] writerFormatName = ImageIO.getWriterFormatNames();
? ? ? ? String[] writerSuffixName = ImageIO.getWriterFileSuffixes();
? ? ? ? String[] writerMIMEType = ImageIO.getWriterMIMETypes();

? ? ? ? System.out.println("========================= OS supports writer ========================");
? ? ? ? System.out.println("OS supports writer format name : ?" + Arrays.asList(writerFormatName));
? ? ? ? System.out.println("OS supports writer suffix name : ?" + Arrays.asList(writerSuffixName));
? ? ? ? System.out.println("OS supports writer MIME type : ?" + Arrays.asList(writerMIMEType));
? ? }

? ? /**
? ? ?* 壓縮圖片
? ? ?*
? ? ?* @param sourceImage 待壓縮圖片
? ? ?* @param width ? ? ? 壓縮圖片高度
? ? ?* @param height ? ? ?壓縮圖片寬度
? ? ?*/
? ? private static BufferedImage zoom(BufferedImage sourceImage, int width, int height) {
? ? ? ? BufferedImage zoomImage = new BufferedImage(width, height, sourceImage.getType());
? ? ? ? Image image = sourceImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
? ? ? ? Graphics gc = zoomImage.getGraphics();
? ? ? ? gc.setColor(Color.WHITE);
? ? ? ? gc.drawImage(image, 0, 0, null);
? ? ? ? return zoomImage;
? ? }

? ? /**
? ? ?* 獲取某個文件的前綴路徑
? ? ?* <p>
? ? ?* 不包含文件名的路徑
? ? ?*
? ? ?* @param file 當前文件對象
? ? ?* @return
? ? ?* @throws IOException
? ? ?*/
? ? public static String getFilePrefixPath(File file) throws IOException {
? ? ? ? String path = null;
? ? ? ? if (!file.exists()) {
? ? ? ? ? ? throw new IOException("not found the file !");
? ? ? ? }
? ? ? ? String fileName = file.getName();
? ? ? ? path = file.getPath().replace(fileName, "");
? ? ? ? return path;
? ? }

? ? /**
? ? ?* 獲取某個文件的前綴路徑
? ? ?* <p>
? ? ?* 不包含文件名的路徑
? ? ?*
? ? ?* @param path 當前文件路徑
? ? ?* @return 不包含文件名的路徑
? ? ?* @throws Exception
? ? ?*/
? ? public static String getFilePrefixPath(String path) throws Exception {
? ? ? ? if (null == path || path.isEmpty()) throw new Exception("文件路徑為空!");
? ? ? ? int index = path.lastIndexOf(File.separator);
? ? ? ? if (index > 0) {
? ? ? ? ? ? path = path.substring(0, index + 1);
? ? ? ? }
? ? ? ? return path;
? ? }

? ? /**
? ? ?* 獲取不包含后綴的文件路徑
? ? ?*
? ? ?* @param src
? ? ?* @return
? ? ?*/
? ? public static String getPathWithoutSuffix(String src) {
? ? ? ? String path = src;
? ? ? ? int index = path.lastIndexOf(".");
? ? ? ? if (index > 0) {
? ? ? ? ? ? path = path.substring(0, index + 1);
? ? ? ? }
? ? ? ? return path;
? ? }

? ? /**
? ? ?* 獲取文件名
? ? ?*
? ? ?* @param filePath 文件路徑
? ? ?* @return 文件名
? ? ?* @throws IOException
? ? ?*/
? ? public static String getFileName(String filePath) throws IOException {
? ? ? ? File file = new File(filePath);
? ? ? ? if (!file.exists()) {
? ? ? ? ? ? throw new IOException("not found the file !");
? ? ? ? }
? ? ? ? return file.getName();
? ? }


? ? /**
? ? ?* @param args
? ? ?* @throws Exception
? ? ?*/
? ? public static void main(String[] args) throws Exception {
? ? ? ? // 獲取系統(tǒng)支持的圖片格式
// ? ? ? ?ImageCutterUtil.getOSSupportsStandardImageFormat();

? ? ? ? try {
? ? ? ? ? ? // 起始坐標,剪切大小
? ? ? ? ? ? int x = 14;
? ? ? ? ? ? int y = 24;
? ? ? ? ? ? int width = 62;
? ? ? ? ? ? int height = 62;

? ? ? ? ? ? // 參考圖像大小
? ? ? ? ? ? int clientWidth = 88;
? ? ? ? ? ? int clientHeight = 88;


? ? ? ? ? ? File file = new File("/Users/mac/IdeaProjects/QRdemo/resources/src/com/xudaolong/QR/TestQR/QR.jpg");


? ? ? ? ? ? BufferedImage image = ImageIO.read(file);

? ? ? ? ? ? double destWidth = image.getWidth();
? ? ? ? ? ? double destHeight = image.getHeight();

? ? ? ? ? ? if (destWidth < width || destHeight < height)
? ? ? ? ? ? ? ? throw new Exception("源圖大小小于截取圖片大小!");

? ? ? ? ? ? double widthRatio = destWidth / clientWidth;
? ? ? ? ? ? double heightRatio = destHeight / clientHeight;

? ? ? ? ? ? x = Double.valueOf(x * widthRatio).intValue();
? ? ? ? ? ? y = Double.valueOf(y * heightRatio).intValue();
? ? ? ? ? ? width = Double.valueOf(width * widthRatio).intValue();
? ? ? ? ? ? height = Double.valueOf(height * heightRatio).intValue();

? ? ? ? ? ? System.out.println("裁剪大小 ?x:" + x + ",y:" + y + ",width:" + width + ",height:" + height);

? ? ? ? ? ? String formatName = getImageFormatName(file);
? ? ? ? ? ? String pathSuffix = "." + formatName;
? ? ? ? ? ? String pathPrefix = getFilePrefixPath(file);
? ? ? ? ? ? String targetPath = pathPrefix + System.currentTimeMillis() + pathSuffix;

? ? ? ? ? ? File destFile = new File(targetPath);

? ? ? ? ? ? ImageCutterUtil.cutImage(file, destFile, x, y, width, height);

? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}

單方面測試

public void cutQR(String sourcePath) {

? ? ? ? try {
? ? ? ? ? ? File file = new File(sourcePath);

? ? ? ? ? ? BufferedImage image = ImageIO.read(file);

? ? ? ? ? ? // 起始坐標,剪切大小
? ? ? ? ? ? int x = 14;
? ? ? ? ? ? int y = 25;
? ? ? ? ? ? int width = 62;
? ? ? ? ? ? int height = 62;
? ? ? ? ? ? // 參考圖像大小
? ? ? ? ? ? int clientWidth = 88;
? ? ? ? ? ? int clientHeight = 88;

? ? ? ? ? ? double destWidth = image.getWidth();
? ? ? ? ? ? double destHeight = image.getHeight();

? ? ? ? ? ? if (destWidth < width || destHeight < height)
? ? ? ? ? ? ? ? throw new Exception("源圖大小小于截取圖片大小!");


? ? ? ? ? ? double widthRatio = destWidth / clientWidth;
? ? ? ? ? ? double heightRatio = destHeight / clientHeight;

? ? ? ? ? ? //修改一下單位
? ? ? ? ? ? x = Double.valueOf(x * widthRatio).intValue();
? ? ? ? ? ? y = Double.valueOf(y * heightRatio).intValue();
? ? ? ? ? ? width = Double.valueOf(width * widthRatio).intValue();
? ? ? ? ? ? height = Double.valueOf(height * heightRatio).intValue();

? ? ? ? ? ? System.out.println("裁剪大小 ?x:" + x + ",y:" + y + ",width:" + width + ",height:" + height);

? ? ? ? ? ? //獲取指定的名字
// ? ? ? ? ? ?String formatName = getImageFormatName(file);
// ? ? ? ? ? ?String pathSuffix = "." + formatName;
// ? ? ? ? ? ?String pathPrefix = getFilePrefixPath(file);
// ? ? ? ? ? ?String targetPath = pathPrefix + System.currentTimeMillis() + pathSuffix;

? ? ? ? ? ? //最后一步進行裁剪到指定的名字

? ? ? ? ? ? File destFile = new File(sourcePath);

? ? ? ? ? ? ImageCutterUtil.cutImage(file, destFile, x, y, width, height);

? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java基礎-封裝和繼承

    Java基礎-封裝和繼承

    這篇文章主要介紹了Java面向對象編程(封裝/繼承/多態(tài))實例解析的相關內容,具有一定參考價值,需要的朋友可以了解下希望可以幫助到你
    2021-07-07
  • Spring Boot實現(xiàn)發(fā)送郵件

    Spring Boot實現(xiàn)發(fā)送郵件

    這篇文章主要為大家詳細介紹了Spring Boot實現(xiàn)發(fā)送郵件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • JPA如何使用findBy方法自定義查詢

    JPA如何使用findBy方法自定義查詢

    這篇文章主要介紹了JPA如何使用findBy方法自定義查詢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java操作SSH2實現(xiàn)遠程執(zhí)行l(wèi)inux命令

    Java操作SSH2實現(xiàn)遠程執(zhí)行l(wèi)inux命令

    這篇文章主要為大家詳細介紹了Java如何操作SSH2實現(xiàn)遠程執(zhí)行l(wèi)inux命令,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2025-01-01
  • Java使用File類遍歷目錄及文件實例代碼

    Java使用File類遍歷目錄及文件實例代碼

    本篇文章主要介紹了Java使用File類遍歷目錄及文件實例代碼,詳細的介紹了File類的使用,有興趣的可以了解一下。
    2017-04-04
  • 使用?Spring?AI?+?Ollama?構建生成式?AI?應用的方法

    使用?Spring?AI?+?Ollama?構建生成式?AI?應用的方法

    通過集成SpringBoot和Ollama,本文詳細介紹了如何構建生成式AI應用,首先,介紹了AI大模型服務的兩種實現(xiàn)方式,選擇使用ollama進行部署,隨后,通過SpringBoot+SpringAI來實現(xiàn)應用構建,本文為開發(fā)者提供了一個實用的指南,幫助他們快速入門生成式AI應用的開發(fā)
    2024-11-11
  • java springmvc實現(xiàn)驗證碼功能

    java springmvc實現(xiàn)驗證碼功能

    這篇文章主要為大家詳細介紹了java springmvc實現(xiàn)驗證碼功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • SpringBoot獲取resources目錄下的文件

    SpringBoot獲取resources目錄下的文件

    在 Spring Boot 項目中,獲取 resources 目錄中的文件路徑通常涉及到訪問類路徑資源,Spring Boot 提供了一些工具類和方法,下面小編就來和大家詳細聊聊
    2024-10-10
  • Spring項目接入DeepSeek的兩種超簡單的方式分享

    Spring項目接入DeepSeek的兩種超簡單的方式分享

    DeepSeek?作為一款卓越的國產?AI?模型,越來越多的公司考慮在自己的應用中集成,本文為大家?分享了Spring項目接入DeepSeek的兩種超簡單的方式,希望對大家有所幫助
    2025-02-02
  • Java程序死鎖問題定位與解決方法

    Java程序死鎖問題定位與解決方法

    死鎖是一種特定的程序狀態(tài),主要是由于循環(huán)依賴導致彼此一直處于等待中,而使得程序陷入僵局,相當尷尬,死鎖不僅僅發(fā)生在線程之間,而對于資源獨占的進程之間同樣可能出現(xiàn)死鎖,本文給大家介紹了Java程序死鎖問題定位與解決方法,需要的朋友可以參考下
    2024-11-11

最新評論

德格县| 巫山县| 梁河县| 肥东县| 青川县| 晋中市| 新龙县| 治县。| 榆林市| 隆安县| 宜城市| 淄博市| 东海县| 九台市| 华亭县| 方城县| 公安县| 明星| 花垣县| 鄂伦春自治旗| 云浮市| 朝阳市| 遂平县| 水城县| 措勤县| 夏河县| 沙湾县| 芒康县| 扶风县| 安多县| 岳池县| 上饶市| 抚顺市| 伊宁市| 高邑县| 县级市| 东辽县| 沙坪坝区| 喜德县| 江都市| 天气|