「java缩放代码」java代码怎么放大
本篇文章给大家谈谈java缩放代码,以及java代码怎么放大对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、Java怎么实现上传图片的等比缩放,原文件和缩放的文件都要上传到服务器
- 2、如何用Java实现图形的放大和缩小
- 3、java 图片缩放代码
- 4、java图片缩放
- 5、怎样实现Java键盘控制图像缩放
- 6、怎么用java代码放大或缩小图片不失真。
Java怎么实现上传图片的等比缩放,原文件和缩放的文件都要上传到服务器
/**
* 等比例缩放图片
* @param infile
* @param outfile
* @param width
* @param height
* @param quality
* @throws IOException
* @throws InterruptedException
*/
public static void Thumbnail(String infile, String outfile, int width, int height, int quality) throws IOException, InterruptedException {
// save thumbnail image to OUTFILE
//System.out.println("infile:" + infile);
BufferedImage thumbImage = null;
BufferedOutputStream out = null;
Image image = null;
image = Toolkit.getDefaultToolkit().createImage(infile);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
int thumbWidth = width;
int thumbHeight = height;
double thumbRatio = (double) thumbWidth / (double) thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double) imageWidth / (double) imageHeight;
if (thumbRatio imageRatio) {
thumbHeight = (int) (thumbWidth / imageRatio);
} else {
thumbWidth = (int) (thumbHeight * imageRatio);
}
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image,0,0, thumbWidth, thumbHeight, null);
out = new BufferedOutputStream(new FileOutputStream(outfile));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float) quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
out.close();
thumbImage = null;
out = null;
image = null;
}
原文转载自:
如何用Java实现图形的放大和缩小
java实现图形的放大和缩小,其实就是在画图时,改变图片的长和宽。以下代码参考一下:
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class App extends JFrame implements MouseListener, ActionListener {
int x = 0;
int y = 0;
File[] selectedFiles = null;
int fileIndex = 0;
int width = 200;
int height = 200;
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);
JPanel panel = new ImagePanel();
panel.setBounds(12, 40, 370, 218);
getContentPane().add(panel);
addMouseListener(this);
JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(this);
btnBrowse.setBounds(12, 9, 91, 21);
getContentPane().add(btnBrowse);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG GIF Images", "jpg", "gif");
// 设置文件类型
chooser.setFileFilter(filter);
// 打开选择器面板
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedFiles = chooser.getSelectedFiles();
repaint();
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
x = point.x;
y = point.y;
}
public void mouseReleased(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
int thisX = point.x;
int thisY = point.y;
System.out.println("thisX=" + thisX + " " + "thisY=" + thisY);
if ((y - thisY 20 y - thisY 0)
|| (y - thisY 0 y - thisY -20)) {
// Y 在20范围内移动认为是水平移动
if (x thisX) {
// right
if (selectedFiles != null
fileIndex selectedFiles.length - 1) {
fileIndex++;
}
} else {
// left
if (selectedFiles != null fileIndex 0) {
fileIndex--;
}
}
} else {
if (x thisX) {
// 右下
width += 20;
height += 20;
} else {
// 左上
width -= 20;
height -= 20;
}
}
repaint();
}
class ImagePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
if (selectedFiles != null) {
ImageIcon icon = new ImageIcon(selectedFiles[fileIndex]
.getPath());
g.drawImage(icon.getImage(), 0, 0, width, height, this);
}
}
}
}
java 图片缩放代码
直接给你一个类,直接套用就好了
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
public class Resize {
BufferedImage bufImage;
int width;
int height;
public Resize() {
// TODO Auto-generated constructor stub
}
public Resize(String srcPath,int width,int height) {
this.width = width;
this.height = height;
try{
this.bufImage = ImageIO.read(new File(srcPath));
}catch(Exception e){
e.printStackTrace();
}
}
public static BufferedImage rize(BufferedImage srcBufImage,int width,int height){
BufferedImage bufTarget = null;
double sx = (double) width / srcBufImage.getWidth();
double sy = (double) height / srcBufImage.getHeight();
int type = srcBufImage.getType();
if(type == BufferedImage.TYPE_CUSTOM){
ColorModel cm = srcBufImage.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(width,
height);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
bufTarget = new BufferedImage(cm, raster, alphaPremultiplied, null);
}else
bufTarget = new BufferedImage(width, height, type);
Graphics2D g = bufTarget.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(srcBufImage, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return bufTarget;
}
}
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键盘控制图像缩放
;
如果用 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代码放大或缩小图片不失真。
放大图像不会导致失真,而缩小图像将不可避免的失真。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代码放大或缩小图片不失真。
关于java缩放代码和java代码怎么放大的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-29,除非注明,否则均为
原创文章,转载请注明出处。