PPTX DOCX XLSX PDF ODP
Aspose.Imaging  สำหรับ Java
JPG

เปลี่ยนพื้นหลังใน JPG ผ่าน Java

สร้างแอป Java ของคุณเองเพื่อเปลี่ยนพื้นหลังในไฟล์ JPG โดยใช้ API ฝั่งเซิร์ฟเวอร์

วิธีเปลี่ยนพื้นหลังในไฟล์ JPG โดยใช้ Java

บ่อยครั้ง การได้ภาพในอุดมคติจำเป็นต้องเปลี่ยนพื้นหลัง เพื่อให้ได้เอฟเฟ็กต์ภาพตามรูปแบบ JPG ที่ต้องการ วัตถุเบื้องหน้าจะต้องแยกออกจากส่วนที่เหลือของภาพ สามารถตรวจจับวัตถุอัตโนมัติได้หากพื้นหลังมีความสม่ำเสมอ หากพื้นหลังของภาพถ่ายไม่สม่ำเสมอหรือการแยกวัตถุทำได้ยาก แนะนำให้ทำเครื่องหมายรูปภาพไว้ล่วงหน้า ซึ่งเกี่ยวข้องกับการระบุบริเวณสี่เหลี่ยมภายในภาพถ่ายซึ่งมีวัตถุที่ต้องการอยู่และระบุประเภทของวัตถุเหล่านั้น ซึ่งสามารถทำได้ด้วยตนเองหรือโดยอัตโนมัติผ่านฟีเจอร์การจดจำวัตถุของ Cloud API หลังจากเลือกวัตถุและลบพื้นหลังดั้งเดิมแล้ว คุณสามารถใช้พื้นหลังใหม่หรือนำความโปร่งใสมาใช้ได้ เราจะใช้เพื่อเปลี่ยนพื้นหลังในไฟล์ JPG Aspose.Imaging for Java API ซึ่งเป็น API การแปลงและการแปลงรูปภาพที่มีคุณลักษณะหลากหลาย มีประสิทธิภาพ และใช้งานง่ายสำหรับแพลตฟอร์ม Java คุณสามารถดาวน์โหลดเวอร์ชันล่าสุดได้โดยตรงจาก Maven และติดตั้งภายใน Maven ของคุณ - ตามโครงการโดยเพิ่มการกำหนดค่าต่อไปนี้ใน pom.xml

Repository

<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-imaging</artifactId>
<version>version of aspose-imaging API</version>
<classifier>jdk16</classifier>
</dependency>

ขั้นตอนในการเปลี่ยนพื้นหลังใน JPGs ผ่าน Java

คุณต้องมี aspose-imaging-version-jdk16.jar เพื่อลองใช้เวิร์กโฟลว์ต่อไปนี้ในสภาพแวดล้อมของคุณเอง

  • โหลดไฟล์ JPG ด้วยวิธี Image.load
  • เปลี่ยนพื้นหลัง;
  • บันทึกภาพลงแผ่นดิสก์ในรูปแบบที่รองรับโดย Aspose.Imaging

ความต้องการของระบบ

Aspose.Imaging สำหรับ Java ได้รับการสนับสนุนในระบบปฏิบัติการหลักทั้งหมด เพียงตรวจสอบให้แน่ใจว่าคุณมีข้อกำหนดเบื้องต้นดังต่อไปนี้

  • ติดตั้ง JDK 1.6 หรือสูงกว่า
 

เปลี่ยนพื้นหลังในรูปภาพ JPG - Java

