public class ImageInit {
BufferedImage image
private int iw, ih
private int[] pixels
public ImageInit(BufferedImage image) {
this.image = image
iw = image.getWidth()
ih = image.getHeight()
pixels = new int[iw * ih]
}
public BufferedImage changeGrey() {
PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih,
pixels, 0, iw)
try {
pg.grabPixels()
} catch (InterruptedException e) {
e.printStackTrace()
}
// 设定二值化的域值,默认值为100
int grey = 100
// 对图像进行二值化处理,Alpha值保持不变
ColorModel cm = ColorModel.getRGBdefault()
for (int i = 0 i < iw * ih i++) {
int red, green, blue
int alpha = cm.getAlpha(pixels[i])
if (cm.getRed(pixels[i]) > grey) {
red = 255
} else {
red = 0
}
if (cm.getGreen(pixels[i]) > grey) {
green = 255
} else {
green = 0
}
if (cm.getBlue(pixels[i]) > grey) {
blue = 255
} else {
blue = 0
}
pixels[i] = alpha << 24 | red << 16 | green << 8 | blue // 通过移位重新构成某一点像素的RGB值
}
// 将数组中的象素产生一个图像
Image tempImg = Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(iw, ih, pixels, 0, iw))
image = new BufferedImage(tempImg.getWidth(null),
tempImg.getHeight(null), BufferedImage.TYPE_INT_BGR)
image.createGraphics().drawImage(tempImg, 0, 0, null)
return image
}
public BufferedImage getMedian() {
PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih,
pixels, 0, iw)
try {
pg.grabPixels()
} catch (InterruptedException e) {
e.printStackTrace()
}
// 对图像进行中值滤波,Alpha值保持不变
ColorModel cm = ColorModel.getRGBdefault()
for (int i = 1 i < ih - 1 i++) {
for (int j = 1 j < iw - 1 j++) {
int red, green, blue
int alpha = cm.getAlpha(pixels[i * iw + j])
// int red2 = cm.getRed(pixels[(i - 1) * iw + j])
int red4 = cm.getRed(pixels[i * iw + j - 1])
int red5 = cm.getRed(pixels[i * iw + j])
int red6 = cm.getRed(pixels[i * iw + j + 1])
// int red8 = cm.getRed(pixels[(i + 1) * iw + j])
// 水平方向进行中值滤波
if (red4 >= red5) {
if (red5 >= red6) {
red = red5
} else {
if (red4 >= red6) {
red = red6
} else {
red = red4
}
}
} else {
if (red4 > red6) {
red = red4
} else {
if (red5 > red6) {
red = red6
} else {
red = red5
}
}
}
int green4 = cm.getGreen(pixels[i * iw + j - 1])
int green5 = cm.getGreen(pixels[i * iw + j])
int green6 = cm.getGreen(pixels[i * iw + j + 1])
// 水平方向进行中值滤波
if (green4 >= green5) {
if (green5 >= green6) {
green = green5
} else {
if (green4 >= green6) {
green = green6
} else {
green = green4
}
}
} else {
if (green4 > green6) {
green = green4
} else {
if (green5 > green6) {
green = green6
} else {
green = green5
}
}
}
// int blue2 = cm.getBlue(pixels[(i - 1) * iw + j])
int blue4 = cm.getBlue(pixels[i * iw + j - 1])
int blue5 = cm.getBlue(pixels[i * iw + j])
int blue6 = cm.getBlue(pixels[i * iw + j + 1])
// int blue8 = cm.getBlue(pixels[(i + 1) * iw + j])
// 水平方向进行中值滤波
if (blue4 >= blue5) {
if (blue5 >= blue6) {
blue = blue5
} else {
if (blue4 >= blue6) {
blue = blue6
} else {
blue = blue4
}
}
} else {
if (blue4 > blue6) {
blue = blue4
} else {
if (blue5 > blue6) {
blue = blue6
} else {
blue = blue5
}
}
}
pixels[i * iw + j] = alpha << 24 | red << 16 | green << 8
| blue
}
}
// 将数组中的象素产生一个图像
Image tempImg = Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(iw, ih, pixels, 0, iw))
image = new BufferedImage(tempImg.getWidth(null),
tempImg.getHeight(null), BufferedImage.TYPE_INT_BGR)
image.createGraphics().drawImage(tempImg, 0, 0, null)
return image
}
public BufferedImage getGrey() {
ColorConvertOp ccp = new ColorConvertOp(
ColorSpace.getInstance(ColorSpace.CS_GRAY), null)
return image = ccp.filter(image, null)
}
// Brighten using a linear formula that increases all color values
public BufferedImage getBrighten() {
RescaleOp rop = new RescaleOp(1.25f, 0, null)
return image = rop.filter(image, null)
}
// Blur by "convolving" the image with a matrix
public BufferedImage getBlur() {
float[] data = { .1111f, .1111f, .1111f, .1111f, .1111f, .1111f,
.1111f, .1111f, .1111f, }
ConvolveOp cop = new ConvolveOp(new Kernel(3, 3, data))
return image = cop.filter(image, null)
}
// Sharpen by using a different matrix
public BufferedImage getSharpen() {
float[] data = { 0.0f, -0.75f, 0.0f, -0.75f, 4.0f, -0.75f, 0.0f,
-0.75f, 0.0f }
ConvolveOp cop = new ConvolveOp(new Kernel(3, 3, data))
return image = cop.filter(image, null)
}
// 11) Rotate the image 180 degrees about its center point
public BufferedImage getRotate() {
AffineTransformOp atop = new AffineTransformOp(
AffineTransform.getRotateInstance(Math.PI,
image.getWidth() / 2, image.getHeight() / 2),
AffineTransformOp.TYPE_NEAREST_NEIGHBOR)
return image = atop.filter(image, null)
}
public BufferedImage getProcessedImg() {
return image
}
public static void main(String[] args) throws IOException {
String filePath="F:/k7qp5.png"
FileInputStream fin = new FileInputStream(filePath)
BufferedImage bi = ImageIO.read(fin)
ImageInit flt = new ImageInit(bi)
flt.changeGrey()
flt.getGrey()
flt.getBrighten()
bi = flt.getProcessedImg()
String pname = filePath.substring(0, filePath.lastIndexOf("."))
File file = new File(pname + ".jpg")
ImageIO.write(bi, "jpg", file)
}
}
package com.aspectjimport java.awt.Color
import java.awt.Graphics2D
import java.awt.Image
import java.awt.image.BufferedImage
import java.io.File
import java.io.FileOutputStream
import javax.swing.ImageIcon
import com.sun.image.codec.jpeg.JPEGCodec
import com.sun.image.codec.jpeg.JPEGEncodeParam
import com.sun.image.codec.jpeg.JPEGImageEncoder
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
*
* 添加水印, filePath 源图片路径 含图片名, watermark 水印图片路径 savePath
* 为你添加水印后的图片保存路径文件夹 words 要添加的文字
*/
// 添加水印,filePath 源图片路径, watermark 水印图片路径
public class Mark {
private static int wid = 0
private static int het = 0
public static boolean createMark(String filePath, String watermark,
String words, String savePath) {
ImageIcon imgIcon = new ImageIcon(filePath)
Image theImg = imgIcon.getImage()
ImageIcon waterIcon = new ImageIcon(watermark)
Image waterImg = waterIcon.getImage()
// /////////////////////////////////////////////////////////////////////
File f = new File(filePath)
String picname = f.getName()// 取得图片名
if (watermark != null && !watermark.equals("")) {// 当水印图标为空时
ImageIcon markIcon = new ImageIcon(watermark) // 要添加的水印图标
Image markImg = markIcon.getImage()
wid = markImg.getWidth(null) // 水印图标宽度
het = markImg.getHeight(null) // 水印图标高度
}
// ////////////////////////////////////////////////////////////////////
int width = theImg.getWidth(null) // 源图片宽度
int height = theImg.getHeight(null) // 源图片高度
if (savePath.equals(""))
savePath = filePath// 如果未指定保存路径则保存回原路径
else
savePath = savePath + "指定保存文件夹时,拼接出保存路径"
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB)
Graphics2D g = bimage.createGraphics()
g.setColor(Color.red) // 设置颜色
g.setBackground(Color.white)
g.drawImage(theImg, 0, 0, null)
g.drawImage(waterImg, width - wid + 5, height - het + 5, null) // 添加图标中间两个数字参数
// 是设定位置
g.drawString(words, width - 120, height - 10) // 添加文字
try {
FileOutputStream out = new FileOutputStream(savePath)
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out)
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage)
param.setQuality(50f, true) // 图片质量
encoder.encode(bimage, param)
out.close()
} catch (Exception e) {
e.printStackTrace()
System.out.println("===========水印失败")
return false
} finally {
System.gc()// 清理 垃圾对象
}
System.out.println("===========水印成功")
return true
}
// /测试主程序
public static void main(String[] args) {
createMark("dcc451da81cb39dbfa76de3ad2160924ab183023.jpg", "u=4038692558,3024950167&fm=21&gp=0.jpg", "aas", "")
}
}
检测水印很难做到,但是加上水印还是比较简单的
对于文档处理控件Aspose.words,它的具有代表性的功能是在没有安装Microsoft Word的情况下,也能实现生成、打印、渲染、邮件合并,文档格式转换等功能。今天在使用Aspose.Words过程中,意外的发现这款文档处理软件的另外一个功能,它可以将图像转换为PDF文件,接下来就为大家分享一下实现这一功能的具体代码如有疑问追问,如满意记得采纳,
如果有其他问题也可点我名字向我求助
答题不易,
如果没有回答完全,请您谅解,
请采纳最快回答的正确答案!!谢谢!