Fungsi kelas alat ini: memperbesar gambar, gambar pemotongan, konversi jenis gambar, warna menjadi hitam dan putih, tanda air teks, gambar watermark, dll.
Salinan kode adalah sebagai berikut:
paket net.kitbox.util;
impor java.awt.alphacomposite;
impor java.awt.color;
impor java.awt.font;
impor java.awt.graphics;
impor java.awt.graphics2d;
impor java.awt.image;
impor java.awt.renderinghints;
impor java.awt.toolkit;
impor java.awt.color.colorspace;
impor java.awt.image.bufferedimage;
impor java.awt.image.colorconvertop;
impor java.awt.image.cropimagefilter;
impor java.awt.image.filteredimageSource;
impor java.awt.image.imagefilter;
impor java.awt.image.imagingopException;
impor java.io.file;
impor java.io.fileoutputStream;
impor java.io.ioException;
impor javax.imageio.imageio;
/**
* Penulis: LLDY
* Waktu: 2012-5-6 6:37:18 PM
* Kategori Alat Pemrosesan Gambar: <br>
* Fungsi: gambar zoom, potong gambar, konversi jenis gambar, warna menjadi hitam dan putih, tanda air teks, gambar watermark, dll.
*/
ImageUtils kelas publik {
/**
* Relatif terhadap lokasi gambar
*/
private static final int position_upperleft = 0;
private static final int position_upperright = 10;
private static final int position_lowerleft = 1;
private static final int position_lowerright = 11;
/**
* Beberapa format gambar umum
*/
Publik Static String Image_Type_GIF = "gif"; // Format pertukaran grafis
public static string Image_type_jpg = "jpg"; // grup ahli foto bersama
public static string Image_type_jpeg = "jpeg"; // grup ahli foto bersama
public static string Image_Type_Bmp = "BMP"; // BITMAP Bahasa Inggris (BitMap), ini adalah format file gambar standar dalam sistem operasi Windows
public static string Image_type_png = "png"; // Portable Network Graphics
instance private static Imageutils;
Private ImageUtils () {
instance = ini;
}
/**
* Dapatkan contoh
* @kembali
*/
Public Static Imageutils getInstance () {
if (instance == null) {
instance = new ImageUtils ();
}
instance return;
}
Public BufferedImage Image2BufferedImage (Image Image) {
System.out.println (image.getWidth (null));
System.out.println (image.getHeight (null));
BufferedImage bufferedImage = baru bufferedImage (image.getWidth (null), image.getHeight (null), bufferedimage.type_int_argb);
Graphics2d G = bufferedImage.creategraphics ();
G.DrawImage (gambar, null, null);
g.dispose ();
System.out.println (bufferedImage.getWidth ());
System.out.println (bufferedImage.getHeight ());
return bufferedImage;
}
/**
* Skala dan konversi format dan simpan
* Jalur sumber @param srcpath
* @param DestPath Target Path
* @param lebar: lebar target
* @param tinggi: target tinggi
* @param format: format file
* @kembali
*/
public static boolean scaletofile (string srcpath, string destpath, int lebar, tinggi int, format string) {
bendera boolean = false;
mencoba {
File file = file baru (srcpath);
File destfile = file baru (destPath);
if (! destfile.getParentFile (). Exists ()) {
destfile.getParentFile (). mkdir ();
}
BufferedImage SRC = ImageIO.READ (file);
Gambar gambar = src.getScaledInstance (lebar, tinggi, gambar.scale_default);
BufferedImage Tag = BufferedImage baru (lebar, tinggi, bufferedImage.type_int_rgb);
Grafik g = tag.getGraphics ();
G.DrawImage (gambar, 0, 0, null);
g.dispose ();
flag = imageo.write (tag, format, FileOutputStream baru (destfile)); // output ke stream file file
} catch (ioException e) {
e.printstacktrace ();
}
pengembalian bendera;
}
/**
* Gambar skala, metode ini mengembalikan gambar setelah gambar sumber diskalakan berdasarkan persentase.
* Inputimage @param
* @param Persentase Persentase Diizinkan Input 0 <Persentase <10000
* @kembali
*/
public static BufferedImage scalebypercentage (bufferedImage inputimage, int persentase) {
// Persentase diizinkan
if (0> Persentase || Persentase> 10000) {
Lempar pencitraan baru ("Kesalahan :: parameter ilegal: persentase->"+persentase+", persentase harus lebih besar dari 0 ~ kurang dari 10.000");
}
// Dapatkan jenis transparansi gambar asli
int type = inputImage.getColormodel (). getTransparency ();
// Dapatkan ukuran gambar target
int w = inputimage.getWidth ()*persentase;
int h = inputimage.getHeight ()*persentase;
// Buka anti-aliasing
Renderinghints renderinghints = renderinghints baru (renderinghints.key_interpolation,
Renderinghints.value_antialias_on);
// Gunakan kompresi berkualitas tinggi
renderinghints.put (renderinghints.key_interpolation, renderinghints.value_render_quality);
BufferedImage IMG = BufferedImage baru (W, H, TYPE);
Graphics2d Graphics2d = img.creategraphics ();
Graphics2d.SetrenderingHints (renderingHints);
Graphics2d.drawimage (inputimage, 0, 0, w, h, 0, 0, inputimage
.getWidth (), inputimage.getHeight (), null);
graphics2d.dispose ();
kembalikan img;
/*Kode ini akan mengembalikan jenis gambar
return inputimage.getScaledInstance (inputimage.getWidth ()*persentase,
inputimage.getHeight ()*persentase, gambar.scale_smooth);
*/
}
/**
* Scaling Image, metode ini mengembalikan gambar setelah gambar sumber diskalakan secara proporsional di bawah batas lebar maksimum yang diberikan.
* Inputimage @param
* @param maxwidth: Lebar maksimum yang diijinkan setelah kompresi
* @param maxheight: tinggi maksimum yang diijinkan setelah kompresi
* @Throws java.io.ioException
* kembali
*/
public static BufferedImage Scalebypixelrate (BufferedImage Inputimage, int maxwidth, int maxheight) melempar Exception {
// Dapatkan jenis transparansi gambar asli
int type = inputImage.getColormodel (). getTransparency ();
Int width = inputImage.getWidth ();
int tinggi = inputimage.getHeight ();
int newwidth = maxwidth;
int newheight = maxheight;
// Jika lebar maksimum yang ditentukan melebihi proporsinya
if (width*maxheight <height*maxwidth) {
newwidth = (int) (newheight*lebar/tinggi);
}
// Jika ketinggian maksimum yang ditentukan melebihi proporsinya
if (lebar*maxheight> tinggi*maxwidth) {
newheight = (int) (newwidth*tinggi/lebar);
}
// Buka anti-aliasing
Renderinghints renderinghints = renderinghints baru (renderinghints.key_interpolation,
Renderinghints.value_antialias_on);
// Gunakan kompresi berkualitas tinggi
renderinghints.put (renderinghints.key_interpolation, renderinghints.value_render_quality);
BufferedImage IMG = BufferedImage baru (Newwidth, Newheight, Type);
Graphics2d Graphics2d = img.creategraphics ();
Graphics2d.SetrenderingHints (renderingHints);
graphics2d.drawimage (inputimage, 0, 0, newwidth, newheight, 0, 0, width, height, null);
graphics2d.dispose ();
kembalikan img;
}
/**
* SKALING Image, metode ini mengembalikan gambar setelah gambar sumber ditingkatkan dengan batas lebar dan ketinggian yang diberikan.
* Inputimage @param
* @param maxwidth: lebar setelah kompresi
* @param maxheight: tinggi setelah kompresi
* @Throws java.io.ioException
* kembali
*/
Public Static BufferedImage Scalebypixel (BufferedImage InputImage, int Newwidth, int NewHeight) melempar Exception {
// Dapatkan jenis transparansi gambar asli
int type = inputImage.getColormodel (). getTransparency ();
Int width = inputImage.getWidth ();
int tinggi = inputimage.getHeight ();
// Buka anti-aliasing
Renderinghints renderinghints = renderinghints baru (renderinghints.key_antialiasing,
Renderinghints.value_antialias_on);
// Gunakan kompresi berkualitas tinggi
renderinghints.put (renderinghints.key_rendering, renderinghints.value_render_quality);
BufferedImage IMG = BufferedImage baru (Newwidth, Newheight, Type);
Graphics2d Graphics2d = img.creategraphics ();
Graphics2d.SetrenderingHints (renderingHints);
graphics2d.drawimage (inputimage, 0, 0, newwidth, newheight, 0, 0, width, height, null);
graphics2d.dispose ();
kembalikan img;
}
/**
* Potong gambar dan kembalikan gambar dalam kisaran yang ditentukan
* Inputimage @param
* @param x titik awal sumbu horizontal
* @param y titik awal koordinat vertikal
* @param lebar potongan gambar: jika lebar melebihi gambar, itu akan diubah ke gambar dari x yang tersisa lebar
* @param tinggi potong tinggi gambar: jika ketinggian melebihi gambar, itu akan diubah ke ketinggian gambar yang tersisa
* Isi @param menentukan apakah akan mengisi jika ukuran gambar target melebihi ukurannya.
* @kembali
*/
Public Static BufferedImage Cut (BufferedImage InputImage, int x, int y, int lebar, tinggi int, boolean isian) {
// Dapatkan jenis transparansi gambar asli
int type = inputImage.getColormodel (). getTransparency ();
int w = inputImage.getWidth ();
int h = inputimage.getHeight ();
int endx = x+lebar;
int endy = y+tinggi;
if (x> w)
Lempar pencitraan baru ("Sumbu horizontal dari titik awal melebihi kisaran gambar sumber");
if (y> h)
Lempar pencitraan baru ("Koordinat vertikal dari titik awal melebihi rentang gambar sumber");
BufferedImage IMG;
// Isi putih
if (isi) {
IMG = BufferedImage baru (lebar, tinggi, jenis);
// Lebar melebihi batas
if ((wx) <width) {
lebar = wx;
endx = w;
}
// tinggi melebihi batas
if ((hy) <height) {
tinggi = hy;
endy = h;
}
// tidak diperbaiki
}kalau tidak{
// Lebar melebihi batas
if ((wx) <width) {
lebar = wx;
endx = w;
}
// tinggi melebihi batas
if ((hy) <height) {
tinggi = hy;
endy = h;
}
IMG = BufferedImage baru (lebar, tinggi, jenis);
}
// Buka anti-aliasing
Renderinghints renderinghints = renderinghints baru (renderinghints.key_interpolation,
Renderinghints.value_antialias_on);
// Gunakan kompresi berkualitas tinggi
renderinghints.put (renderinghints.key_interpolation, renderinghints.value_render_quality);
Graphics2d Graphics2d = img.creategraphics ();
Graphics2d.SetrenderingHints (renderingHints);
graphics2d.drawimage (inputimage, 0, 0, lebar, tinggi, x, y, endx, endy, null);
graphics2d.dispose ();
kembalikan img;
}
/**
* Potong gambar dan kembalikan posisi titik awal yang ditentukan dan gambar ukuran yang ditentukan
* Inputimage @param
* @param Start Start Point: kiri atas: 0, kanan atas: 10, kiri bawah: 1, kanan bawah: 11
* @param width cut Width gambar
* @param tinggi memotong tinggi gambar
* Isi @param menentukan apakah akan mengisi jika ukuran gambar target melebihi ukurannya.
* @kembali
*/
Pemotongan BufferedImage Statis Publik (Inputimage BufferedImage, Int Startpoint, Int Width, Int Height, Boolean Fill) {
// Dapatkan jenis transparansi gambar asli
int type = inputImage.getColormodel (). getTransparency ();
int w = inputImage.getWidth ();
int h = inputimage.getHeight ();
BufferedImage IMG;
// Isi putih
if (isi) {
IMG = BufferedImage baru (lebar, tinggi, jenis);
if (width> w)
lebar = w;
if (height> h)
tinggi = h;
// tidak diperbaiki
}kalau tidak{
if (width> w)
lebar = w;
if (height> h)
tinggi = h;
IMG = BufferedImage baru (lebar, tinggi, jenis);
}
// Buka anti-aliasing
Renderinghints renderinghints = renderinghints baru (renderinghints.key_interpolation,
Renderinghints.value_antialias_on);
// Gunakan kompresi berkualitas tinggi
renderinghints.put (renderinghints.key_interpolation, renderinghints.value_render_quality);
Graphics2d Graphics2d = img.creategraphics ();
Graphics2d.SetrenderingHints (renderingHints);
sakelar (startpoint) {
// sampai ke kanan
case position_upperright:
graphics2d.drawimage (inputimage, w-width, 0, w, tinggi, 0, 0, lebar, tinggi, nol);
merusak;
// kiri bawah
Posisi case_lowerft:
grafik2d.drawimage (inputimage, 0, height, lebar, h, 0, 0, lebar, tinggi, nol);
merusak;
// Bawah kanan
Posisi case_lowerright:
graphics2d.drawimage (inputimage, w-width, h-height, w, h, 0, 0, lebar, tinggi, null);
merusak;
// Kiri atas default
Posisi case_UPPERLEFT:
bawaan:
graphics2d.drawimage (inputimage, 0, 0, lebar, tinggi, 0, lebar, tinggi, null);
}
graphics2d.dispose ();
kembalikan img;
}
/**
* Putar gambar pada sudut yang ditentukan: Gunakan theta untuk memutar titik pada sumbu x positif menuju sumbu y positif.
* Inputimage @param
* @param Delare Angle: dalam derajat
* @kembali
*/
Public Static BufferedImage RotateImage (Inputimage BufferedImage Akhir,
derajat int akhir) {
int w = inputImage.getWidth ();
int h = inputimage.getHeight ();
int type = inputImage.getColormodel (). getTransparency ();
BufferedImage IMG = BufferedImage baru (W, H, TYPE);
Graphics2d Graphics2d = img.creategraphics ();
// Buka anti-aliasing
Renderinghints renderinghints = renderinghints baru (renderinghints.key_interpolation,
Renderinghints.value_antialias_on);
// Gunakan kompresi berkualitas tinggi
renderinghints.put (renderinghints.key_interpolation, renderinghints.value_render_quality);
Graphics2d.SetrenderingHints (renderingHints);
Graphics2d.Rotate (Math.toradians (derajat), w / 2, h / 2);
graphics2d.drawimage (inputimage, 0, 0, null);
graphics2d.dispose ();
kembalikan img;
}
/**
* Balikkan gambar secara horizontal
*
* @param BufferedImage Target Image
* @kembali
*/
fliphorizontalimage public static (inputimage buferedimage akhir) {
int w = inputImage.getWidth ();
int h = inputimage.getHeight ();
BufferedImage IMG;
Graphics2d Graphics2d;
(Graphics2D = (IMG = BufferedImage baru (W, H, inputimage
.getColormodel (). getTransparency ())). createGraphics ())
.drawimage (inputimage, 0, 0, w, h, w, 0, 0, h, null);
graphics2d.dispose ();
kembalikan img;
}
/**
* Balikkan gambar secara vertikal
*
* @param BufferedImage Target Image
* @kembali
*/
Public Static BufferedImage FlipverticalImage (inputimage buferedimage akhir) {
int w = inputImage.getWidth ();
int h = inputimage.getHeight ();
BufferedImage IMG;
Graphics2d Graphics2d;
(Graphics2D = (IMG = BufferedImage baru (W, H, inputimage
.getColormodel (). getTransparency ())). createGraphics ())
.drawimage (inputimage, 0, 0, w, h, 0, h, w, 0, null);
graphics2d.dispose ();
kembalikan img;
}
/**
* Bayangkan tanda air
*
* Inputimage @param
* Gambar yang akan diproses
* @param Markimage
* Gambar tanda air
* @param x
* Tanda air terletak di sudut kiri atas gambar.
* @param y
* Tanda air terletak di sudut kiri atas gambar.
* @param alpha
* Watermark Transparency 0.1F ~ 1.0F
* *//
watermark bufferedmage statis publik (inputimage buferedimage, markimage bufferedimage, int x, int y,
float alpha) {
BufferedImage Image = BufferedImage baru (inputimage.getWidth (), inputimage
.getHeight (), bufferedimage.type_int_argb);
Graphics2d G = Image.Creategraphics ();
g.drawimage (inputimage, 0, 0, null);
// Muat gambar tanda air
g.setComposite (alphacomposite.getInstance (alphacomposite.src_atop,
alfa));
G.DrawImage (MarkImage, X, Y, NULL);
g.dispose ();
gambar kembali;
}
/**
* SMS Watermark
*
* Inputimage @param
* Gambar yang akan diproses
* @param Text
* Teks tanda air
* @param font
* Informasi Font Watermark
* @param warna
* Warna Watermark Font
* @param x
* Tanda air terletak di sudut kiri atas gambar.
* @param y
* Tanda air terletak di sudut kiri atas gambar.
* @param alpha
* Watermark Transparency 0.1F ~ 1.0F
*/
Textmark BufferedImage Statis Publik (Inputimage BufferedImage, Teks String, Font Font,
Warna warna, int x, int y, float alpha) {
Font dfont = (font == null)?
BufferedImage Image = BufferedImage baru (inputimage.getWidth (), inputimage
.getHeight (), bufferedimage.type_int_argb);
Graphics2d G = Image.Creategraphics ();
g.drawimage (inputimage, 0, 0, null);
g.setColor (warna);
g.setfont (dfont);
g.setComposite (alphacomposite.getInstance (alphacomposite.src_atop,
alfa));
g.drawstring (teks, x, y);
g.dispose ();
gambar kembali;
}
/**
* Warna gambar menjadi hitam dan putih
* Inputimage @param
* @return yang dikonversi bufferedImage
*/
TOGRAY BUFFEREDIMAGE PUBLIK PUBLIK (BUFFEDEDIMAGE INPUTIMAGE)
{
ColorsPace CS = ColorsPace.GetInstance (ColorsPace.CS_GRAY);
// Konversi warna sumber bufferedImage. Jika gambar target nol,
// Buat bufferedImage berdasarkan colormodel yang sesuai.
ColorConvertop OP = ColorConVertop baru (CS, NULL);
return op.filter (inputimage, null);
}
/**
* Warna gambar berubah menjadi hitam dan putih
* @param srcimageFile
* Alamat gambar sumber
* @param destimageFile
* Alamat gambar target
* @param FormatType
* Format Gambar Target: Jika Formattype adalah NULL;
*/
public final static void TOGRAY (String srcimageFile, string destimageFile, string formatType)
{
mencoba
{
BufferedImage SRC = ImageIO.READ (file baru (SRCIMAGEFILE));
ColorsPace CS = ColorsPace.GetInstance (ColorsPace.CS_GRAY);
ColorConvertop OP = ColorConVertop baru (CS, NULL);
src = op.filter (src, null);
// Jika formattype adalah nol;
if (formatType == null) {
formatType = "png";
}
ImageIO.Write (SRC, FormatType, File Baru (DestImageFile));
} catch (ioException e)
{
e.printstacktrace ();
}
}
/**
* Konversi tipe gambar: gif-> jpg, gif-> png, png-> jpg, png-> gif (x), bmp-> png
*
* Inputimage @param
* Alamat gambar sumber
* @param FormatType
* String yang berisi nama format informal: seperti jpg, jpeg, gif, dll.
* @param destimageFile
* Alamat gambar target
*/
public final static void convert (bufferedimage inputimage, string formatype, string destImageFile)
{
mencoba
{
ImageIO.Write (InputImage, FormatType, File Baru (DestImageFile));
} tangkap (pengecualian e)
{
e.printstacktrace ();
}
}
/**
* Pemotongan gambar (tentukan jumlah baris dan kolom irisan)
*
* @param srcimageFile
* Alamat gambar sumber
* @param destdir
* Iris folder target
* @param FormatType
* Format target
* Baris @param
* Jumlah baris irisan target. Default 2, harus berada dalam kisaran [1, 20]
* @param cols
* Jumlah kolom slice target. Default 2, harus berada dalam kisaran [1, 20]
*/
Cut void static final publik (BufferedImage InputImage, String destdir,
String FormatType, Int Rows, Int Cols)
{
mencoba
{
if (baris <= 0 || baris> 20)
Baris = 2; // Jumlah baris irisan
if (cols <= 0 || cols> 20)
cols = 2; // Jumlah kolom irisan
// Baca gambar sumber
// bufferedImage bi = imageo.read (file baru (srcimageFile));
int w = inputimage.getHeight ();
int h = inputimage.getWidth ();
if (w> 0 && h> 0)
{
Gambar img;
Imagefilter cropfilter;
Gambar gambar = inputimage.getScaledInstance (w, h,
Image.scale_default);
int destwidth = w; // lebar setiap irisan
Int WHDHEIGHT = H; // Tinggi setiap irisan
// Hitung lebar dan tinggi irisan
if (w % cols == 0)
{
destwidth = w / cols;
} kalau tidak
{
destWidth = (int) math.floor (w / cols) + 1;
}
if (h % baris == 0)
{
HUBUNGAN = H / Baris;
} kalau tidak
{
destheight = (int) math.floor (h / baris) + 1;
}
// Buat irisan di loop
// Ide Peningkatan: Apakah Multithreading dapat digunakan untuk mempercepat kecepatan pemotongan
untuk (int i = 0; i <baris; i ++)
{
untuk (int j = 0; j <cols; j ++)
{
// Empat parameter adalah koordinat titik awal gambar dan lebar dan tinggi
// yaitu: cropimagefilter (int x, int y, lebar int, tinggi int)
cropfilter = cropimagefilter baru (j * destwidth, i
* WHREIGHT, DESTWIDTH, DESTHEIGHT);
img = toolkit.getDefaultToolKit (). createImage (
FilteredImageSource baru (Image.GetSource (),
cropfilter));
BufferedImage Tag = BufferedImage baru (DestWidth,
Hutan, bufferedImage.type_int_argb);
Grafik g = tag.getGraphics ();
G.DrawImage (IMG, 0, 0, NULL);
g.dispose ();
// output sebagai file
ImageIO.Write (Tag, FormatType, File Baru (DestDir + "_R" + i
+ "_c" + j + "." + FormatType.TolowerCase ()));
}
}
}
} tangkap (pengecualian e)
{
e.printstacktrace ();
}
}
/**
* Tambahkan tanda air teks ke gambar
*
* @param pressText
* Teks tanda air
* @param srcimageFile
* Alamat gambar sumber
* @param destimageFile
* Alamat gambar target
* @param fontname
* Nama font dari tanda air
* @param fontstyle
* Gaya font watermark
* @param warna
* Warna font dari tanda air
* @param fontsize
* Ukuran font air
* @param x
* Nilai Terkoreksi
* @param y
* Nilai Terkoreksi
* @param alpha
* Transparansi: Alpha harus berupa nomor titik mengambang dalam kisaran [0,0, 1.0] (termasuk nilai batas)
* @param FormatType
* Format target
*/
public final static void pressText (String PressText, String SRCIMAGEFILE,
String destimageFile, string fontname, int fontstyle, warna warna,
int fontsize, int x, int y, float alpha, string formatType)
{
mencoba
{
File img = file baru (srcimageFile);
Image src = ImageIo.read (IMG);
Int width = src.getWidth (null);
int tinggi = src.getHeight (null);
BufferedImage Image = BufferedImage Baru (Lebar, Tinggi,
BufferedImage.type_int_argb);
Graphics2d G = Image.Creategraphics ();
G.DrawImage (Src, 0, 0, lebar, tinggi, nol);
g.setColor (warna);
g.setfont (font baru (fontname, fontstyle, fontsize));
g.setComposite (alphacomposite.getInstance (alphacomposite.src_atop,
alfa));
// Gambarlah teks tanda air dalam koordinat yang ditentukan
g.drawstring (pressText, (width - (getLength (pressText) * fontSize)))
/ 2 + x, (tinggi - fontsize) / 2 + y);
g.dispose ();
Imageio.write ((bufferedImage) Image, FormatType,
file baru (destImageFile)); // output ke aliran file
} tangkap (pengecualian e)
{
e.printstacktrace ();
}
}
/**
* Tambahkan tanda air gambar ke gambar
*
* @param pressimg
* Gambar tanda air
* @param srcimageFile
* Alamat gambar sumber
* @param destimageFile
* Alamat gambar target
* @param x
* Nilai tetap. Default ada di tengah
* @param y
* Nilai tetap. Default ada di tengah
* @param alpha
* Transparansi: Alpha harus berupa nomor titik mengambang dalam kisaran [0,0, 1.0] (termasuk nilai batas)
* @param FormatType
* Format target
*/
PressImage void statis akhir publik (String Pressimg, String SrcimageFile,
String DestImageFile, int x, int y, float alpha, string formatType)
{
mencoba
{
File img = file baru (srcimageFile);
Image src = ImageIo.read (IMG);
int wideth = src.getWidth (null);
int tinggi = src.getHeight (null);
BufferedImage Image = BufferedImage Baru (Wideth, Height,
BufferedImage.type_int_argb);
Graphics2d G = Image.Creategraphics ();
G.DrawImage (Src, 0, 0, Wideth, Height, Null);
// file watermark
Gambar src_biao = imageIo.read (file baru (pressimg));
int wideth_biao = src_biao.getwidth (null);
int height_biao = src_biao.getheight (null);
g.setComposite (alphacomposite.getInstance (alphacomposite.src_atop,
alfa));
G.DrawImage (src_biao, (wideth - wideth_biao) / 2,
(Height - height_biao) / 2, wideth_biao, height_biao, null);
// Akhir dari file watermark
g.dispose ();
Imageio.write ((bufferedImage) Image, FormatType,
file baru (DestImageFile));
} tangkap (pengecualian e)
{
e.printstacktrace ();
}
}
/**
* Hitung panjang teks (satu Cina menghitung dua karakter)
*
* @param Text
* @kembali
*/
Publik final static int getLength (Teks String)
{
panjang int = 0;
untuk (int i = 0; i <text.length (); i ++)
{
if (string baru (text.charat (i) + "") .getbytes (). Panjang> 1)
{
panjang += 2;
} kalau tidak
{
panjang += 1;
}
}
Panjang pengembalian / 2;
}
}
Fungsi pemrosesan gambar yang sangat praktis, saya harap Anda menyukainya.