java如何把图片转换成二进制并存到oracle的blob中,求代码import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
public class ImageUtils {
public static void main(String[] args) {
String str = img2Binary("C:\\Users\\hny\\Desktop\\favicon.jpg");
System.out.println(str);
binary2Img("C:\\Users\\hny\\Desktop\\favicon2.jpg", str);
}
/**
* 图片转二进制字符串
*
* @param path 图片路径
* @return
*/
public static String img2Binary(String path) {
File file = new File(path);
if (!file.exists()) {
return null;
}
try {
BufferedImage bi = ImageIO.read(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String suffix = getSuffix(path);
ImageIO.write(bi, suffix, baos);
byte[] bytes = baos.toByteArray();
return new sun.misc.BASE64Encoder().encodeBuffer(bytes).trim();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 字符串转图片文件
*
* @param path图片路径
* @param imgBinary 图片字符串
*/
public static void binary2Img(String path, String imgBinary) {
try {
File file = new File(path);
byte[] bytes1 = new sun.misc.BASE64Decoder().decodeBuffer(imgBinary);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
BufferedImage bi1 = ImageIO.read(bais);
String suffix = getSuffix(path);
ImageIO.write(bi1, suffix, file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取图片后缀名
*
* @param path
* @return
*/
private static String getSuffix(String path) {
int index = path.contains(".") ? path.lastIndexOf(".") : -1;
if (index-1) {
return path.substring(index1);
}
return null;
}
}
java代码怎么实现图像二值化较为常用的图像二值化方法有:1)全局固定阈值;2)局部自适应阈值;3)OTSU等 。
局部自适应阈值则是根据像素的邻域块的像素值分布来确定该像素位置上的二值化阈值 。
这样做的好处在于每个像素位置处的二值化阈值不是固定不变的,而是由其周围邻域像素的分布来决定的 。
亮度较高的图像区域的二值化阈值通常会较高,而亮度较低的图像区域的二值化阈值则会相适应地变小 。不同亮度、对比度、纹理的局部图像区域将会拥有相对应的局部二值化阈值 。
常用的局部自适应阈值有:
1)局部邻域块的均值;
2)局部邻域块的高斯加权和 。
急?。?,二值化后的图像 , 用JAVA中值滤波算法,去除椒盐噪点?。。?/h2>椒盐噪声的话一般可以用中值滤波器去除, 中值滤波器很容易实现, 依此遍历图像中每个像素点, 每个像素点与其周围的8个点像素值做一下排序操作, 找到这九个点中的中值点赋给当前遍历点的像素就可以了, 算法很简单吧. 我这有c的源码, 楼主要想要的话发邮件到我的邮箱769569350@qq.com我可以把程序发给你.
如何用java去除图片水?。?/h2>//运行以下程序即可
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; iiw * 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] = alpha24 | red16 | green8 | 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; iih - 1; i) {
for (int j = 1; jiw - 1; j) {
int red, green, blue;
int alpha = cm.getAlpha(pixels[i * iwj]);
// int red2 = cm.getRed(pixels[(i - 1) * iwj]);
int red4 = cm.getRed(pixels[i * iwj - 1]);
int red5 = cm.getRed(pixels[i * iwj]);
int red6 = cm.getRed(pixels[i * iwj1]);
// int red8 = cm.getRed(pixels[(i1) * iwj]);
// 水平方向进行中值滤波
if (red4 = red5) {
if (red5 = red6) {
red = red5;
} else {
if (red4 = red6) {
red = red6;
} else {
red = red4;
}
}
} else {
if (red4red6) {
red = red4;
} else {
if (red5red6) {
red = red6;
} else {
red = red5;
}
}
}
int green4 = cm.getGreen(pixels[i * iwj - 1]);
int green5 = cm.getGreen(pixels[i * iwj]);
int green6 = cm.getGreen(pixels[i * iwj1]);
// 水平方向进行中值滤波
if (green4 = green5) {
if (green5 = green6) {
green = green5;
} else {
if (green4 = green6) {
green = green6;
} else {
green = green4;
}
}
} else {
if (green4green6) {
green = green4;
} else {
if (green5green6) {
green = green6;
} else {
green = green5;
}
}
}
// int blue2 = cm.getBlue(pixels[(i - 1) * iwj]);
int blue4 = cm.getBlue(pixels[i * iwj - 1]);
int blue5 = cm.getBlue(pixels[i * iwj]);
int blue6 = cm.getBlue(pixels[i * iwj1]);
// int blue8 = cm.getBlue(pixels[(i1) * iwj]);
// 水平方向进行中值滤波
if (blue4 = blue5) {
if (blue5 = blue6) {
blue = blue5;
} else {
if (blue4 = blue6) {
blue = blue6;
} else {
blue = blue4;
}
}
} else {
if (blue4blue6) {
blue = blue4;
} else {
if (blue5blue6) {
blue = blue6;
} else {
blue = blue5;
}
}
}
pixels[i * iwj] = alpha24 | red16 | green8
| 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 = https://www.04ip.com/post/{ .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 = https://www.04ip.com/post/{ 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);
}
}
【图片二植化java代码 java 图片二值化】关于图片二植化java代码和java 图片二值化的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- go语言枚举,go语言math包
- 微信视频号企业如何认证,微信视频号企业认证步骤
- 玩真实赛车驾驶游戏犯法吗,玩真实赛车驾驶游戏犯法吗视频
- 包含windows系统音的词条
- go语言安卓运行吗,go语言下载安装
- 包含postgresql设置ip访问的词条
- 手机怎么删多个好友微信,手机怎么删除多个联系人
- php怎样建立数据库和点 php怎样建立数据库和点的关系
- 拍摄水下需要什么,水下拍摄照片