java - Resize an image in Java and return the image as a BLOB to the Oracle database
问题描述
I have another problem with the interaction between the Oracle database and an embedded Java class. I want to pass an image (as a blob) from the database to the Java class, where the image is resized to the desired height and width and then the image is passed back as a blob to the calling function.
For resizing the image, I also found an example at www.codejava.net that resizes a locally located image and stores it locally again.
This also works very well locally. My cat example - Jpg is resized to 50% of the original size (1024 x 811 WxH), i.e. to 512 x 405. Of course, this also reduces the file size - in this case from 192 KB to 39 KB.
My adapted programme (see attachment HKRCALL.java) also takes the BLOB transferred from the database, determines the size of the original image, calculates 50 % of the width and height and finally returns a blob. This is then stored in a table again. I built a few outputs into my Java program and let them be output via dbms-Output during the execution of the PL/SQL procedure (which in turn calls the Java class) (see below).
Unfortunately, this blob is only 3955 bytes in size.
When I download the returned image from the database, it is indeed Jpeg and the image information also shows the dimensions 512 x 405. Unfortunately, the image is black.
Can someone perhaps take a look at where my error could lie (ImageOutputStream ?)? I am in despair at the moment :-(
Output:
This ist the output:
resizeJpegCall: Size of the given blob: 194334
Original scaledWidth = 1024
Original scaledHeight = 811
50 % scaledWidth = 512
50 % scaledHeight = 405
resizeJpeg: BufferedImage outputImage
resizeJpeg: Graphics2D g2d = outputImage.createGraphics()
resizeJpeg: g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null)
resizeJpeg: g2d.dispose()
ImageIO.write(outputImage, JPG, ios)
Image resized successfully.
Image resized successfully.
Size of the return blob: 3955
import java.sql.*;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import oracle.sql.*;
import oracle.jdbc.driver.*;
import java.sql.Blob;
import javax.imageio.stream.ImageOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
class HKRCALL{
public static java.sql.Blob resizeJpegCall (java.sql.Blob blob ) throws Exception{
// Quality muss zwischen 0.01 und 1.00 liegen, als Parameter wird 0-100 mitgegeben,
// daher hier noch mal durch 100 geteilt
BufferedImage inputImage = ImageIO.read(blob.getBinaryStream());
System.out.println("resizeJpegCall: Groesse des uebergebenen Blob: " + blob.length());
// Quality muss zwischen 0.01 und 1.00 liegen, als Parameter wird 0-100 mitgegeben,
// daher hier noch mal durch 100 geteilt
float percent_float = (float) 0.50;
System.out.println("Original scaledWidth = " + inputImage.getWidth());
System.out.println("Original scaledHeight = " + inputImage.getHeight());
int scaledWidth = (int) (inputImage.getWidth() * percent_float );
int scaledHeight = (int) (inputImage.getHeight() * percent_float );
System.out.println("50 % scaledWidth = " + scaledWidth);
System.out.println("50 % scaledHeight = " + scaledHeight);
// blob fuer den returnwert wird ueber eine DB-Connection erstellt
oracle.jdbc.OracleConnection conn = (oracle.jdbc.OracleConnection)new OracleDriver().defaultConnection();
java.sql.Blob retBlob = conn.createBlob();
try {
// RenderedImage renderedImage = (RenderedImage)bufferedImage;
OutputStream os = retBlob.setBinaryStream(1L);
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
/*
In der Unterfunktion findet der eigentliche Resizen des JPEG statt
*/
boolean result = resizeJpeg (inputImage, ios, scaledWidth, scaledHeight ) ;
if (result) {
// Display message when image is converted
// successfully
System.out.println("Image resized successfully.");
System.out.println("Groesse des Return Blob: " + retBlob.length());
return retBlob;
}
else {
// Display message when image is not
// converted successfully
System.out.println( "Could not resize image.");
}
} catch (IOException e) {
// Display message when excception is thrown
System.out.println("Error during resizing image.");
// Print the line number
// where the exception occured
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
catch(IllegalArgumentException e) {
e.printStackTrace();
}
return retBlob;
}
public static boolean resizeJpeg(BufferedImage inputImage, ImageOutputStream ios, int scaledWidth, int scaledHeight ) throws Exception{
if ( inputImage == null )
return false;
if ( ios == null )
return false;
if ( scaledWidth == 0 )
return false;
if ( scaledHeight == 0 )
return false;
try {
// creates output image
System.out.println("resizeJpeg: BufferedImage outputImage");
BufferedImage outputImage = new BufferedImage(scaledWidth, scaledHeight, inputImage.getType());
// scales the input image to the output image
System.out.println("resizeJpeg: Graphics2D g2d = outputImage.createGraphics()");
Graphics2D g2d = outputImage.createGraphics();
System.out.println("resizeJpeg: g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null)");
g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
System.out.println("resizeJpeg: g2d.dispose()");
g2d.dispose();
System.out.println("ImageIO.write(outputImage, JPG, ios)");
ImageIO.write(outputImage, "jpg", ios);
return true;
} catch (IOException e) {
System.out.println("IOException");
e.printStackTrace();
return false;
}
catch(IllegalArgumentException e) {
e.printStackTrace();
return false;
}
finally {
try {
if (ios != null) {
ios.flush();
ios.close();
System.out.println(
"Image resized successfully.");
//return retBlob;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
解决方案
推荐阅读
- xml - 面临使用 XSL 将 XML 转换为另一个 xml 的问题
- angular - ReferenceError:“未定义谷歌”
- azure-media-services - Azure 媒体播放器广告叠加
- c# - 如何在 C# 中使用 OpenXML 隐藏 Excel 中的行?
- javascript - 摩卡测试超时
- python - 哪些运算符要重载以使 Python 设置为正常运行
- python - Python 脚本 - 使用 mailmerge 将图像插入 docx 文件
- javascript - 在 HTML 中显示/隐藏 UI 元素的问题
- php - 预处理功能未在主题drupal 8上运行
- image - iText7 将 SVG 添加到 PdfDocument 并在 PDF 中正确对齐 SVG 图像