Il n'y a pas beaucoup d'articles sur le contenu du format d'image Java sur Internet, et ils ne sont pas très terminés. L'éditeur a collecté trois codes de conversion de format d'image Java et les a partagés avec vous:
Le premier paragraphe: code de conversion de format d'image Java
import java.awt.image.bufferedImage; import java.io.file; import java.io.ioexception; import java.util.scanner; import javax.imageio. *; public class FormatConversion {public static final string jpg = "jpg"; public statique final string gif = "gif"; String final statique publique png = "png"; chaîne finale statique publique BMP = "BMP"; public static void main (String [] args) {String src = "e: // 2."; Nouveau formatConversion (). Conversion (jpg, png, src); // jpg se convertit en png new FormatConversion (). Conversion (jpg, gif, src); // jpg converties en gif new formatConversion (). Conversion (jpg, bmp, src); // jpg converts à bmp // converts) Lorsque vous appelez la fonction de conversion} // inputformat représente le format d'origine, OutputFormat représente le format converti Public void Conversion (String inputFormat, String outputFormat, String src) {try {file input = new File (src + inputFormat); BufferedImage bim = imageo.read (entrée); Fichier Output = nouveau fichier (SRC + OutputFormat); ImageIo.Write (BIM, OutputFormat, Output); } catch (ioException e) {e.printStackTrace (); }}}Deuxième paragraphe: code de conversion de format d'image Java
Importer java.awt.image.bufferedImage; import java.io.file; import java.io.ioException; import javax.imageio.imageio; public class converterUtil {// jgp format public static final chaîne jpg = "jpeg"; // GIF Format public static final string gif = "gif"; // Format png public static final string png = "png"; // Format bmpat public statique final statique bmp = "bmp"; Le convertisseur de void statique public (fichier imgfile, format de chaîne, format de fichier) lève ioException {bufferedImage bimg = imageo.read (imgfile); Imageo.write (bimg, format, formatfile); } public static void main (string [] args) {try {// converti en jgp converterutil.converter (nouveau fichier ("c: //psb.jpg"), jpg, nouveau fichier ("c: //psb2.jpg")); // converti en gif converterUtil.converter (nouveau fichier ("c: //psb.jpg"), gif, nouveau fichier ("c: //psb2.gif")); // converti en png converterUtil.converter (nouveau fichier ("c: //psb.jpg"), png, nouveau fichier ("c: //psb2.png")); // converti en bmp converterUtil.converter (nouveau fichier ("c: //psb.jpg"), bmp, nouveau fichier ("c: //psb2.bmp")); } catch (ioException e) {e.printStackTrace (); }}}Paragraphe 3: code de conversion de format d'image Java
package cn.xsbiz.servlet.test; Importer java.io.fileOutputStream; import java.io.outputStream; import javax.media.jai.jai; import javax.media.jai.renderedop; import com.sun.media.jai.codec.bmpencomeparam; import com.sun.media.jai.codec.ImageCodec; import com.sun.media.jai.codec.Imageencoder; Import com.sun.media.jai.codec.jPegencodeParam; / * * Peut convertir les formats entre jpg / tif / bmp et autres images les uns aux autres * / public class {public static void main (String [] args) lève une exception {/ * if au format jpg * / string input2 = "d: /img/a.tif"; String output2 = "d: /img/a.jpg"; RenderedOp Src2 = Jai.Create ("FileLoad", Input2); OutputStream OS2 = new FileOutputStream (Output2); JpeGencodeParam param2 = new JpenceCodeParam (); // Spécifiez le type de format, JPG appartient au type JPEG ImageEncoder enc2 = ImageCodec.CreateImageEncoder ("JPEG", OS2, param2); ENC2CODOCK (SRC2); OS2.Close (); / * if converti au format bmp * / string inputfile = "d: /img/b.tif"; String OutputFile = "D: /img/b.bmp"; RendeRedOp Src = Jai.Create ("FileLoad", InputFile); OutputStream OS = new FileOutputStream (OutputFile); BMPENCODEPARAM PARAM = NOUVEAU BMPENCODEPARAM (); ImageEncoder ENC = ImageCodec.CreateImageEncoder ("BMP", OS, param); Enc.Encode (SRC); OS.Close (); // Fermer Stream // Convertir d'autres manières de la même manière}}Les trois codes ci-dessus sont écrits afin de ne pas représenter les performances du code. Vous pouvez distinguer les avantages et les inconvénients de chaque code pendant le processus d'apprentissage, en résumer l'expérience et le maîtriser davantage.
Méthodes pour convertir le format d'image Java.