当前位置: 代码迷 >> J2SE >> java 对图片的读写有关问题
  详细解决方案

java 对图片的读写有关问题

热度:59   发布时间:2016-04-24 12:43:17.0
java 对图片的读写问题
大家好,java中,怎样实现图片的翻转(180度)?

 

------解决方案--------------------
如果用Java做,下面有一个小例子
Java code
import java.awt.*;import java.awt.image.*;import java.awt.event.*;import javax.swing.*;import java.net.URL;import javax.swing.event.*;public class ImageRotate extends JFrame{    private FImage2D originalImage, resultImage;  private JLabel originalImageLabel, resultImageLabel;      public ImageRotate(String name){    // Initialize the GUI    originalImage = new FImage2D( loadImage( name ) );    resultImage = originalImage.invert();         Container pane = getContentPane();    originalImageLabel = new JLabel( new ImageIcon(originalImage.bufferedImage() ));    resultImageLabel = new JLabel( new ImageIcon(resultImage.bufferedImage() ));        pane.add( originalImageLabel, BorderLayout.WEST );    pane.add( resultImageLabel, BorderLayout.EAST );            pane.add( new JLabel("angle:") );    JSlider angleSlider = new JSlider(JSlider.HORIZONTAL, -180, 180, 0);    angleSlider.addChangeListener(new angleListener() );    angleSlider.setMajorTickSpacing(90);    angleSlider.setMinorTickSpacing(10);    angleSlider.setPaintTicks(true);    angleSlider.setPaintLabels(true);    angleSlider.setBorder(BorderFactory.createEmptyBorder(0,0,20,20));    pane.add( angleSlider );      }    class angleListener implements ChangeListener {    public void stateChanged(ChangeEvent e) {      JSlider source = (JSlider)e.getSource();      if (!source.getValueIsAdjusting()) {        double angle = (double)source.getValue();        resultImage = originalImage.rotate( (float)(angle * Math.PI / 180.0) );        resultImageLabel.setIcon( new ImageIcon(resultImage.bufferedImage()) );      }    }  }    public BufferedImage loadImage( String name ) {    URL url = ImageRotate.class.getResource(name);        Image image = this.getToolkit().getImage(url);;    try {      MediaTracker tracker = new MediaTracker( this );      tracker.addImage( image, 0 );      tracker.waitForID(0);    } catch ( Exception e ) {      System.err.println(e);      System.exit(1);    }    int width = image.getWidth(this);    int height = image.getHeight(this);        BufferedImage input = new BufferedImage( width, height,                                            BufferedImage.TYPE_INT_RGB );    input.createGraphics().drawImage( image, 0, 0, null );    return input;  }  public static void main( String[] argv ){    if( argv.length == 0 ) {      try {        JFrame  frame = new ImageRotate( "cameraman.jpg" );        frame.pack();        frame.setVisible(true);      }      catch (Exception e) {        System.err.println(e);        System.exit(1);      }    }    else {      System.err.println("usage: java ImageRotate" );      System.exit(1);    }  }}
------解决方案--------------------
mark

------解决方案--------------------
用JAI就可以了
  相关解决方案