「java视图缩放」java实现图片缩放
本篇文章给大家谈谈java视图缩放,以及java实现图片缩放对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、我写的java图片查看器怎么让图片缩小放大呢?
- 2、怎么用java代码放大或缩小图片不失真。
- 3、怎样实现Java键盘控制图像缩放
- 4、Java能否实现JPanel视图的放大与缩小
- 5、java图片缩放
我写的java图片查看器怎么让图片缩小放大呢?
放大像素会失真,如果你要实现这一共能的话可以用JLabel来显示图片。有一个方法可以实现图片的缩放ImageIcon
ii
=
new
ImageIcon("img/item.jpg");
Image
img
=
ii.getImage();
img
=
img.getScaledInstance(100,
100,
Image.SCALE_DEFAULT);
ii
=
new
ImageIcon(img);这个例子的getScaledInstance方法可以生成一个新的Image对象,可以缩放成指定的大小。
怎么用java代码放大或缩小图片不失真。
放大图像不会导致失真,而缩小图像将不可避免的失真。
Java中也同样是这样。
但java提供了4个缩放的微调选项。
image.SCALE_SMOOTH
//平滑优先
image.SCALE_FAST//速度优先
image.SCALE_AREA_AVERAGING
//区域均值
image.SCALE_REPLICATE
//像素复制型缩放
image.SCALE_DEFAULT
//默认缩放模式
调用方法
Image
new_img=old_img.getScaledInstance(1024,
768,
Image.SCALE_SMOOTH);
得到一张缩放后的新图。
怎样实现Java键盘控制图像缩放
;
如果用 JLabel 显示图像,稍微覆盖它的 paintComponent 方法后根据键盘输入调整它的尺寸既可。
比如:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class C extends JFrame {
public static void main(String[] args) { new C(); }
public C() {
final JLabel imgView = new JLabel(new ImageIcon(你的图片路径)) {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(((ImageIcon)getIcon()).getImage(), 0, 0, getWidth(), getHeight(), null);
}
};
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
double zoomFactor = e.getKeyCode() == KeyEvent.VK_UP ? 1.01 :
e.getKeyCode() == KeyEvent.VK_DOWN ? .99 : 1;
imgView.setSize((int)(imgView.getWidth() * zoomFactor),
(int)(imgView.getHeight() * zoomFactor));
}
});
add(imgView);
setSize(800, 600);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Java能否实现JPanel视图的放大与缩小
问的有点模糊,你看是不是这个
import java.applet.Applet;
import java.awt.Image;
import java.awt.Graphics;
public class MyApplet_16 extends Applet {
Image img;
//初始化方法
public void init() {
//加载gif格式图像文件
img=getImage(getCodeBase(),"飞机.gif");
}
public void paint(Graphics g){ //绘图方法
//原大小显示图片
g.drawImage(img,10,10,this);
//获取图片尺寸
int w=img.getWidth(this);
int h=img.getHeight(this);
//缩小一半显示
g.drawImage(img,250,50,w/2,h/2,this);
//放大一倍显示
g.drawImage(img,160,160,w*2,h*2,this);
}
}
可以的话,将你的具体要求给个百度词条
java图片缩放
根据你的鼠标移动事件,判断你第一次点击的点和最后一次的点,就可以算出这个句型区域的长和宽了,
下面代码自己看
package com.itheima.util;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 制作图片缩略图
*
* @author seawind
*
*/
public class PicUtils {
private String srcFile;
private String destFile;
private int width;
private int height;
private Image img;
/**
* 构造函数
*
* @param fileName
* String
* @throws IOException
*/
public PicUtils(String fileName) throws IOException {
File _file = new File(fileName); // 读入文件
this.srcFile = fileName;
// 查找最后一个.
int index = this.srcFile.lastIndexOf(".");
String ext = this.srcFile.substring(index);
this.destFile = this.srcFile.substring(0, index) + "_s" + ext;
img = javax.imageio.ImageIO.read(_file); // 构造Image对象
width = img.getWidth(null); // 得到源图宽
height = img.getHeight(null); // 得到源图长
}
/**
* 强制压缩/放大图片到固定的大小
*
* @param w
* int 新宽度
* @param h
* int 新高度
* @throws IOException
*/
public void resize(int w, int h) throws IOException {
BufferedImage _image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
_image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(_image); // 近JPEG编码
out.close();
}
/**
* 按照固定的比例缩放图片
*
* @param t
* double 比例
* @throws IOException
*/
public void resize(double t) throws IOException {
int w = (int) (width * t);
int h = (int) (height * t);
resize(w, h);
}
/**
* 以宽度为基准,等比例放缩图片
*
* @param w
* int 新宽度
* @throws IOException
*/
public void resizeByWidth(int w) throws IOException {
int h = (int) (height * w / width);
resize(w, h);
}
/**
* 以高度为基准,等比例缩放图片
*
* @param h
* int 新高度
* @throws IOException
*/
public void resizeByHeight(int h) throws IOException {
int w = (int) (width * h / height);
resize(w, h);
}
/**
* 按照最大高度限制,生成最大的等比例缩略图
*
* @param w
* int 最大宽度
* @param h
* int 最大高度
* @throws IOException
*/
public void resizeFix(int w, int h) throws IOException {
if (width / height w / h) {
resizeByWidth(w);
} else {
resizeByHeight(h);
}
}
/**
* 设置目标文件名 setDestFile
*
* @param fileName
* String 文件名字符串
*/
public void setDestFile(String fileName) throws Exception {
if (!fileName.endsWith(".jpg")) {
throw new Exception("Dest File Must end with \".jpg\".");
}
destFile = fileName;
}
/**
* 获取目标文件名 getDestFile
*/
public String getDestFile() {
return destFile;
}
/**
* 获取图片原始宽度 getSrcWidth
*/
public int getSrcWidth() {
return width;
}
/**
* 获取图片原始高度 getSrcHeight
*/
public int getSrcHeight() {
return height;
}
}
java视图缩放的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现图片缩放、java视图缩放的信息别忘了在本站进行查找喔。