Problème avec la fonction JPEGCodec.createJPEGEncoder
Bonjour à tous,
J'ai un problème avec la fonction JPEGCodec.createJPEGEncoder qui ne semble pas fonctionner. Je vous met le code et continue de vous expliquer:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| public class MyImage {
int width;
int height;
String cheminImage;
String nomImage;
String cheminImageCryptee;
String nomImageCryptee;
public MyImage(String cheminImage, String nomImage,boolean bouton) {
this.nomImage=nomImage;
this.cheminImage=cheminImage;
this.cheminImageCryptee="";
String libelle = "Image : ";
String titre = libelle+nomImage;
MaFenetre fen = new MaFenetre(cheminImage, nomImage,bouton) ;
fen.setTitle(titre);
if(titre.length() >= 50){
fen.setMinimumSize(new Dimension(500, 150));
}else{
fen.setMinimumSize(new Dimension(500+titre.length(), 150));
}
this.width=fen.pan.photo.getWidth(null);
this.height=fen.pan.photo.getHeight(null);
fen.setSize(fen.pan.photo.getWidth(null)+20, fen.pan.photo.getHeight(null)+80);
fen.setResizable(false);
fen.setVisible(true);
}
public void crypter() throws FileNotFoundException, IOException {
int i;
int posPoint = cheminImage.lastIndexOf('.');
String cheminAbsolu = cheminImage.substring(0,posPoint);
String extension = cheminImage.substring(posPoint + 1);
cheminImageCryptee=cheminAbsolu+"_crypte."+extension;
posPoint = nomImage.lastIndexOf('.');
String nomAbsolu = nomImage.substring(0,posPoint);
nomImageCryptee=nomAbsolu+"_crypte."+extension;
Toolkit tk = Toolkit.getDefaultToolkit();
Image img = tk.getImage(cheminImage);
int w = -1;
w = img.getWidth(null);
if(w != -1){
System.out.println(w);
}
int h = img.getHeight(null);
int [] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img,0,0,w,h,pixels,0,w);
try {
pg.grabPixels();
}
catch(InterruptedException ie) {
ie.printStackTrace();
}
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.drawImage(bi, 0, 0, width, height, null);
BufferedOutputStream fos = null;
fos = new BufferedOutputStream(new FileOutputStream(cheminImageCryptee));
JPEGImageEncoder jpeg = null;
jpeg = JPEGCodec.createJPEGEncoder(fos);
jpeg.encode(bi);
fos.close();
}
} |
Dans la fonction crypter(), toutes les variables semblent bien avoir été affectées excepté la variable JPEGImageEncore jpeg qui est restée à null. Par la suite, la fonction encode nous renvoie une exception de pointeur null:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javaapplication1.MyImage.crypter(MyImage.java:92)
at javaapplication1.Principal.jButton8ActionPerformed(Principal.java:612)
at javaapplication1.Principal.access$700(Principal.java:36)
at javaapplication1.Principal$8.actionPerformed(Principal.java:321)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
at java.awt.Component.processMouseEvent(Component.java:6108)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:5873)
at java.awt.Container.processEvent(Container.java:2105)
at java.awt.Component.dispatchEventImpl(Component.java:4469)
at java.awt.Container.dispatchEventImpl(Container.java:2163)
at java.awt.Component.dispatchEvent(Component.java:4295)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
at java.awt.Container.dispatchEventImpl(Container.java:2149)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4295)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138) |
Je précise que je développe avec Netbeans sous Linux. Merci d'avance pour vos réponses.