이 기사의 예에서는 Java에서 축소판을 만들고 이미지 비율을 조정하는 방법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 구체적인 구현 방법은 다음과 같습니다.
이 인스턴스는 이미지의 너비와 높이를 지정된 너비와 높이로 조정하고 지정된 디렉터리에 저장하는 것을 지원합니다. 이미지 축소 비율은 대상 개체의 크기와 이미지의 표준(지정된) 크기를 기준으로 계산됩니다. 크기 조정 품질을 설정할 수 있으며, 이미지 크기 조정 품질은 지정된 너비와 높이의 이미지 크기 조정에 따라 설정할 수 있습니다.
구체적인 코드는 다음과 같습니다.
다음과 같이 코드 코드를 복사합니다.
패키지 com.hoo.util;
java.awt.Image 가져오기;
import java.awt.image.BufferedImage;
java.io.파일 가져오기;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
java.net.URL 가져오기;
javax.imageio.ImageIO 가져오기;
import com.sun.image.codec.jpeg.ImageFormatException;
com.sun.image.codec.jpeg.JPEGCodec을 가져옵니다.
import com.sun.image.codec.jpeg.JPEGEncodeParam;
com.sun.image.codec.jpeg.JPEGImageEncoder 가져오기;
/**
* <b>기능:</b> 이미지 도구 클래스 확대/축소, 축소판 만들기, 이미지 비율 늘리기
*@authorhoojo
* @createDate 2012-2-3 오전 10:08:47
* @file ScaleImageUtils.java
* @package com.hoo.util
* @버전 1.0
*/
공개 추상 클래스 ScaleImageUtils {
개인 정적 최종 부동 소수점 DEFAULT_SCALE_QUALITY = 1f;
private static final String DEFAULT_IMAGE_FORMAT = ".jpg"; // 이미지 파일 형식
private static final String DEFAULT_FILE_PATH = "C:/temp-";
/**
* <b>함수:</b> 이미지 압축 품질 열거 클래스를 설정합니다.
* 일부 지침: 고품질 0.75, 중간 품질 0.5, 낮은 품질 0.25
*@authorhoojo
* @createDate 2012-2-7 오전 11:31:45
* @file ScaleImageUtils.java
* @package com.hoo.util
* @project JQueryMobile
* @버전 1.0
*/
공개 열거형 ImageQuality {
최대(1.0f), 높음(0.75f), 중간(0.5f), 낮음(0.25f);
개인 플로트 품질;
공공 부동 getQuality() {
이것을 반환하십시오.품질;
}
ImageQuality(플로트 품질) {
this.quality = 품질;
}
}
개인 정적 이미지 이미지;
/**
* <b>기능:</b> 대상 물체의 크기와 표준(지정된) 크기를 기준으로 이미지 축소 비율을 계산합니다.
*@authorhoojo
* @createDate 2012-2-6 04:41:48 오후
* @param targetWidth 대상의 너비
* @param targetHeight 타겟의 높이
* @param StandardWidth 표준(지정된) 너비
* @param StandardHeight 표준(지정된) 높이
* @최소 적합한 비율을 반환합니다.
*/
public static double getScaling(double targetWidth, double targetHeight, doublestandardWidth, doublestandardHeight) {
이중 너비Scaling = 0d;
이중 높이스케일링 = 0d;
if (targetWidth > StandardWidth) {
widthScaling = 표준Width / (targetWidth * 1.00d);
} 또 다른 {
widthScaling = 1d;
}
if (targetHeight > 표준 높이) {
heightScaling = 표준 높이 / (targetHeight * 1.00d);
} 또 다른 {
heightScaling = 1d;
}
return Math.min(widthScaling, heightScaling);
}
/**
* <b>함수:</b> 이미지의 너비와 높이를 지정된 너비와 높이로 조정하고 savePath 디렉터리에 저장합니다.
*@authorhoojo
* @createDate 2012-2-6 04:54:35 오후
* @param width 스케일링 너비
* @param height 줌 높이
* @param savePath 저장 디렉터리
* @param targetImage 확대할 대상 이미지
* @return 이미지 저장 경로 및 이름
* @throws ImageFormatException
* @throwsIOException
*/
public static String resize(int width, int height, String savePath, Image targetImage) throws ImageFormatException, IOException {
너비 = Math.max(너비, 1);
높이 = Math.max(높이, 1);
BufferedImage 이미지 = new BufferedImage(너비, 높이, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(targetImage, 0, 0, 너비, 높이, null);
if (savePath == null || "".equals(savePath)) {
savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT;
}
FileOutputStream fos = new FileOutputStream(new File(savePath));
JPEGImageEncoder 인코더 = JPEGCodec.createJPEGEncoder(fos);
인코더.인코드(이미지);
image.flush();
fos.flush();
fos.close();
savePath를 반환합니다.
}
/**
* <b>기능:</b> 이미지 크기 조정 품질을 설정하고 지정된 너비와 높이에 따라 이미지 크기를 조정할 수 있습니다.
*@authorhoojo
* @createDate 2012-2-7 오전 11:01:27
* @param width 스케일링 너비
* @param height 줌 높이
* @param quality 이미지 압축 품질, 최대값은 1입니다. 열거 값을 사용합니다: {@link ImageQuality}
* 일부 지침: 고품질 0.75, 중간 품질 0.5, 낮은 품질 0.25
* @param savePath 저장 디렉터리
* @param targetImage 확대할 대상 이미지
* @return 이미지 저장 경로 및 이름
* @throws ImageFormatException
* @throwsIOException
*/
public static String resize(int width, int height, Float quality, String savePath, Image targetImage) throws ImageFormatException, IOException {
너비 = Math.max(너비, 1);
높이 = Math.max(높이, 1);
BufferedImage 이미지 = new BufferedImage(너비, 높이, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(targetImage, 0, 0, 너비, 높이, null);
if (savePath == null || "".equals(savePath)) {
savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT;
}
FileOutputStream fos = new FileOutputStream(new File(savePath));
JPEGImageEncoder 인코더 = JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(이미지);
if (품질 == null || 품질 <= 0) {
품질 = DEFAULT_SCALE_QUALITY;
}
/**이미지 압축 품질 설정*/
encodeParam.setQuality(quality, true);
인코더.encode(image, encodeParam);
image.flush();
fos.flush();
fos.close();
savePath를 반환합니다.
}
/**
* <b>기능:</b> 이미지의 크기와 크기를 지정하여 이미지 축소에 적합한 크기를 계산합니다.
*@authorhoojo
* @createDate 2012-2-6 오후 05:53:10
* @param 너비 지정 너비
* @param height 지정된 높이
* @param 이미지 이미지 파일
* @return 너비와 높이의 int 배열을 반환합니다.
*/
public static int[] getSize(int 너비, int 높이, 이미지 이미지) {
int targetWidth = image.getWidth(null);
int targetHeight = image.getHeight(null);
이중 스케일링 = getScaling(targetWidth, targetHeight, 너비, 높이);
long StandardWidth = Math.round(targetWidth * scale);
long StandardHeight = Math.round(targetHeight * scale);
return new int[] { Integer.parseInt(Long.toString(standardWidth)), Integer.parseInt(String.valueOf(standardHeight)) };
}
/**
* <b>함수:</b> 지정된 비율과 이미지 객체를 통해 너비와 높이를 확대하거나 축소하여 반환합니다.
*@authorhoojo
* @createDate 2012-2-7 오전 10:27:59
* @param 스케일 스케일링 비율
* @param 이미지 이미지 객체
* @return 너비, 높이를 반환합니다.
*/
public static int[] getSize(float scale, 이미지 이미지) {
int targetWidth = image.getWidth(null);
int targetHeight = image.getHeight(null);
long StandardWidth = Math.round(targetWidth * scale);
long StandardHeight = Math.round(targetHeight * scale);
return new int[] { Integer.parseInt(Long.toString(standardWidth)), Integer.parseInt(String.valueOf(standardHeight)) };
}
public static int[] getSize(int 너비, 이미지 이미지) {
int targetWidth = image.getWidth(null);
int targetHeight = image.getHeight(null);
긴 높이 = Math.round((targetHeight * width) / (targetWidth * 1.00f));
return new int[] { width, Integer.parseInt(String.valueOf(height)) };
}
public static int[] getSizeByHeight(int 높이, 이미지 이미지) {
int targetWidth = image.getWidth(null);
int targetHeight = image.getHeight(null);
긴 너비 = Math.round((targetWidth * height) / (targetHeight * 1.00f));
return new int[] { Integer.parseInt(String.valueOf(width)), height };
}
/**
*
* <b>함수:</b> 지정된 targetFile 이미지 파일의 너비와 높이를 지정된 너비와 높이보다 크게 줄여서 savePath 디렉터리에 저장합니다.
*@authorhoojo
* @createDate 2012-2-6 오후 04:57:02
* @param 너비 감소 너비
* @param height 높이 감소
* @param savePath 저장 디렉터리
* @param targetImage가 대상 이미지를 변경했습니다.
* @return 이미지 저장 경로 및 이름
* @throws ImageFormatException
* @throwsIOException
*/
public static String resize(int width, int height, String savePath, File targetFile) throws ImageFormatException, IOException {
이미지 = ImageIO.read(targetFile);
int[] size = getSize(너비, 높이, 이미지);
return resize(크기[0], 크기[1], savePath, 이미지);
}
/**
*
* <b>기능:</b> 지정된 targetURL 네트워크 이미지 파일의 너비와 높이를 지정된 너비와 높이보다 크게 줄여서 savePath 디렉터리에 저장합니다.
*@authorhoojo
* @createDate 2012-2-6 오후 04:57:07
* @param 너비 감소 너비
* @param height 높이 감소
* @param savePath 저장 디렉터리
* @param targetImage가 대상 이미지를 변경했습니다.
* @return 이미지 저장 경로 및 이름
* @throws ImageFormatException
* @throwsIOException
*/
public static String resize(int width, int height, String savePath, URL targetURL) throws ImageFormatException, IOException {
이미지 = ImageIO.read(targetURL);
int[] size = getSize(너비, 높이, 이미지);
return resize(크기[0], 크기[1], savePath, 이미지);
}
/**
* <b>기능:</b> 지정된 비율에 따라 로컬 이미지 파일의 크기를 조정합니다.
*@authorhoojo
* @createDate 2012-2-7 오전 10:29:18
* @param 스케일 스케일링 비율
* @param savePath 저장 파일 경로 및 이름
* @param targetFile 로컬 이미지 파일
* @return 새 파일 이름
* @throws ImageFormatException
* @throwsIOException
*/
public static String resize(float scale, String savePath, File targetFile)는 ImageFormatException, IOException을 발생시킵니다.
이미지 = ImageIO.read(targetFile);
int[] size = getSize(스케일, 이미지);
return resize(크기[0], 크기[1], savePath, 이미지);
}
/**
* <b>기능:</b> 지정된 비율에 따라 네트워크 이미지 파일의 크기를 조정합니다.
*@authorhoojo
* @createDate 2012-2-7 오전 10:30:56
* @param 스케일 스케일링 비율
* @param savePath 저장 파일 경로 및 이름
* @param targetFile 로컬 이미지 파일
* @return 새 파일 이름
* @throws ImageFormatException
* @throwsIOException
*/
public static String resize(float scale, String savePath, URL targetURL)는 ImageFormatException, IOException을 발생시킵니다.
이미지 = ImageIO.read(targetURL);
int[] size = getSize(스케일, 이미지);
return resize(크기[0], 크기[1], savePath, 이미지);
}
/**
* <b>기능:</b> 고정된 너비에 따라 로컬 이미지 크기를 비례적으로 조정
*@authorhoojo
* @createDate 2012-2-7 오전 10:49:56
* @param 너비 고정 너비
* @param savePath 저장 경로 및 이름
* @param targetFile 로컬 타겟 파일
* @return 저장 경로를 반환합니다.
* @throws ImageFormatException
* @throwsIOException
*/
public static String resize(int width, String savePath, File targetFile)는 ImageFormatException, IOException을 발생시킵니다.
이미지 = ImageIO.read(targetFile);
int[] size = getSize(너비, 이미지);
return resize(크기[0], 크기[1], savePath, 이미지);
}
/**
* <b>기능:</b> 고정 너비에 따라 네트워크 이미지 크기를 비례적으로 조정
*@authorhoojo
* @createDate 2012-2-7 오전 10:50:52
* @param 너비 고정 너비
* @param savePath 저장 경로 및 이름
* @param targetFile 로컬 타겟 파일
* @return 저장 경로를 반환합니다.
* @throws ImageFormatException
* @throwsIOException
*/
public static String resize(int width, String savePath, URL targetURL)는 ImageFormatException, IOException을 발생시킵니다.
이미지 = ImageIO.read(targetURL);
int[] size = getSize(너비, 이미지);
return resize(크기[0], 크기[1], savePath, 이미지);
}
/**
*
* <b>기능:</b> 고정된 높이에 따라 로컬 이미지 크기를 비례적으로 조정
*@authorhoojo
* @createDate 2012-2-7 오전 10:51:17
* @param height 고정 높이
* @param savePath 저장 경로 및 이름
* @param targetFile 로컬 타겟 파일
* @return 저장 경로를 반환합니다.
* @throws ImageFormatException
* @throwsIOException
*/
public static String resizeByHeight(int height, String savePath, File targetFile) throws ImageFormatException, IOException {
이미지 = ImageIO.read(targetFile);
int[] size = getSizeByHeight(높이, 이미지);
return resize(크기[0], 크기[1], savePath, 이미지);
}
/**
* <b>기능:</b> 고정된 높이에 따라 네트워크 이미지 크기를 비례적으로 조정
*@authorhoojo
* @createDate 2012-2-7 오전 10:52:23
* @param height 고정 높이
* @param savePath 저장 경로 및 이름
* @param targetFile 로컬 타겟 파일
* @return 저장 경로를 반환합니다.
* @throws ImageFormatException
* @throwsIOException
*/
public static String resizeByHeight(int height, String savePath, URL targetURL) throws ImageFormatException, IOException {
이미지 = ImageIO.read(targetURL);
int[] size = getSizeByHeight(높이, 이미지);
return resize(크기[0], 크기[1], savePath, 이미지);
}
/**
* <b>기능:</b>
*@authorhoojo
* @createDate 2012-2-3 오전 10:08:47
* @param 인수
* @throwsIOException
* @MalformedURLException 발생
* @throws ImageFormatException
*/
public static void main(String[] args)는 ImageFormatException, MalformedURLException, IOException을 발생시킵니다.
System.out.println(ScaleImageUtils.resize(140, 140, null, 새 URL("http://www.open-open.com/lib/images/logo.jpg")));
ScaleImageUtils.resize(100, 100, ImageQuality.high.getQuality(), null, ImageIO.read(새 URL("http://www.open-open.com/lib/images/logo.jpg")));
}
}
이 글이 모든 사람의 Java 프로그래밍에 도움이 되기를 바랍니다.