用bufferimage读取图片并操做图片太慢了,求方法?
程序代码:
import java.awt.BorderLayout; import java.awt.Color; import java.import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import import import javax.imageio.ImageIO; import javax.sound.sampled.ReverbType; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Test extends JFrame{ private BufferedImage image; private JButton jButton; private JPanel jPanel; private ImageIcon imageIcon; private File f; private int height,width; public Test(){ super("测试"); f=new File("C:/Users/wenguang/Desktop/新建文件夹/33.jpg"); try { image=ImageIO.read(f); imageIcon=new ImageIcon(image); width = imageIcon.getIconWidth(); height = imageIcon.getIconHeight(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if (width > 1000) { width = 1276; height = 850; } imageIcon.setImage(imageIcon.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT)); jPanel=new JPanel(new BorderLayout()); JLabel jLabel=new JLabel(imageIcon); jPanel.add(jLabel,BorderLayout.CENTER); //jPanel.setBackground(new Color(220,190,195)); jButton=new JButton("rotate"); jButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub ImageChange change= new ImageChange(); try { remove(jPanel); image=ImageIO.read(change.rotateImg(image, 90, Color.red));//用IO流来读取图片 imageIcon=new ImageIcon(image);//转换类型 width = imageIcon.getIconWidth();//获取大小 height = imageIcon.getIconHeight(); if (width > 1000) { width = 1276; height = 850; } imageIcon.setImage(imageIcon.getImage().getScaledInstance(width, height,//设置图片的大小 Image.SCALE_DEFAULT)); jPanel=new JPanel(new BorderLayout()); JLabel jLabel=new JLabel(imageIcon);/*添加图片到面板中**/ jPanel.add(jLabel,BorderLayout.CENTER); //jPanel.setBackground(new Color(220,190,195)); getContentPane().add(jButton,new BorderLayout().SOUTH); getContentPane().add(jPanel,new BorderLayout().CENTER); validate(); //System.err.println("11111"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); getContentPane().add(jButton,new BorderLayout().SOUTH); getContentPane().add(jPanel,new BorderLayout().CENTER); setBounds(100, 100, 1276, 850); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { new Test(); } } import java.awt.Color; import java.awt.Graphics2D; import java.awt.Transparency; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import import import import import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; public class ImageChange { public static InputStream rotateImg(BufferedImage image, int degree, Color bgcolor) throws IOException { int iw = image.getWidth();//原始图象的宽度 int ih = image.getHeight();//原始图象的高度 int w = 0; int h = 0; int x = 0; int y = 0; degree = degree % 360; if (degree < 0) degree = 360 + degree;//将角度转换到0-360度之间 double ang = Math.toRadians(degree);//将角度转为弧度 /** *确定旋转后的图象的高度和宽度 */ if (degree == 180 || degree == 0 || degree == 360) { w = iw; h = ih; } else if (degree == 90 || degree == 270) { w = ih; h = iw; } else { int d = iw + ih; w = (int) (d * Math.abs(Math.cos(ang))); h = (int) (d * Math.abs(Math.sin(ang))); } x = (w / 2) - (iw / 2);//确定原点坐标 y = (h / 2) - (ih / 2); BufferedImage rotatedImage = new BufferedImage(w, h, image.getType()); Graphics2D gs = (Graphics2D)rotatedImage.getGraphics(); if(bgcolor==null){ rotatedImage = gs.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT); }else{ gs.setColor(bgcolor); gs.fillRect(0, 0, w, h);//以给定颜色绘制旋转后图片的背景 } AffineTransform at = new AffineTransform(); at.rotate(ang, w / 2, h / 2);//旋转图象 at.translate(x, y); AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC); op.filter(image, rotatedImage); image = rotatedImage; ByteArrayOutputStream byteOut= new ByteArrayOutputStream(); ImageOutputStream iamgeOut = ImageIO.createImageOutputStream(byteOut); ImageIO.write(image, "jpg", iamgeOut); InputStream inputStream = new ByteArrayInputStream(byteOut.toByteArray()); return inputStream; } }