0
나는 itext jar로 pdf 파일에 여러 고해상도 이미지를 추가하려는 안드로이드 응용 프로그램을 개발 중입니다. 메모리 부족 예외가 발생합니다. 너비와 높이에 더 작은 값을 설정하면 잘 동작합니다. 이것은 비동기 백그라운드 태스크 클래스 내에서 구현됩니다. 소스 코드는 다음과 같습니다.android에서 itext로 PDF 문서에 여러 이미지 추가
fos = new FileOutputStream(pdfFilePath);
//Rectangle pagesize = new Rectangle(595.44f, 841.68f);
document = new Document(PageSize.A4, 0f, 0f, 0f, 0f);
PdfWriter.getInstance(document,new FileOutputStream(pdfFilePath));
fileArray = imageDirectory.listFiles();
Log.d("Files", "Size: "+ fileArray.length);
for (i=0; i < fileArray.length; i++){
document.open();
bMap= BitmapFactory.decodeFile(fileArray[i].getPath(), null);
bMap = Bitmap.createScaledBitmap(bMap, 2339, 1654, false);
matrix = new Matrix();
matrix.postRotate(90);
bMap = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), matrix, false);
bMap = toGrayscale(bMap);
stream = new ByteArrayOutputStream();
bMap.compress(Bitmap.CompressFormat.JPEG, 25, stream);
byteArray = stream.toByteArray();
document.add(Image.getInstance(byteArray));
Log.e("Files", "FileName:" + fileArray[i].getName());
bMap = null;
matrix = null;
byteArray = null;
stream = null;
document.close();
}
fos.close();
이것은 실제로 iText 질문이 아닙니다. 'Bitmap'은 iText 객체가 아닙니다. 이미지 처리에는 CPU와 메모리가 필요합니다. 이미지 크기를 줄일 때 코드가 작동한다고 말하면 코드에 문제가 없음을 증명할 수 있습니다. 더 큰 크기의 이미지는 더 많은 리소스가 필요합니다. 이러한 자원을 사용할 수 없으면 예외가 발생합니다. –