我们正在云项目中进行隐私保障的图像重建服务外包。我们想压缩图像,然后使用AES算法加密这个压缩图像并存储在云上。 我们能够使用JAI API压缩图像,但我们无法解压缩此文件。我们使用以下代码进行图像压缩。
private void compressFile(String realPath, File in, String fileName) {
BufferedImage input = null;
try {
if (fileName.endsWith(".jpg") || fileName.endsWith(".JPG")) {
RenderedImage img1 = (RenderedImage) JAI.create("fileload", in.getAbsolutePath());
input = getBufferedImage(fromRenderedToBuffered(img1));
} else if (fileName.endsWith(".gif") || fileName.endsWith(".GIF")) {
RenderedOp img1 = FileLoadDescriptor.create(in.getAbsolutePath(), null, null, null);
input = getBufferedImage(img1.getAsBufferedImage());
} else if (fileName.endsWith(".bmp") || fileName.endsWith(".BMP")) {
// Wrap the InputStream in a SeekableStream.
InputStream is;
try {
is = new FileInputStream(in);
SeekableStream s = SeekableStream.wrapInputStream(is, false);
// Create the ParameterBlock and add the SeekableStream to it.
ParameterBlock pb = new ParameterBlock();
pb.add(s);
// Perform the BMP operation
RenderedOp img1 = JAI.create("BMP", pb);
input = getBufferedImage(img1.getAsBufferedImage());
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else if (fileName.endsWith(".png") || fileName.endsWith(".PNG")) {
// Wrap the InputStream in a SeekableStream.
InputStream is;
try {
is = new FileInputStream(in);
SeekableStream s = SeekableStream.wrapInputStream(is, false);
// Create the ParameterBlock and add the SeekableStream to it.
ParameterBlock pb = new ParameterBlock();
pb.add(s);
// Perform the PNG operation
RenderedOp img1 = JAI.create("PNG", pb);
input = getBufferedImage(img1.getAsBufferedImage());
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
if (input == null) {
return;
}
// Get Writer and set compression
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
if (iter.hasNext()) {
ImageWriter writer = (ImageWriter) iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
float values[] = iwp.getCompressionQualityValues();
iwp.setCompressionQuality(values[2]);
String newName = realPath + "/" + "Compress" + fileName;
File outFile = new File(newName);
FileImageOutputStream output;
output = new FileImageOutputStream(outFile);
writer.setOutput(output);
IIOImage image = new IIOImage(input, null, null);
System.out.println(
"Writing " + values[2] + "%");
writer.write(null, image, iwp);
input.flush();
output.flush();
output.close();
writer.dispose();
writer = null;
outFile = null;
image = null;
input = null;
output = null;
}
} catch (FileNotFoundException finfExcp) {
System.out.println(finfExcp);
} catch (IOException ioExcp) {
System.out.println(ioExcp);
}
}
private BufferedImage getBufferedImage(Image img) {
int w = img.getWidth(null);
int h = img.getHeight(null);
int thumbWidth = 330;
int thumbHeight = 250;
// if width is less than 330 keep the width as it is.
if (w < thumbWidth) {
thumbWidth = w;
}
// if height is less than 250 keep the height as it is.
if (h < thumbHeight) {
thumbHeight = h;
}
//if less than 330*250 then do not compress
if (w > 330 || h > 250) {
double imageRatio = (double) w / (double) h;
double thumbRatio = (double) thumbWidth / (double) thumbWidth;
if (thumbRatio < imageRatio) {
thumbHeight = (int) (thumbWidth / imageRatio);
} else {
thumbWidth = (int) (thumbHeight * imageRatio);
}
}
// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
BufferedImage bi = new BufferedImage(thumbWidth, thumbHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
g2d.drawImage(img, 0, 0, thumbWidth, thumbHeight, null);
g2d.dispose();
return bi;
}
public static BufferedImage fromRenderedToBuffered(RenderedImage img) {
if (img instanceof BufferedImage) {
return (BufferedImage) img;
}
ColorModel cm = img.getColorModel();
int w = img.getWidth();
int h = img.getHeight();
WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
Hashtable props = new Hashtable();
String[] keys = img.getPropertyNames();
if (keys != null) {
for (int i = 0; i < keys.length; i++) {
props.put(keys[i], img.getProperty(keys[i]));
}
}
BufferedImage ret = new BufferedImage(cm, raster,
isAlphaPremultiplied,
props);
img.copyData(raster);
cm = null;
return ret;
}