Я использую эти служебные методы для преобразования jpg в bmp (изменен размер и перевернут)
public static Image rotateImage(Image img,double degree){
BufferedImage bufImg = toBufferedImage(img);
double angle = Math.toRadians(degree);
return tilt(bufImg,angle);
}
public static BufferedImage tilt(BufferedImage image, double angle) {
double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
int w = image.getWidth(), h = image.getHeight();
int neww = (int)Math.floor(w*cos+h*sin), newh = (int)Math.floor(h*cos+w*sin);
GraphicsConfiguration gc = getDefaultConfiguration();
BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.OPAQUE);
Graphics2D g = result.createGraphics();
g.translate((neww-w)/2, (newh-h)/2);
g.rotate(angle, w/2, h/2);
g.drawRenderedImage(image, null);
g.dispose();
return result;
}
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
image = new ImageIcon(image).getImage();
boolean hasAlpha = hasAlpha(image);
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
if (hasAlpha) {
transparency = Transparency.BITMASK;
}
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null),
image.getHeight(null), transparency);
} catch (HeadlessException e) {
return null;
}
if (bimage == null) {
int type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha) {
type = BufferedImage.TYPE_INT_ARGB;
}
bimage = new BufferedImage(image.getWidth(null),
image.getHeight(null), type);
}
Graphics g = bimage.createGraphics();
g.drawImage(image, 0, 0, Color.WHITE, null);
g.dispose();
return bimage;
}
public static GraphicsConfiguration getDefaultConfiguration() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
return gd.getDefaultConfiguration();
}
Я обрабатываю это изображение:
https://tecordeon.com/wp-content/uploads/2016/01/security-testing.png
и получим такой результат:
![enter image description here](https://i.stack.imgur.com/OyRXz.png)
Я должен сделать это в Java 7 из-за устаревших системных ограничений.
Когда я попробовал использовать 64-битную версию, это выглядит хорошо. Когда я пытаюсь сделать это в 32-разрядной версии (обернутый как сервис, он действительно выглядит плохо).
Какую часть кода я могу переписать?
Поток кода выглядит следующим образом:
private String prepareImageForPrinting(String imagePath) {
String fileName = null;
try {
Image myImage = new ImageIcon(imagePath).getImage();
//FIXME ugly printing for 32-bit
Image rotated = ImageUtility.rotateImage(myImage, -90);
BufferedImage bufferedImage = ImageUtility.toBufferedImage(rotated);
BufferedImage scaledBuffImg = ImageUtility.rescale(bufferedImage,
this.imageHeight, this.imageWidth);
BufferedImage paddedScaledBuffImg = ImageUtility.addPadding(
scaledBuffImg, newheight, newWidth,paddingWidth,paddingHeight);
File f;
try {
fileName = imagePath.replace(".png", ".bmp");
f = new File(fileName);
ImageIO.write(paddedScaledBuffImg, "bmp", f);
} finally {
myImage.flush();
rotated.flush();
bufferedImage.flush();
scaledBuffImg.flush();
paddedScaledBuffImg.flush();
}
} catch (Exception e) {
//log e
}
return fileName;
}