Kode kanggo ngompres JPG ing Java

Pustaka Java kinerja dhuwur kanggo ngoptimalake isi lan ukuran JPG njaga kualitas asli lan data dasar

Iki minangka solusi profesional kanggo ngompres JPG nggunakake Java. Gunakake API optimasi JPG kanggo nggawe piranti lunak portabel, mantep lan aman ing Jawa. Coba online gratis!

Ndeleng potongan kode

Kompres file JPG ing Java

Nggawe file JPG gedhe luwih cilik tanpa mundhut kualitas. Mbusak data sing ora perlu lan ora digunakake. Nyilikake ukuran file JPG ing kode. Pustaka Java nyedhiyakake pangembang API terpadu kanggo ngoptimalake isi JPG.

Ngurangi ukuran file JPG nggunakake Java

Perpustakaan Java minangka solusi mandiri lan ora mbutuhake piranti lunak pihak katelu sing diinstal.

Kompresi lossless lan optimasi isi jero digawe gampang karo 'Aspose.Words for Java'. Conto ing ngisor iki nuduhake carane ngoptimalake isi file JPG ing Java:

Conto kode ing Java kanggo ngompres JPG file
Unggah file sing pengin dioptimalake / kompres
Kode roto
Pilih format target saka dhaptar
50%
// Repository path: https://releases.aspose.com/java/repo/
// Maven, where 'ver' - Aspose.Words version number, for example, 24.4.
<dependency>
  <groupId>com.aspose</groupId>
  <artifactId>aspose-words</artifactId>
  <version>ver</version>
  <classifier>jdk17</classifier>
</dependency>
Nyalin
// Repository path: https://releases.aspose.com/java/repo/
// Gradle, where 'ver' - Aspose.Words version number, for example, 24.4.
compile(group: 'com.aspose', name: 'aspose-words', version: 'ver', classifier: 'jdk17')
Nyalin
// Repository path: https://releases.aspose.com/java/repo/
// Ivy, where 'ver' - Aspose.Words version number, for example, 24.4.
<dependency org="com.aspose" name="aspose-words" rev="ver">
 <artifact name="aspose-words" m:classifier="jdk17" ext="jar"/>
</dependency>
Nyalin
// Repository path: https://releases.aspose.com/java/repo/
// Sbt, where 'ver' - Aspose.Words version number, for example, 24.4.
libraryDependencies += "com.aspose" % "aspose-words" % "ver"
Nyalin
import com.aspose.words.*;

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Shape shape = builder.insertImage("Input.jpg");

ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.JPEG);
saveOptions.setJpegQuality(50);
shape.getShapeRenderer().save("Output.jpg", saveOptions);
import com.aspose.words.*; Document doc = new Document("Input.jpg"); doc.cleanup(); NodeCollection nodes = doc.getChildNodes(NodeType.SHAPE, true); for (Shape shape : (Iterable<Shape>) nodes) { if (shape.isImage()) { // Iku nganti pangembang kanggo milih perpustakaan kanggo komprèsi gambar. BufferedImage image = ImageIO.read(shape.getImageData().toStream()); // ... // Compress gambar lan nyetel maneh menyang wangun. shape.getImageData().setImage("yourCompressedImage"); } } OoxmlSaveOptions saveOptions = new PdfSaveOptions(); saveOptions.setCacheBackgroundGraphics(true); doc.save("Output.jpg", saveOptions); import com.aspose.words.*; Document doc = new Document("Input.jpg"); doc.cleanup(); NodeCollection nodes = doc.getChildNodes(NodeType.SHAPE, true); for (Shape shape : (Iterable<Shape>) nodes) { if (shape.isImage) { // Iku nganti pangembang kanggo milih perpustakaan kanggo komprèsi gambar. BufferedImage image = ImageIO.read(shape.getImageData().toStream()); // ... // Compress gambar lan nyetel maneh menyang wangun. shape.getImageData().setImage("yourCompressedImage"); } } doc.save("Output.jpg"); import com.aspose.words.*; Document doc = new Document("Input.jpg"); doc.cleanup(); NodeCollection nodes = doc.getChildNodes(NodeType.SHAPE, true); for (Shape shape : (Iterable<Shape>) nodes) { if (shape.isImage()) { // Iku nganti pangembang kanggo milih perpustakaan kanggo komprèsi gambar. BufferedImage image = ImageIO.read(shape.getImageData().toStream()); // ... // Compress gambar lan nyetel maneh menyang wangun. shape.getImageData().setImage("yourCompressedImage"); } } OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(); saveOptions.setCompressionLevel(CompressionLevel.MAXIMUM); doc.save("Output.jpg", saveOptions); import com.aspose.words.*; Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Shape shape = builder.insertImage("Input.jpg"); ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.JPEG); saveOptions.setJpegQuality(50); shape.getShapeRenderer().save("Output.jpg", saveOptions);
Kode roto

Cara ngompres JPG Ing Java

  1. Pasang Aspose.Words for Java.
  2. Tambah referensi perpustakaan (ngimpor perpustakaan) menyang proyek Java sampeyan.
  3. Bukak JPG berkas sumber ing Java.
  4. Kompres JPG tanpa kelangan kualitas.
  5. Telpon metode Document.cleanup(). Kode asil bisa beda-beda gumantung saka format file output.
  6. Entuk asil sing dikompres minangka JPG file.

Pustaka Java kanggo ngoptimalake lan ngompres JPG file

Kita dadi tuan rumah paket Java ing repositori Maven. 'Aspose.Words kanggo Java' JAR umum sing ngemot kode byte. Tututi pandhuan langkah demi langkah babagan carane nginstal ing lingkungan pangembang Java.

Syarat Sistem

Java SE 7 Java sing luwih anyar didhukung. Kita uga nyedhiyakake paket kapisah kanggo Java SE 6 yen sampeyan wajib nggunakake JRE ketinggalan jaman iki.

Java kita lintas platform lan mbukak kabeh sistem operasi kanthi JVM, kalebu Microsoft Windows, Linux, macOS, Android lan iOS.

Kanggo informasi babagan katergantungan paket opsional, kayata JogAmp JOGL, Harfbuzz font Java Advanced Imaging JAI, waca Dokumentasi Produk.

Optimasi format file liyane sing didhukung

Sampeyan bisa ngoptimalake file ing akeh format file liyane:

5%

Langganan kanggo Aspose Update Product

Entuk buletin saben wulan lan tawaran langsung dikirim menyang kothak layang.

© Aspose Pty Ltd 2001-2024. Kabeh hak dilindhungi undhang-undhang.