如题
要求可以用鼠标拖动截图区大小和显示截图区预览
鼠标双击后截取图片存储到指定文件夹
要求可以用鼠标拖动截图区大小和显示截图区预览
鼠标双击后截取图片存储到指定文件夹
解决方案 »
- 如何在Jsp页面获得数据库的数据?
- HTTP Status 404 - Servlet action is not available
- Struts2 表单的javascript验证问题
- 菜鸟提问,困扰我几天的问题
- 求教~急!! 关于JDBC驱动的连接
- 关于logic:iterate标签的问题~高分求教!
- tomcat 能否做邮件服务器?
- 怎样在jsp页面上以表格的形式显示数据库里的内容?(在线等待)急!!
- 删除问题回答者都给分
- 请教:关于英文,简体中文,繁体中文在JSP/JAVA中的整体解决方案!!!!!
- 怎样在java代码中写空格输出到jsp页面
- 有谁知道ERROR:JDWP Unable to get JNI 1.2 environment ,jvm-> GetEvn() return =-2
js确定截取图片的代码:http://topic.csdn.net/u/20080806/02/B295D4E4-C98F-4621-9835-B7D915B4952D.html
然后通过java来截取对应的图片即可完成楼主的功能!
呵
太谢谢了
不过用java怎么截取图片还是不知道哦 `
拜托了
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
/*******************************************************************************
* 该JavaBean可以直接在其他Java应用程序中调用,实现屏幕的"拍照" This JavaBean is used to snapshot the
* GUI in a Java application! You can embeded it in to your java application
* source code, and us it to snapshot the right GUI of the application
*
* @see javax.ImageIO
* @author liluqun ([email protected])
* @version 1.0
*
******************************************************************************/
class GuiCamera
{
private String fileName; // 文件的前缀
private String defaultName = "GuiCamera";
static int serialNum = 0;
private String imageFormat; // 图像文件的格式
private String defaultImageFormat = "png";
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
/***********************************************************************
* 默认的文件前缀为GuiCamera,文件格式为PNG格式 The default construct will use the
* default Image file surname "GuiCamera", and default image format
* "png"
**********************************************************************/
public GuiCamera()
{
fileName = defaultName;
imageFormat = defaultImageFormat;
}
/***********************************************************************
* @param s
* the surname of the snapshot file
* @param format
* the format of the image file, it can be "jpg" or "png"
* 本构造支持JPG和PNG文件的存储
**********************************************************************/
public GuiCamera(String s, String format)
{
fileName = s;
imageFormat = format;
}
/***********************************************************************
* 对屏幕进行拍照 snapShot the Gui once
**********************************************************************/
public void snapShot()
{
try
{
// 拷贝屏幕到一个BufferedImage对象screenshot
//这里 createScreenCapture中的4个参数就是需要截取的图形的2个顶点,也就是通过js确定的2个点
BufferedImage screenshot = (new Robot())
.createScreenCapture(new Rectangle(0, 0,
(int) d.getWidth(), (int) d.getHeight()));
serialNum++;
// 根据文件前缀变量和文件格式变量,自动生成文件名
String name = fileName + String.valueOf(serialNum) + "."
+ imageFormat;
File f = new File(name);
System.out.print("Save File " + name);
// 将screenshot对象写入图像文件
ImageIO.write(screenshot, imageFormat, f);
System.out.print("..Finished! ");
} catch (Exception ex)
{
System.out.println(ex);
}
}
public static void main(String[] args)
{
GuiCamera cam = new GuiCamera("Test", "png");
cam.snapShot();
}
}
太感谢了
o(∩_∩)o...
java是运行在服务器端的语言。而javascript运行在客户端。按照你所传的参数所截取的应该是服务器的屏幕而不是客户端!!!!
要不就装qq把,先按ctrl+alt+a打开截图功能,然后按照楼主的那种操作就可以截图了。
但是applet有个安全问题,默认是不能访问客户端资源的,需要签名!
截取的是服务器端的截屏,是截不到客户的截屏的。
第一次进来,里面人气不够
我是有一个问题想问大家,希望 能得到帮助,
谢谢!
设计并实现一个简单的输入程序
程序要求:
1. 要求输入学号、姓名、班级、课程名称、成绩,并保存到数据库中;
2. 请尽量美化界面;
3. 课程名称和班级名称要以下拉选择的方式输入;
4. 采用相应的数据库管理系统直接打开数据库,察看数据输入是否正确。
数据库设计要求:起码要有课程表、班级表、学生名单表、成绩表等四个表,字段的属性要求正确设置。
基础数据处理:先用相应的数据库管理系统,直接输入2个以上班级、2门以上课程、3个以上学生的基本情况。
可采用任何语言和任何数据库管理系统,C/S或B/S模式,实现本功能。
package test_img;import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.imageio.ImageIO; import com.sun.image.codec.jpeg.JPEGImageEncoder;import sun.awt.image.ToolkitImage;
class GuiCamera
{
private String fileName; // 文件的前缀
private String defaultName = "GuiCamera";
static int serialNum = 0;
private String imageFormat; // 图像文件的格式
private String defaultImageFormat = "jpg";
// Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
public GuiCamera()
{
fileName = defaultName;
imageFormat = defaultImageFormat;
}
public GuiCamera(String s, String format)
{
fileName = s;
imageFormat = format;
}
public void snapShot(String resource,int x,int y,int width,int height)
{
try
{
Image img = ImageIO.read(GuiCamera.class.getClassLoader().getResource(resource));
BufferedImage screenshot = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_3BYTE_BGR);
Graphics g = screenshot.getGraphics();
g.drawImage(img, 0,0, null);
g.drawString("cut image", 50, 50);
screenshot = screenshot.getSubimage(x, y, width, height);
g.dispose();
screenshot.flush();
serialNum++;
// 根据文件前缀变量和文件格式变量,自动生成文件名
String name = fileName + String.valueOf(serialNum) + "."
+ imageFormat;
File f = new File(name);
System.out.print("Save File " + name);
// 将screenshot对象写入图像文件
ImageIO.write(screenshot, imageFormat, f);
System.out.print("..Finished! ");
} catch (Exception ex)
{
System.out.println(ex);
}
}
public static void main(String[] args)
{
GuiCamera cam = new GuiCamera("Test", "jpg");
cam.snapShot("img/demo1.jpg",20,20,200,100);
}
}
变来变去的!
下面代码中用到的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:[email protected]*/
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.CS_MAX) {
substituteColorModel(cm,new GrayModel(cm));
}
else if (modelStyle==GrayModel.CS_FLOAT) {
substituteColorModel(cm,new GrayModel(cm,modelStyle));
}
}
public int filterRGB(int x,int y,int pixel) {
return pixel;
}
}/* GrayModel.java*/
/*@author:cherami */
/*email:[email protected]*/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)+(gray<<16)+(gray<<8)+gray;
}
} 如果你有自己的算法或者想取得特殊的效果,你可以修改类GrayModel的方法getGrayLevel()。
色彩变换 根据上面的原理,我们也可以实现色彩变换,这样的效果就很多了。下面是一个反转变换的例子:/* ReverseColorModel.java*/
/*@author:cherami */
/*email:[email protected]*/
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:[email protected]*/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:[email protected]*/
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) {
}
}
public void paint(Graphics g) {
Graphics2D g2=(Graphics2D)g;
bimg=new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
Graphics2D srcG = bimg.createGraphics();
RenderingHints rhs = g2.getRenderingHints();
srcG.setRenderingHints(rhs);
srcG.drawImage(source, 0, 0, null);
ColorSpace graySpace=ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op=new ColorConvertOp(graySpace,rhs);
gray2=new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
op.filter(bimg,gray2);
g2.drawImage(source,40,40,this);
g2.drawImage(gray,80,40,this);
g2.drawImage(gray2,120,40,this);
g2.drawImage(gray3,160,40,this);
g2.drawImage(clip,40,80,this);
g2.drawImage(bigimg,80,80,this);
}
public void update(Graphics g) {
paint(g);
}
public static void main(String args[]) {
GrayImage m=new GrayImage();
m.setSize(400,400);
m.setVisible(true);
}
}