iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >如何使用Java实现图片切割功能
  • 164
分享到

如何使用Java实现图片切割功能

2023-06-28 06:06:50 164人浏览 八月长安
摘要

这篇文章将为大家详细讲解有关如何使用Java实现图片切割功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。具体内容如下工具类package com.xudaolong.Utils;import&

这篇文章将为大家详细讲解有关如何使用Java实现图片切割功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

具体内容如下

工具

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;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;        }    }        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;    }            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;    }        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;    }        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();        }    }        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);    }        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));    }        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;    }        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;    }        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;    }        public static String getPathWithoutSuffix(String src) {        String path = src;        int index = path.lastIndexOf(".");        if (index > 0) {            path = path.substring(0, index + 1);        }        return path;    }        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();    }        public static void main(String[] args) throws Exception {        // 获取系统支持的图片格式//        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实现图片切割功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

--结束END--

本文标题: 如何使用Java实现图片切割功能

本文链接: https://www.lsjlt.com/news/317066.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何使用Java实现图片切割功能
    这篇文章将为大家详细讲解有关如何使用Java实现图片切割功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。具体内容如下工具类package com.xudaolong.Utils;import&...
    99+
    2023-06-28
  • Java实现图片切割功能
    本文实例为大家分享了Java实现图片切割功能的具体代码,供大家参考,具体内容如下 工具类 package com.xudaolong.Utils; import javax.ima...
    99+
    2022-11-13
  • js实现图片切割功能
    本文实例为大家分享了js实现图片切割的具体代码,供大家参考,具体内容如下 代码: <!DOCTYPE html> <html lang="en"> &l...
    99+
    2022-11-12
  • Android使用ViewFlipper实现图片切换功能
    今天给大家简单的讲一下Android手势,目前市场上的App中手势的运用比较少。 Android提供了两种手势: ①.Android提供了手势检测,并为手势检测提供了相应的监听器 ②.Android允许开发者添加手势,并提供了相应的API识...
    99+
    2023-05-30
    android viewflipper 图片切换
  • Vue.js实现图片切换功能
    本文实例为大家分享了Vue.js实现图片切换功能的具体代码,供大家参考,具体内容如下 实现功能如下 文件目录如下,要实现本功能只需要修改图片的存储位置即可 代码如下 <...
    99+
    2022-11-12
  • Vue实现裁切图片功能
    本文实例为大家分享了Vue实现裁切图片的具体代码,供大家参考,具体内容如下 项目需求做一个身份证的裁切功能 原生开发的话,这种功能挺容易实现的 Web的没有做过相关功能,百度了一下...
    99+
    2022-11-13
  • Java实现图像分割功能
    使用Java实现图像分割,供大家参考,具体内容如下 为减少动画制作过程中的IO操作,我们可以使用连续动画来改善动画播放效率。 假如我们有如下的一张图像: 如果我们对图像中的每张小图...
    99+
    2022-11-12
  • vue-cropper组件如何实现图片切割上传
    小编给大家分享一下vue-cropper组件如何实现图片切割上传,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!vue-cropper在vue中的引入1、组件内引入...
    99+
    2023-06-15
  • 使用JS实现简单的图片切换功能
    本文实例为大家分享了使用JS实现简单的图片切换的具体代码,供大家参考,具体内容如下 效果如图: 分析:首先为按钮添加单击响应事件,然后构造函数。其实切换图片就是切换img标签src...
    99+
    2022-11-13
  • javascript实现点击图片切换功能
    本文实例为大家分享了javascript实现点击图片切换的具体代码,供大家参考,具体内容如下 实现效果:图片点击切换 代码: <!DOCTYPE html> <h...
    99+
    2022-11-13
  • Vue怎么实现裁切图片功能
    今天小编给大家分享一下Vue怎么实现裁切图片功能的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。效果:1)、安装 vue-cr...
    99+
    2023-06-30
  • vue利用插件实现按比例切割图片
    本文实例为大家分享了vue利用插件实现按比例切割图片的具体代码,供大家参考,具体内容如下 1.使用插件——vueCropper 安装该插件:npm install vue-cropp...
    99+
    2022-11-12
  • java使用CKEditor实现图片上传功能
    java如何使用CKEditor实现图片上传功能,具体内容如下根据实际需要下载指定的CKEditor删除文件ckeditor/plugins/image/dialogs/image.js预览框中文本内容,并修改hidden属性值为显示上传选...
    99+
    2023-05-31
    ckeditor 上传 ava
  • Android基于ImageSwitcher实现图片切换功能
    左右切换图片控件大家都用ViewPager, ViewFipper比较多吧,我之前也用ViewPager实现了,使用ViewPager实现左右循环滑动图片,有兴趣的可以去看下,...
    99+
    2022-06-06
    图片 Android
  • 怎么使用JS实现简单的图片切换功能
    这篇“怎么使用JS实现简单的图片切换功能”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“怎么使用JS实现简单的图片切换功能”文...
    99+
    2023-07-02
  • 如何使用backgroundImage解决图片轮播切换功能
    这篇文章主要介绍了如何使用backgroundImage解决图片轮播切换功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。单dom节点实现轮...
    99+
    2022-10-19
  • 基于Python如何实现图片一键切割九宫格工具
    本文小编为大家详细介绍“基于Python如何实现图片一键切割九宫格工具”,内容详细,步骤清晰,细节处理妥当,希望这篇“基于Python如何实现图片一键切割九宫格工具”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。实...
    99+
    2023-07-05
  • Java实现图片验证码功能
    简介 在实现登录功能时,一般为了安全都会设置验证码登录,为了防止某个用户用特定的程序暴力破解方式进行不断的尝试登录。常见验证码分为图片验证码和短信验证码,还有滑动窗口模块和选中指定物...
    99+
    2022-11-13
  • Java使用Ajax实现跨域上传图片功能
    说明 :图片服务器是用Nginx搭建的,用的是PHP语言这个功能 需要 用到两个js文件:jquery.js和jQuery.form.js<script type="text/JavaScript" src="js/jquery.js...
    99+
    2023-05-31
    javajava 跨域 ava
  • Android控件ImageSwitcher实现左右图片切换功能
    ImageSwitcher类是ViewSwitcher类的子类,它实现的效果是在完成ImageView的切换并且带有动画效果。要使用这个类需要以下两个步骤: 1)为ImageS...
    99+
    2022-06-06
    图片 Android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作