In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how Java achieves the function of picture clipping. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Preface
The following provides the Java tool class that tailors the image to a custom size, as pragmatic as ever.
Maven relies on com.google.guava guava 30.1.1-jre org.bytedeco javacv-platform 1.5.5 cn.hutool hutool-all 5.5.2 code
No nonsense, go to the code.
Package ai.guiji.csdn.tool;import cn.hutool.core.util.IdUtil;import com.google.common.base.Joiner;import com.google.common.base.Splitter;import org.bytedeco.javacpp.Loader;import java.io.File;import java.text.MessageFormat;import java.util.Arrays;import java.util.List / * @ Program: csdn @ ClassName: CutOutTool @ Author: swordsman A Liang _ ALiang @ Date: 2022-01-23 18:27 @ Description: * cropping tool @ Version: V1.0 * / public class CutOutTool {/ * Image clipping * * @ param imagePath image address * @ param outputDir temporary directory * @ param startX crop start x coordinates * @ param startY crop start Y coordinate * @ param weight crop width * @ param height crop height * @ throws Exception exception * / public static String cutOutImage (String imagePath String outputDir, Integer startX, Integer startY, Integer weight, Integer height) throws Exception {List paths = Splitter.on (".") .splitToList (imagePath) String ext = paths.get (paths.size ()-1); if (! Arrays.asList ("png", "jpg") .join (ext) {throw new Exception ("format error");} String resultPath = Joiner.on (File.separator) .join (outputDir, IdUtil.simpleUUID () + "." + ext); String ffmpeg = Loader.load (org.bytedeco.ffmpeg.ffmpeg.class) ProcessBuilder builder = new ProcessBuilder (ffmpeg, "- I", imagePath, "- vf", MessageFormat.format ("crop= {0}: {1}: {2}: {3}", String.valueOf (weight), String.valueOf (height), String.valueOf (startX)) String.valueOf (startY)), "- y", resultPath) Builder.inheritIO (). Start (). WaitFor (); return resultPath;} public static void main (String [] args) throws Exception {System.out.println (cutOutImage ("C:\\ Users\\ yi\\ Desktop\ 2054011.jpg", "C:\ Users\\ yi\\ Desktop\", 0, 0, 1920, 2160));}}
Code description:
1. The parameters of cutOutImage method are image path, output temporary directory, starting coordinate x value, starting coordinate y value, clipping width and clipping height, respectively.
2. Use uuid as the only id for temporary output to avoid repetition.
3. Check the file suffix format, and you can adjust it according to your needs.
4. The cutting size cannot exceed the limit of the picture, and can be adjusted according to the demand.
Verify it.
The prepared pictures are as follows
Execution result
Ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers built with gcc 10.2.0 (Rev5, Built by MSYS2 project) configuration:-- prefix=.. -- disable-iconv-- disable-opencl-- disable-sdl2-- disable-bzlib-- disable-lzma-- disable-linux-perf-- enable-shared-- enable-version3-- enable-runtime-cpudetect-- enable-zlib-- enable-libmp3lame-- enable-libspeex-- enable-libopencore-amrnb-- enable-libopencore-amrwb-- enable-libvo-amrwbenc-- enable-openssl-- enable-libopenh364-enable-libvpx-- enable-libfreetype-- enable-libopus-- enable-cuda-- enable-cuvid -- enable-nvenc-- enable-libmfx-- enable-w32threads-- enable-indev=dshow-- target-os=mingw32-- cc='gcc-M64'-- extra-cflags=-I../include/-- extra-ldflags=-L../lib/-- extra-libs='-static-libgcc-static-libstdc++-Wl Bstatic-lstdc++-lgcc_eh-lWs2_32-lcrypt32-lpthread-lz-lm-Wl,-Bdynamic-lole32-luuid' libavutil 56. 51.100 / 56. 51.100 libavcodec 58. 91.100 / 58. 91.100 libavformat 58. 45.100 / 58. 45.100 libavdevice 58. 10.100 / 58. 10.100 libavfilter 7. 85.100 / 7. 85.100 libswscale 5. 7.100 / 5. 7.100 libswresample 3. 7.100 / 3. 7.100Input # 0, image2, from'C:\ Users\ yi\ Desktop\ 2054011.jpgkeeper: Duration: 00VOV 00.04, start: 0.000000, bitrate: 255438 kb/s Stream # 0:0: Video: mjpeg (Progressive), yuvj444p (pc, bt470bg/unknown/unknown), 3840x2160, 25 tbr, 25 tbn 25 tbcStream mapping: Stream # 0:0-> # 0:0 (mjpeg (native)-> mjpeg (native)) Press [Q] to stop, [?] For helpOutput # 0, image2, to'C:\ Users\ yi\ Desktop\\ d1013fbee79e4380a01c574addf72afb.jpgbread: Metadata: encoder: Lavf58.45.100 Stream # 0:0: Video: mjpeg, yuvj444p (pc), 1920x2160, Qothers 2-31,200 kb/s, 25 fps, 25 tbn 25 tbc Metadata: encoder: Lavc58.91.100 mjpeg Side data: cpb: bitrate max/min/avg: 0 vbv_delay 0200000 buffer size: 0 vbv_delay: N fps=0.0 Aframe = 1 fps=0.0 qshock 10.4 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.201x video:234kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknownC:\ Users\ yi\ Desktop\\ d1013fbee79e4380a01c574addf72afb.jpgProcess finished with exit code 0
The result is as follows
What are the basic data types of java? the basic data types of Java can be divided into: 1. Integer types, which are used to represent the data types of integers. 2. Floating point type, which is used to represent the data type of decimals. 3. Character type. The keyword of character type is "char". 4. Boolean type, which is the basic data type that represents logical values.
After reading the above, do you have any further understanding of how Java implements the image clipping function? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.