java는 원격 네트워크에서 이미지 파일 스트림을 가져와 압축한 후 로컬에 저장합니다
1. 원격 네트워크에서 이미지 가져오기
/** * 根据地址获得数据的字节流 * * @param strUrl * 网络连接地址 * @return */ public static byte[] getImageFromNetByUrl(String strUrl) { try { URL url = new URL(strUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); InputStream inStream = conn.getInputStream();// 通过输入流获取图片数据 byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据 return btImg; } catch (Exception e) { e.printStackTrace(); } return null; }
/** * 根据地址获得数据的字节流 * * @param strUrl * 本地连接地址 * @return */ public static byte[] getImageFromLocalByUrl(String strUrl) { try { File imageFile = new File(strUrl); InputStream inStream = new FileInputStream(imageFile); byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据 return btImg; } catch (Exception e) { e.printStackTrace(); } return null; }
/** * 从输入流中获取数据 * * @param inStream * 输入流 * @return * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[10240]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); }
2. 네트워크 읽기 파일을 로컬 파일로 변환
byte[] btImg1 = ImageUtil.getImageFromNetByUrl(fileUrl1); if (null != btImg1 && btImg1.length > 0) { logger.debug("读取到:" + btImg1.length + " 字节"); ImageUtil.writeImageToDisk(btImg1, fileZipUrl1); } else { logger.debug("没有从该连接获得内容"); } byte[] btImg2 = ImageUtil.getImageFromNetByUrl(fileUrl2); if (null != btImg2 && btImg2.length > 0) { logger.debug("读取到:" + btImg2.length + " 字节"); ImageUtil.writeImageToDisk(btImg2, fileZipUrl2); } else { logger.debug("没有从该连接获得内容"); }
/** * 将图片写入到磁盘 * * @param img * 图片数据流 * @param fileName * 文件保存时的名称 */ public static void writeImageToDisk(byte[] img, String zipImageUrl) { try { File file = new File(zipImageUrl); FileOutputStream fops = new FileOutputStream(file); fops.write(img); fops.flush(); fops.close(); System.out.println("图片已经写入"+zipImageUrl); } catch (Exception e) { e.printStackTrace(); } }
3. 로컬 이미지 압축
import java.io.*; import java.util.Date; import java.awt.*; import java.awt.image.*; import javax.imageio.ImageIO; import com.sun.image.codec.jpeg.*; /** * 图片压缩处理 */ public class ImgCompress { private Image img; private int width; private int height; /** * 构造函数 */ public ImgCompress(String fileName) throws IOException { File file = new File(fileName);// 读入文件 img = ImageIO.read(file); // 构造Image对象 width = img.getWidth(null); // 得到源图宽 height = img.getHeight(null); // 得到源图长 } /** * 按照宽度还是高度进行压缩 * @param w int 最大宽度 * @param h int 最大高度 */ public void resizeFix(int w, int h) throws IOException { if (width / height > w / h) { resizeByWidth(w); } else { resizeByHeight(h); } } /** * 以宽度为基准,等比例放缩图片 * @param w int 新宽度 */ public void resizeByWidth(int w) throws IOException { int h = (int) (height * w / width); resize(w, h); } /** * 以高度为基准,等比例缩放图片 * @param h int 新高度 */ public void resizeByHeight(int h) throws IOException { int w = (int) (width * h / height); resize(w, h); } /** * 强制压缩/放大图片到固定的大小 * @param w int 新宽度 * @param h int 新高度 */ public void resize(int w, int h) throws IOException { // SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢 BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB ); image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图 File destFile = new File("C:/Users/Administrator/Desktop/147.jpg"); FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流 // 可以正常实现bmp、png、gif转jpg JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); // JPEG编码 out.close(); }
@SuppressWarnings("deprecation") public static void main(String[] args) throws Exception { System.out.println("开始:" + new Date().toLocaleString()); ImgCompress imgCom = new ImgCompress("C:/Users/Administrator/Desktop/1479209533362.jpg"); imgCom.resizeFix(285, 380); System.out.println("结束:" + new Date().toLocaleString()); }
}
끝~

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

SecList
SecLists는 최고의 보안 테스터의 동반자입니다. 보안 평가 시 자주 사용되는 다양한 유형의 목록을 한 곳에 모아 놓은 것입니다. SecLists는 보안 테스터에게 필요할 수 있는 모든 목록을 편리하게 제공하여 보안 테스트를 더욱 효율적이고 생산적으로 만드는 데 도움이 됩니다. 목록 유형에는 사용자 이름, 비밀번호, URL, 퍼징 페이로드, 민감한 데이터 패턴, 웹 셸 등이 포함됩니다. 테스터는 이 저장소를 새로운 테스트 시스템으로 간단히 가져올 수 있으며 필요한 모든 유형의 목록에 액세스할 수 있습니다.

PhpStorm 맥 버전
최신(2018.2.1) 전문 PHP 통합 개발 도구

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

DVWA
DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)