import com.aspose.imaging.*;
import com.aspose.imaging.fileformats.png.PngColorType;
import com.aspose.imaging.imageoptions.PngOptions;
import com.aspose.imaging.masking.IMaskingSession;
import com.aspose.imaging.masking.ImageMasking;
import com.aspose.imaging.masking.options.*;
import com.aspose.imaging.masking.result.MaskingResult;
import com.aspose.imaging.sources.FileCreateSource;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
//Most common example to demonstrate background change/remove tool
removeBackgroundGenericExample();
// Folder that contains images to process
static final String templatesFolder = "c:\\Data\\";
public static void removeBackgroundProcessingWithManualRectangles()
{
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng",
"dicom", "jp2", "j2k", "tga", "webp", "tif", "gif");
List<String> vectorFormats = Arrays.asList("svg", "otg", "odg", "eps",
"wmf", "emf", "wmz", "emz", "cmx", "cdr");
List<String> allFormats = new LinkedList<>(rasterFormats);
allFormats.addAll(vectorFormats);
allFormats.forEach(new Consumer<String>()
{
@Override
public void accept(String formatExt)
{
String inputFile = templatesFolder + "couple." + formatExt;
boolean isVectorFormat = vectorFormats.contains(formatExt);
//Need to rasterize vector formats before background remove
if (isVectorFormat)
{
inputFile = rasterizeVectorImage(formatExt, inputFile);
}
String outputFile = templatesFolder + "remove_background_manual_rectangles." + formatExt;
System.out.println("Processing " + formatExt);
try (RasterImage image = (RasterImage) Image.load(inputFile))
{
//Additional code examples can be found at
//https://docs.aspose.com/imaging/java/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api
AutoMaskingGraphCutOptions maskingOptions = new AutoMaskingGraphCutOptions();
maskingOptions.setFeatheringRadius(2);
maskingOptions.setMethod(SegmentationMethod.GraphCut);
AutoMaskingArgs maskingArgs = new AutoMaskingArgs();
maskingArgs.setObjectsRectangles(new Rectangle[]
{
// girl's bound box
new Rectangle(87, 47, 123, 308),
// boy's bound box
new Rectangle(180, 24, 126, 224)
});
maskingOptions.setArgs(maskingArgs);
PngOptions pngOptions = new PngOptions();
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setSource(new FileCreateSource(outputFile, false));
maskingOptions.setExportOptions(pngOptions);
IMaskingSession maskingSession = new ImageMasking(image)
.createSession(maskingOptions);
try
{
// first run of segmentation
maskingSession.decompose().dispose();
AutoMaskingArgs argsWithUserMarkers = new AutoMaskingArgs();
argsWithUserMarkers.setObjectsPoints(new Point[][]
{
// background markers
null,
// foreground markers
new UserMarker()
// boy's head
.addPoint(218, 48, 10)
// girl's head
.addPoint(399, 66, 10)
// girs's body
.addPoint(158, 141, 10)
.addPoint(158, 209, 20)
.addPoint(115, 225, 5)
.getPoints()
});
try (MaskingResult maskingResult = maskingSession
.improveDecomposition(argsWithUserMarkers))
{
try (Image resultImage = maskingResult.get_Item(1).getImage())
{
resultImage.save();
}
}
}
finally
{
maskingSession.dispose();
}
}
//Remove rasterized vector image
if (isVectorFormat)
{
new File(inputFile).delete();
}
}
});
}
public static void removeBackgroundAutoProcessingWithAssumedObjects()
{
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng",
"dicom", "jp2", "j2k", "tga", "webp", "tif", "gif");
List<String> vectorFormats = Arrays.asList("svg", "otg", "odg", "eps",
"wmf", "emf", "wmz", "emz", "cmx", "cdr");
List<String> allFormats = new LinkedList<>(rasterFormats);
allFormats.addAll(vectorFormats);
allFormats.forEach(new Consumer<String>() {
@Override
public void accept(String formatExt)
{
String inputFile = templatesFolder + "couple." + formatExt;
boolean isVectorFormat = vectorFormats.contains(formatExt);
//Need to rasterize vector formats before background remove
if (isVectorFormat)
{
inputFile = rasterizeVectorImage(formatExt, inputFile);
}
String outputFile = templatesFolder
+ "remove_background_auto_assumed_objects." + formatExt;
System.out.println("Processing " + formatExt);
try (RasterImage image = (RasterImage) Image.load(inputFile))
{
//Additional code examples can be found at
//https://docs.aspose.com/imaging/java/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api
AutoMaskingGraphCutOptions maskingOptions = new AutoMaskingGraphCutOptions();
final LinkedList<AssumedObjectData> assumedObjects = new LinkedList<>();
// girl's bound box
assumedObjects.add(
new AssumedObjectData(DetectedObjectType.Human,
new Rectangle(87, 47, 123, 308)));
// boy's bound box
assumedObjects.add(
new AssumedObjectData(DetectedObjectType.Human,
new Rectangle(180, 24, 126, 224)));
maskingOptions.setAssumedObjects(assumedObjects);
maskingOptions.setCalculateDefaultStrokes(true);
maskingOptions.setFeatheringRadius(1);
maskingOptions.setMethod(SegmentationMethod.GraphCut);
PngOptions pngOptions = new PngOptions();
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setSource(new FileCreateSource(outputFile, false));
maskingOptions.setExportOptions(pngOptions);
maskingOptions.setBackgroundReplacementColor(Color.getGreen());
try (MaskingResult maskingResult = new ImageMasking(image)
.decompose(maskingOptions))
{
try (Image resultImage = maskingResult.get_Item(1).getImage())
{
resultImage.save();
}
}
}
//Remove rasterized vector image
if (isVectorFormat)
{
new File(inputFile).delete();
}
}
});
}
public static void removeBackgroundAutoProcessing()
{
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng",
"dicom", "jp2", "j2k", "tga", "webp", "tif", "gif");
List<String> vectorFormats = Arrays.asList("svg", "otg", "odg", "eps",
"wmf", "emf", "wmz", "emz", "cmx", "cdr");
List<String> allFormats = new LinkedList<>(rasterFormats);
allFormats.addAll(vectorFormats);
allFormats.forEach(new Consumer<String>() {
@Override
public void accept(String formatExt)
{
String inputFile = templatesFolder + "couple." + formatExt;
boolean isVectorFormat = vectorFormats.contains(formatExt);
//Need to rasterize vector formats before background remove
if (isVectorFormat)
{
inputFile = rasterizeVectorImage(formatExt, inputFile);
}
String outputFile = templatesFolder + "remove_background_auto." + formatExt;
System.out.println("Processing " + formatExt);
try (RasterImage image = (RasterImage) Image.load(inputFile))
{
//Additional code examples can be found at
//https://docs.aspose.com/imaging/java/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api
AutoMaskingGraphCutOptions maskingOptions = new AutoMaskingGraphCutOptions();
maskingOptions.setFeatheringRadius(1);
maskingOptions.setMethod(SegmentationMethod.GraphCut);
PngOptions pngOptions = new PngOptions();
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setSource(new FileCreateSource(outputFile, false));
maskingOptions.setExportOptions(pngOptions);
maskingOptions.setBackgroundReplacementColor(Color.getGreen());
try (MaskingResult maskingResult = new ImageMasking(image)
.decompose(maskingOptions))
{
try (Image resultImage = maskingResult.get_Item(1).getImage())
{
resultImage.save();
}
}
}
//Remove rasterized vector image
if (isVectorFormat)
{
new File(inputFile).delete();
}
}
});
}
public static void removeBackgroundGenericExample()
{
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng",
"dicom", "jp2", "j2k", "tga", "webp", "tif", "gif");
List<String> vectorFormats = Arrays.asList("svg", "otg", "odg", "eps",
"wmf", "emf", "wmz", "emz", "cmx", "cdr");
List<String> allFormats = new LinkedList<>(rasterFormats);
allFormats.addAll(vectorFormats);
allFormats.forEach(new Consumer<String>() {
@Override
public void accept(String formatExt)
{
String inputFile = templatesFolder + "couple." + formatExt;
boolean isVectorFormat = vectorFormats.contains(formatExt);
//Need to rasterize vector formats before background remove
if (isVectorFormat)
{
inputFile = rasterizeVectorImage(formatExt, inputFile);
}
String outputFile = templatesFolder + "remove_background." + formatExt;
System.out.println("Processing " + formatExt);
try (RasterImage image = (RasterImage) Image.load(inputFile))
{
//Additional code examples can be found at
//https://docs.aspose.com/imaging/java/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api
AutoMaskingGraphCutOptions maskingOptions = new AutoMaskingGraphCutOptions();
maskingOptions.setCalculateDefaultStrokes(true);
maskingOptions.setFeatheringRadius(1);
maskingOptions.setMethod(SegmentationMethod.GraphCut);
PngOptions pngOptions = new PngOptions();
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setSource(new FileCreateSource(outputFile, false));
maskingOptions.setExportOptions(pngOptions);
maskingOptions.setBackgroundReplacementColor(Color.getGreen());
try (MaskingResult maskingResult = new ImageMasking(image)
.decompose(maskingOptions))
{
try (Image resultImage = maskingResult.get_Item(1).getImage())
{
resultImage.save();
}
}
}
//Remove rasterized vector image
if (isVectorFormat)
{
new File(inputFile).delete();
}
}
});
}
private static String rasterizeVectorImage(String formatExt, String inputFile)
{
String outputFile = templatesFolder + "rasterized."+ formatExt + ".png";
try (Image image = Image.load(inputFile))
{
image.save(outputFile, new PngOptions());
}
return outputFile;
}
class UserMarker
{
private final List<Point> list = new LinkedList<>();
public UserMarker addPoint(int left, int top, int radius)
{
for (int y = top - radius; y <= top + radius; y++)
{
for (int x = left - radius; x <= left + radius; x++)
{
this.list.add(new Point(x, y));
}
}
return this;
}
public Point[] getPoints()
{
return this.list.toArray(new Point[0]);
}
}
 
  • เกี่ยวกับ Aspose.Imaging สำหรับ Java API

    Aspose.Imaging API เป็นโซลูชันการประมวลผลรูปภาพเพื่อสร้าง แก้ไข วาดหรือแปลงรูปภาพ (ภาพถ่าย) ภายในแอปพลิเคชัน นำเสนอ: การประมวลผลภาพข้ามแพลตฟอร์ม รวมถึงแต่ไม่จำกัดเพียงการแปลงระหว่างรูปแบบภาพต่างๆ (รวมถึงการประมวลผลภาพแบบหลายหน้าหรือหลายเฟรมแบบเดียวกัน) การปรับเปลี่ยน เช่น การวาด การทำงานกับภาพกราฟิกดั้งเดิม การแปลงภาพ (ปรับขนาด ครอบตัด พลิกและหมุน , ไบนารี, ระดับสีเทา, ปรับ), คุณสมบัติการจัดการภาพขั้นสูง (การกรอง, การแยกสี, การปิดบัง, การเดสก์) และกลยุทธ์การปรับหน่วยความจำให้เหมาะสม เป็นไลบรารีแบบสแตนด์อโลนและไม่ขึ้นกับซอฟต์แวร์ใด ๆ สำหรับการทำงานของรูปภาพ คุณสามารถเพิ่มคุณสมบัติการแปลงรูปภาพประสิทธิภาพสูงด้วย API ดั้งเดิมภายในโปรเจ็กต์ได้อย่างง่ายดาย สิ่งเหล่านี้เป็น API ภายในองค์กรที่เป็นส่วนตัว 100% และอิมเมจได้รับการประมวลผลที่เซิร์ฟเวอร์ของคุณ

    เปลี่ยนพื้นหลังใน JPG ผ่านแอปออนไลน์

    เปลี่ยนพื้นหลังในเอกสาร JPG โดยไปที่ เว็บไซต์ Live Demos การสาธิตสดมีประโยชน์ดังต่อไปนี้

      ไม่จำเป็นต้องดาวน์โหลดหรือตั้งค่าอะไรเลย
      ไม่ต้องเขียนโค้ดใดๆ
      เพียงอัปโหลดไฟล์ JPG แล้วกดปุ่ม เปลี่ยนพื้นหลังทันที
      รับลิงค์ดาวน์โหลดทันทีสำหรับไฟล์ผลลัพธ์

    JPG คืออะไร JPG รูปแบบไฟล์

    JPEG เป็นรูปแบบรูปภาพประเภทหนึ่งที่บันทึกโดยใช้วิธีการบีบอัดแบบสูญเสียข้อมูล ภาพที่ส่งออกซึ่งเป็นผลมาจากการบีบอัด เป็นการแลกเปลี่ยนระหว่างขนาดพื้นที่จัดเก็บและคุณภาพของภาพ ผู้ใช้สามารถปรับระดับการบีบอัดเพื่อให้ได้ระดับคุณภาพที่ต้องการ ในขณะเดียวกันก็ลดขนาดการจัดเก็บลง คุณภาพของรูปภาพจะได้รับผลกระทบเล็กน้อยหากใช้การบีบอัด 10:1 กับรูปภาพ ยิ่งค่าการบีบอัดสูง คุณภาพของภาพก็จะยิ่งลดลง

    อ่านเพิ่มเติม

    รูปแบบพื้นหลังการเปลี่ยนแปลงอื่น ๆ ที่รองรับ

    การใช้ Java คุณสามารถเปลี่ยนพื้นหลังในรูปแบบต่างๆ ได้อย่างง่ายดายรวมถึง

    APNG (กราฟิกเครือข่ายแบบพกพาแบบเคลื่อนไหว)
    BMP (รูปภาพบิตแมป)
    ICO (ไอคอน Windows)
    DIB (บิตแมปอิสระของอุปกรณ์)
    DICOM (การถ่ายภาพและการสื่อสารดิจิทัล)
    DJVU (รูปแบบกราฟิก)
    DNG (ภาพกล้องดิจิตอล)
    EMF (รูปแบบไฟล์ Metafile ที่ปรับปรุงแล้ว)
    EMZ (Windows บีบอัด Metafile ที่ปรับปรุงแล้ว)
    GIF (รูปแบบการแลกเปลี่ยนกราฟิก)
    JP2 (JPEG 2000)
    J2K (ภาพบีบอัดเวฟเล็ต)
    PNG (กราฟิกเครือข่ายแบบพกพา)
    TIFF (รูปแบบภาพที่ติดแท็ก)
    WEBP (รูปภาพเว็บแรสเตอร์)
    WMF (Microsoft Windows Metafile)
    WMZ (สกิน Windows Media Player ที่บีบอัด)
    TGA (Targa Graphic)
    SVG (กราฟิกแบบเวกเตอร์ที่ปรับขนาดได้)
    EPS (ภาษา PostScript ที่ห่อหุ้ม)
    CDR (วาดภาพเวกเตอร์)
    CMX (รูปภาพ Corel Exchange)
    OTG (มาตรฐาน OpenDocument)
    ODG (รูปแบบการวาด Apache OpenOffice)