Convertendo PDF em JPG

2 respostas
anderson.bonavides

Pessoal tenho um método que le um PDF e converte em imagem. O problema é que quando eu debugo tudo funciona 100% mas quando o projeto é chamado sem a forma de debug a imagem não é gerada mais o arquivo sim. É criado o arquivo mas sem seu conteúdo.

Segue meu método que trata de gerar a imagem.
public File resize(BufferedImage img, File resizedFile, int newWidth, float quality) throws IOException {

		if (quality < 0 || quality > 1) {
			throw new IllegalArgumentException("Quality has to be between 0 and 1");
		}

		// ImageIcon ii = new ImageIcon(img.getCanonicalPath());
		Image i = img;
		Image resizedImage = null;

		int iWidth = i.getWidth(null);
		int iHeight = i.getHeight(null);

		/*
		 * if (iWidth > iHeight) { resizedImage = i.getScaledInstance(newWidth,
		 * (newWidth iHeight) / iWidth, Image.SCALE_SMOOTH); } else {
		 * resizedImage = i.getScaledInstance((newWidth iWidth) / iHeight,
		 * newWidth, Image.SCALE_SMOOTH); }
		 */

		resizedImage = i.getScaledInstance(newWidth, 550, Image.SCALE_SMOOTH);
		
		// This code ensures that all the pixels in the image are loaded.
		Image temp = new ImageIcon(resizedImage).getImage();
		
		// Create the buffered image.
		BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
		

		// Copy image to buffered image.
		Graphics g = bufferedImage.createGraphics();

		// Clear background and paint the image.
		g.setColor(Color.gray);
		g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
		g.drawImage(temp, 0, 0, null);
		g.dispose();

		// Soften.
		float softenFactor = 0.05f;
		float[] softenArray = { 0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
		Kernel kernel = new Kernel(3, 3, softenArray);
		ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
		bufferedImage = cOp.filter(bufferedImage, null);

		/*
		 * É necessário escrever o arquivo no disco pois o JPEGImageEncoder 
		 * recebe um FileOutputStream como parâmetro.
		 */

		// Write the jpeg to a file.
		FileOutputStream fis = new FileOutputStream(resizedFile);

		// Encodes image as a JPEG data stream
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fis);

		JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);

		param.setQuality(quality, true);

		encoder.setJPEGEncodeParam(param);
		encoder.encode(bufferedImage);

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		out.writeTo(fis);
		fis.flush();
		fis.close();

		byte[] b = new byte[(int) resizedFile.length()];
		File f = new File(resizedFile.getAbsolutePath());
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(f);
			fos.write(b);
		} finally {
			if (fos != null) {
				try {
					fos.close();
					fos.flush();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return f;
	}

Fico grato pela ajuda

2 Respostas

T

Pra que serve isto? Parece macumba

ByteArrayOutputStream out = new ByteArrayOutputStream();  
     out.writeTo(fis);
anderson.bonavides

Foi a tentativa de algum sucesso.
Alás, qual o problema com isto?

Criado 27 de julho de 2009
Ultima resposta 27 de jul. de 2009
Respostas 2
Participantes 2