java 在屏幕给定区域内找图的实现!
根据问题来看,比历纳较梁烂圆复杂,但是可以提供一个思路,因为每个图都是有名称的
因此使用图像识别可能就不必了,橡塌但是可以通过相应手段在特定区域内查找某
一文件的名称,查找到后可以返回该文件所在坐标,你可以自己试一试,如果
有什么疑问,可以联系我,一起研究研究
java适合做图像处理吗?
Java图像处理技巧四则
下面代码中用到的sourceImage是一个已经存在的Image对象
图像剪雀租切
对于一个已经存在的Image对象,要得到它的一个局部图像,可以使用下面的步骤:
//import java.awt.*;
//import java.awt.image.*;
Image croppedImage;
ImageFilter cropFilter;
CropFilter =new CropImageFilter(25,30,75,75); //四个参数分别为图像起点坐标和宽高,即CropImageFilter(int x,int y,int width,int height),详细情况请参考API
CroppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter));
如果是在Component的子类中使用,可以将上面的Toolkit.getDefaultToolkit().去掉。FilteredImageSource是一个ImageProducer对象。
图像缩放
对于一个已经存在的Image对象,得到它的一个缩放的Image对象可以使用Image的getScaledInstance方法:
Image scaledImage=sourceImage. getScaledInstance(100,100, Image.SCALE_DEFAULT); //得到一个100X100的图像
Image doubledImage=sourceImage. getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2, Image.SCALE_DEFAULT); //得到一个放大两倍的图像,这个程序一般在一个swing的组件中使用,而类Jcomponent实现了图像观察者接口ImageObserver,所有可以使用this。
//其它情况请参考API
灰度变换
下面的程序使用三种方法对一个彩色图像进行灰度变换,变换的效果都不一样。一般而言,灰度变换的算法是将象素的三个洞岁孝颜色分量使用R*0.3+G*0.59+ B*0.11得到灰度值,然后将之赋值给红绿蓝,这样颜色取得的效果就是灰度的。另一种就是取红绿蓝三色中的最大值作为灰度值。java核心包也有一种算法,但是没有看源代码,不知纳稿道具体算法是什么样的,效果和上述不同。
/* GrayFilter.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.image.*;
public class GrayFilter extends RGBImageFilter {
int modelStyle;
public GrayFilter() {
modelStyle=GrayModel.CS_MAX;
canFilterIndexColorModel=true;
}
public GrayFilter(int style) {
modelStyle=style;
canFilterIndexColorModel=true;
}
public void setColorModel(ColorModel cm) {
if (modelStyle==GrayModel
else if (modelStyle==GrayModel
}
public int filterRGB(int x,int y,int pixel) {
return pixel;
}
}
/* GrayModel.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.image.*;
public class GrayModel extends ColorModel {
public static final int CS_MAX=0;
public static final int CS_FLOAT=1;
ColorModel sourceModel;
int modelStyle;
public GrayModel(ColorModel sourceModel) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle=0;
}
public GrayModel(ColorModel sourceModel,int style) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle=style;
}
public void setGrayStyle(int style) {
modelStyle=style;
}
protected int getGrayLevel(int pixel) {
if (modelStyle==CS_MAX) {
return Math.max(sourceModel.getRed(pixel),Math.max(sourceModel.getGreen(pixel),sourceModel.getBlue(pixel)));
}
else if (modelStyle==CS_FLOAT){
return (int)(sourceModel.getRed(pixel)*0.3+sourceModel.getGreen(pixel)*0.59+sourceModel.getBlue(pixel)*0.11);
}
else {
return 0;
}
}
public int getAlpha(int pixel) {
return sourceModel.getAlpha(pixel);
}
public int getRed(int pixel) {
return getGrayLevel(pixel);
}
public int getGreen(int pixel) {
return getGrayLevel(pixel);
}
public int getBlue(int pixel) {
return getGrayLevel(pixel);
}
public int getRGB(int pixel) {
int gray=getGrayLevel(pixel);
return (getAlpha(pixel)24)+(gray16)+(gray8)+gray;
}
}
如果你有自己的算法或者想取得特殊的效果,你可以修改类GrayModel的方法getGrayLevel()。
色彩变换
根据上面的原理,我们也可以实现色彩变换,这样的效果就很多了。下面是一个反转变换的例子:
/* ReverseColorModel.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.image.*;
public class ReverseColorModel extends ColorModel {
ColorModel sourceModel;
public ReverseColorModel(ColorModel sourceModel) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
}
public int getAlpha(int pixel) {
return sourceModel.getAlpha(pixel);
}
public int getRed(int pixel) {
return ~sourceModel.getRed(pixel);
}
public int getGreen(int pixel) {
return ~sourceModel.getGreen(pixel);
}
public int getBlue(int pixel) {
return ~sourceModel.getBlue(pixel);
}
public int getRGB(int pixel) {
return (getAlpha(pixel)24)+(getRed(pixel)16)+(getGreen(pixel)8)+getBlue(pixel);
}
}
/* ReverseColorModel.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.image.*;
public class ReverseFilter extends RGBImageFilter {
public ReverseFilter() {
canFilterIndexColorModel=true;
}
public void setColorModel(ColorModel cm) {
substituteColorModel(cm,new ReverseColorModel(cm));
}
public int filterRGB(int x,int y,int pixel) {
return pixel;
}
}
要想取得自己的效果,需要修改ReverseColorModel.java中的三个方法,getRed、getGreen、getBlue。
下面是上面的效果的一个总的演示程序。
/*GrayImage.java*/
/*@author:cherami */
/*email:cherami@163.net*/
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.color.*;
public class GrayImage extends JFrame{
Image source,gray,gray3,clip,bigimg;
BufferedImage bimg,gray2;
GrayFilter filter,filter2;
ImageIcon ii;
ImageFilter cropFilter;
int iw,ih;
public GrayImage() {
ii=new ImageIcon(\”images/11.gif\”);
source=ii.getImage();
iw=source.getWidth(this);
ih=source.getHeight(this);
filter=new GrayFilter();
filter2=new GrayFilter(GrayModel.CS_FLOAT);
gray=createImage(new FilteredImageSource(source.getSource(),filter));
gray3=createImage(new FilteredImageSource(source.getSource(),filter2));
cropFilter=new CropImageFilter(5,5,iw-5,ih-5);
clip=createImage(new FilteredImageSource(source.getSource(),cropFilter));
bigimg=source.getScaledInstance(iw*2,ih*2,Image.SCALE_DEFAULT);
MediaTracker mt=new MediaTracker(this);
mt.addImage(gray,0);
try {
mt.waitForAll();
} catch (Exception e) {
}
java blob
java blob是什么,让我们一起了解一下?
Blob是计算机视觉图像中的一块连通区域,Blob分析的就是对前景或背景分离后的二值培磨图像,进行连通域提取和标记以及计算Blob的一些相关特征,而且通过Blob提取,还可以获得相关区域的信息。
Blob分析的重要一个步骤是连通区域的确定,那么它的优缺点是什么?
优点:
Blob在目消中谈标跟踪的优势有:
1、通拿碰过Blob提取,可以获得相关区域的信息,这些信息可以作为边缘监测器或者角点检测器的补充信息。在目标识别中,Blob可以提供局部的统计信息和外貌信息,这些信息能够为目标识别和跟踪提供依据。
2、可以利用Blob对直方图进行峰值检测。
3、Blob还可以作为纹理分析和纹理识别的基元。
4、通过Blob分析,可以得到目标的个数及其所在区域,在进行目标匹配时,不需要对全局图像进行搜索。
缺点:
1、速度过慢,要整个区域作逐点扫描。
2、Blob分析难度大。这是一纯几何学上的问题,一个不规则的形状,如何计算它的面积、大小没有简单易行的算法,太过复杂,运算时间就长,速度就更慢了。
3、实际应用。Blob算法在实际应用中,非常依赖光源。几乎可以说,Blob算法如果离开了一个可靠的光源设计,则完全不起作用。
那么java是怎样对Blob读写的?示例如下:
package com.you.sister; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Properties; public class BlobTest { public static Connection conn; public static Connection getConn() throws Exception { FileInputStream fis = new FileInputStream(new File(“jdbc.properties”)); Properties prop = new Properties(); prop.load(fis); String driver = prop.getProperty(“jdbc.driver”); String url = prop.getProperty(“jdbc.url”); String username = prop.getProperty(“jdbc.username”); String password = prop.getProperty(“jdbc.password”); Class.forName(driver); return DriverManager.getConnection(url, username, password); } public static void main(String[] args) throws Exception { conn = getConn(); readBlob(); writeBlob(); conn.close(); } /** * 从数据库中读大对象出来 * 保存在本地 */ public static void readBlob() { try { String readSql = “select * from emp where empno = ?”; PreparedStatement ps = conn.prepareStatement(readSql); ps.setInt(1, 7369); ResultSet rs = ps.executeQuery(); while (rs.next()) { Blob image = rs.getBlob(“image”); DataOutputStream dos = // 在FileOutputStream中指定文件输出路径 new DataOutputStream(new FileOutputStream(7369 + “_image.jpeg”)); InputStream fis = image.getBinaryStream(); int out; byte[] outByte = new byte [100]; // 将blob对象输入流写入本地输出流中 while ((out = fis.read(outByte)) != -1) { dos.write(outByte); } fis.close(); dos.flush(); dos.close(); } rs.close(); ps.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 将大对象文件保存进数据库中 */ public static void writeBlob() { try { BufferedInputStream fis = new BufferedInputStream(new FileInputStream(new File(“D:\\Tulips.jpg”))); // 如果是新插入字段,则将大对象对应字段插入为empty_clob(); // 如果是修改,则可以先update 该行数据,将大对象对应字段设置为empty_clob(); String writeSql = “select * from emp where empno = ? for update”; PreparedStatement ps = conn.prepareStatement(writeSql); ps.setInt(1, 7499); conn.setAutoCommit(false); ResultSet rs = ps.executeQuery(); while (rs.next()) { oracle.sql.BLOB image = (oracle.sql.BLOB)rs.getBlob(“image”); BufferedOutputStream bos = new BufferedOutputStream(image.getBinaryOutputStream()); int c; // 将实际文件中的内容以二进制的形式来输出到blob对象对应的输出流中 while ((c = fis.read()) != -1) { bos.write(c); } fis.close(); bos.close(); } conn.commit(); rs.close(); ps.close(); } catch (Exception e) { e.printStackTrace(); } } }
关于JAVA的图片处理问题
public static boolean write(RenderedImage im, String formatName, File output) throws IOException
使用支持给定格式的任意 ImageWriter 将一个图像写入 File。如蚂盯果已经有一个 File 存在,则丢弃其内容。
参数:im – 要写入的 RenderedImage。
formatName – 包含格式非正式名称的 String。
output – 将在其中写入数据的 File。
返回:如果没有找到合适的 writer,则返回 false。
抛出: IllegalArgumentException – 如果任何参数为 null。
IOException – 如闷仿和果在写入过程中发生错误。
说白了,就是按指定的formatName把图片存到file(或OutputStream)中。formatName是已注册的、可以保存图片的writer的非正式名称,比如“jpeg”,“tiff”。如果想知道到底有哪些writer在你的机器上被注册了,用ImageIO.getWriterFormatNames(),返回类型是String[] 。同样的,还有读取图片的reader,对应的是ImageIO.getReaderFormatNames()。
最后要说的是,这个方法是保存图片,和上传没有关系。你可能是要上传图大森片后再保存吧!