일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- spring integration
- 자바
- JVM
- spring data jpa
- oracle
- 웹 크롤링
- 역학
- 소프트웨어공학
- kotlin
- 시스템
- 자바암호
- write by chatGPT
- chatGPT's answer
- 리눅스
- write by GPT-4
- flet
- jpa
- 코틀린
- 유닉스
- Database
- android
- NIO
- python
- 인프라
- GPT-4's answer
- 자바네트워크
- 데이터베이스
- 파이썬
- Java
- 고전역학
- Today
- Total
기억을 지배하는 기록
Nail 이미지 생성 클래스 본문
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* <pre>
* 사용법 java nailImgMaker [inPutFileName] [outPutFileName] [이미지 가로크기 픽셀단위] [화질 0.0F~1.0F]
* 예) java nailImgMaker craftlee.gif nail_craftlee.jpg 150 0.5F
*
* outPutFile은 JPG파일만 됩니다.
* </pre>
*/
public class nailImgMaker {
public final MediaTracker tracker = new MediaTracker( new Component() {} );
public Image setSize( Image image, int width, int height ) {
return setSize( image, width, height, java.awt.Image.SCALE_DEFAULT );
}
public Image setSize( Image image, int width, int height, int hints ) {
return image.getScaledInstance( width, height, hints );
}
public void checkImage( Image image )
throws InterruptedException {
waitForImage( image );
int imageWidth = image.getWidth( null );
int imageHeight = image.getHeight( null );
if ( imageWidth < 1 || imageHeight < 1)
throw new IllegalArgumentException( "File is Not ImageFile" );
}
private void waitForImage( Image image )
throws InterruptedException {
tracker.addImage( image, 0 );
tracker.waitForID( 0 );
tracker.removeImage(image, 0);
}
public void encodeJPEG( OutputStream outputStream,
Image outputImage,
float outputQuality )
throws IOException {
int outputWidth = outputImage.getWidth( null );
int outputHeight = outputImage.getHeight( null );
BufferedImage bi = new BufferedImage( outputWidth, outputHeight,
BufferedImage.TYPE_INT_RGB );
Graphics2D biContext = bi.createGraphics();
biContext.drawImage( outputImage, 0, 0, null );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( outputStream );
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam( bi );
jep.setQuality( outputQuality, true );
encoder.encode( bi, jep );
outputStream.flush();
}
public static void main( String [] args ) {
if (args.length < 4) {
System.out.println( "use: java ImageSizer inputFile outputFile outputWidth outputQuality" );
System.exit(0);
}
String inputFileName = args[0];
String outputFileName = args[1];
int outputWidth = Integer.parseInt( args[2] );
float outputQuality = Float.parseFloat( args[3] );
try {
new nailImgMaker().nailMake(inputFileName,
outputFileName,
outputWidth,
outputQuality);
} catch(Exception e) {
e.printStackTrace();
}
}
public void nailMake(String inputFileName,
String outputFileName,
int outputWidth)
throws IOException, InterruptedException {
nailMake(inputFileName, outputFileName, outputWidth, 0.5F);
}
public void nailMake(String inputFileName,
int outputWidth)
throws IOException, InterruptedException {
int lastSlash = inputFileName.lastIndexOf(".");
String outputFileName = inputFileName.substring( 0, lastSlash + 1 )+"_nail.jpg";
nailMake(inputFileName, outputFileName, outputWidth, 0.5F);
}
public void nailMake(String inputFileName,
String outputFileName,
int outputWidth,
float outputQuality)
throws IOException, InterruptedException {
if ( outputWidth < 1 )
throw new IllegalArgumentException( "output width is >= 1" );
if (( outputQuality < 0.0F ) || ( outputQuality > 1.0F ))
throw new IllegalArgumentException( "0.0F <= output quality <= 1.0F " );
Image inputImage = Toolkit.getDefaultToolkit().getImage( inputFileName );
checkImage( inputImage );
saveJPG(setSize(inputImage, outputWidth, -1), outputQuality, outputFileName);
}
private void saveJPG(Image outputImage,
float outputQuality,
String outputFileName)
throws FileNotFoundException,
IOException {
FileOutputStream fos = new FileOutputStream(outputFileName);
encodeJPEG(fos, outputImage, outputQuality);
fos.close();
}
}
'오래된글 > Java' 카테고리의 다른 글
Powerful Fluent Interfaces is Now Easier than Ever: op4j 1.0 Released (0) | 2018.04.09 |
---|---|
non-blocking 소켓 (0) | 2018.04.09 |
Lessons learned from getting .NET to REST with Java (0) | 2018.04.09 |
JUnitParams (0) | 2018.04.09 |
JNI Hello World Program (0) | 2018.04.09 |