1 //入口 2 public static BufferedImage constructHeatWheelView(int pageWidth, int pageHeight, DoubleHolder scaleHolder) throws ValidateException{ 3 4 BufferedImage bi = new BufferedImage(pageWidth, pageHeight, BufferedImage.TYPE_INT_RGB); 5 Graphics2D g = (Graphics2D) bi.getGraphics(); 6 DrawingHelper.clearGraphics(g, pageWidth, pageHeight); 7 try { 8 9 //1 读取 jpg 格式图片 10 Image srcImage= ImageIO.read(ECSigleton.getInstance().getAppManager().loadImageStream("HeatWheel")); 11 12 //2 旋转 Image 13 bi=getImage_rote270(srcImage); 14 //3 按比例缩放 15 bi=scale(bi,0.75); 16 17 } catch (IOException e) { 18 // TODO Auto-generated catch block 19 e.printStackTrace(); 20 } 21 return bi; 22 } 23 24 public static BufferedImage getImage_rote270(Image img) 25 { 26 int width = img.getWidth(null); 27 int height = img.getHeight(null); 28 BufferedImage newImg = new BufferedImage(height, width, 1); 29 Graphics g = newImg.getGraphics(); 30 Graphics2D g2d = (Graphics2D) g; 31 g2d.rotate(Math.toRadians(-90), 0, 0); 32 g2d.drawImage(img, -width, 0, width, height, null); 33 g2d.dispose(); 34 return newImg; 35 } 36 37 public static BufferedImage scale(BufferedImage src, double scale) { 38 39 int width = src.getWidth(); // 得到源图宽 40 int height = src.getHeight(); // 得到源图长 41 42 width = (int)(width * scale); 43 height = (int)(height * scale); 44 45 Image image = src.getScaledInstance(width, height, 46 Image.SCALE_DEFAULT); 47 BufferedImage tag = new BufferedImage(width, height, 48 BufferedImage.TYPE_INT_RGB); 49 Graphics g = tag.getGraphics(); 50 g.drawImage(image, 0, 0, null); // 绘制缩小后的图 51 g.dispose(); 52 //ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流 53 return tag; 54 } 55
java读取jpg图片旋转按比例缩放
时间: 2024-10-10 09:31:24