Java - 만 나이 계산

*** 생년월일을 기준으로 현재 나이 계산 ***
 public int getAge(int birthYear, int birthMonth, int birthDay)
{
        Calendar current = Calendar.getInstance();
        int currentYear  = current.get(Calendar.YEAR);
        int currentMonth = current.get(Calendar.MONTH) + 1;
        int currentDay   = current.get(Calendar.DAY_OF_MONTH);
      
        int age = currentYear - birthYear;
        // 생일 안 지난 경우 -1
        if (birthMonth * 100 + birthDay > currentMonth * 100 + currentDay) 
            age--;
      
        return age;
}

JQuery 화면 로딩 후 버튼 클릭 이벤트 발생시키기


*** 설명 ***
화면 로딩 후 이름이 searchButton인 버튼의 클릭 이벤트를 실행

*** Sample ***
$(window).bind("load", function() {
     $("#searchButton").trigger("click");
});


   



Java - 이미지 크기 변경

String imgSourcePath= "test_image.jpg";       // 원본 이미지 파일명 (경로 포함)
String imgTargetPath= "test_imageNew.jpg";    // 새 이미지 파일명(경로 포함)
String imgFormat = "jpg";                     // 새 이미지 포맷. jpg, gif 등
int newWidth = 640;              // 새 이미지 넓이
int newHeight = 360;             // 새 이미지 높이



imgResize(imgSourcePath, newImgFilePath, imgFormat, newWidth, newHeight);


/**
 * Image Resize
 */
public void imgResize(String imgSourcePath, String imgTargetPath, String imgFormat, int newWidth, int newHeight)
{
    try
    {
        // 원본 이미지 가져오기
        Image imgSrc = ImageIO.read(new File(imgSourcePath));

        // 이미지 리사이즈
        // Image.SCALE_DEFAULT : 기본 이미지 스케일링 알고리즘 사용
        // Image.SCALE_FAST    : 이미지 부드러움보다 속도 우선
        // Image.SCALE_SMOOTH  : 속도보다 이미지 부드러움을 우선
        // Image.SCALE_AREA_AVERAGING  : 평균 알고리즘 사용
        Image resizeImage = imgSrc.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);

        // 새 이미지  저장하기
        BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
        Graphics g = newImage.getGraphics();
        g.drawImage(resizeImage, 0, 0, null);
        g.dispose();
        ImageIO.write(newImage, imgFormat, new File(imgTargetPath));
    }
    catch (Exception e)
    {
    }
}

Java - URL 이미지 파일로 저장하기


String imgUrl = "http://www.a.com/test_image.jpg";  // 이미지 URL
String imgFilePath = "test_image.jpg";              // 저장할 파일명 (경로 포함)
String imgFormat = "jpg";                         // 저장할 이미지 포맷. jpg, gif 등

getImageFromUrl(imgUrl, imgFilePath, imgFormat);






/**
 * Image URL to File
 */
public void getImageFromUrl(String imgUrl, String imgFilePath, String imgFormat)
{
        try
        {
            // Image 가져오기
            BufferedImage image = ImageIO.read(new URL(imgUrl));
                       
            // Image 저장할 파일
            File imgFile = new File(imgFilePath);
           
            // Image 저장
            ImageIO.write(image, imgFormat, imgFile);
        }
        catch (Exception e)
        {
        }
}