Image 객체 및 BufferedImage

2238 단어 F#
하나의 Imge에서 BufferedImage로 바뀝니다. 소스는 다음과 같습니다.

//covert Image object to BufferedImage object
//pic (Image type)
	private BufferedImage convertImageToBuffer(){
		BufferedImage bufferedImage = new BufferedImage(pic
				.getWidth(null), pic.getHeight(null),
				BufferedImage.TYPE_INT_RGB);
		Graphics g = bufferedImage.createGraphics();
		g.drawImage(pic, 0, 0, null);
		g.dispose();
		return bufferedImage;
	}

Jpanel에서 이미지를 가져와 jpg 형식으로 저장합니다.

public static void saveImage(Component com){
		int comH=com.getHeight();
		int comW=com.getWidth();
		BufferedImage bufferImage=new BufferedImage(comW,comH,BufferedImage.TYPE_INT_RGB);
		Graphics2D g2=bufferImage.createGraphics();
		g2.setBackground(Color.white);
		com.paint(g2);
		g2.dispose();
FileFilter filter = new FileFilter() {
			@Override
			public boolean accept(File f) {
				// TODO Auto-generated method stub
				return f.isFile() && f.getName().endsWith("jpg")
						|| f.getName().endsWith("JPG");
			}

			@Override
			public String getDescription() {
				// TODO Auto-generated method stub
				return "JPG or jpg";
			}
		};
		JFileChooser fileChoose = new JFileChooser();
		fileChoose.setFileFilter(filter);
		int result = fileChoose.showSaveDialog(null);
		if (result == JFileChooser.APPROVE_OPTION) {
			File file = fileChoose.getSelectedFile();
			String path = file.getPath();
		try {

				FileOutputStream output = new FileOutputStream(path);
				JPEGImageEncoder jpg = JPEGCodec.createJPEGEncoder(output);
				jpg.encode(bufferImage);
				output.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ImageFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

좋은 웹페이지 즐겨찾기