「java图像缩放」java图片缩略图

博主:adminadmin 2023-01-23 13:42:08 247

本篇文章给大家谈谈java图像缩放,以及java图片缩略图对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java怎么设置插入图片大小?

用Image中的getScaledInstance方法得到一个按照指定宽度和高度缩放以后的Image实例,然后再用setImage方法设置ImageIcon所显示的图像。

static int SCALE_DEFAULT表示默认的图像缩放算法。

public Image getScaledInstance创建此图像的缩放版本。返回一个新的 Image 对象,默认情况下,该对象按指定的 width 和 height 呈现图像。即使已经完全加载了初始源图像,新的 Image 对象也可以被异步加载。

如果 width 或 height 为负数,则替换该值以维持初始图像尺寸的高宽比。如果 width 和 height 都为负,则使用初始图像尺寸。

我写的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 图片缩放代码

直接给你一个类,直接套用就好了

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实现图形的放大和缩小?

要用Java实现图形的放大和缩小,可以使用以下代码:

import java.awt.*;import java.awt.event.*;import javax.swing.*;//实现矩形在规定时间间隔里循环放大缩小;

public class Test02 extends JFrame implements Runnable{ static int w = 0,h = 0; //w:矩形宽度,h:矩形高度 ;

boolean flag = false;//false:放大,true:缩小 public Test02(){    this.setSize (500,500);  this.setVisible (true);  this.setTitle (w+","+h);  this.setLocationRelativeTo (this);  this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); }  public void paint(Graphics g){  int width = getWidth();

//窗口宽度  int height = getWidth();  //窗口高度  g.setColor (Color.WHITE); //设置画笔颜色  g.fillRect (0,0,width,height);  //填充窗口  int x = (width-w)/2; //x:矩形左上角横坐标  int y = (height-h)/2; //y:矩形左上角纵坐标  g.setColor (Color.BLUE); //同上...  g.drawRect (x,y,w,h);

//画矩形,实心矩形为fillRect(....)  g.setColor (Color.RED); //同上...  g.drawOval (x,y,w,h);     //画椭圆  setTitle (w+","+h);     //以矩形宽和高来设置窗口标题 } public void setSize(){  float n = getWidth()/getWidth();  if(w==0||h==0)   flag = false;

if(w==getWidth()||h==getWidth())   flag = true;  if(!flag){   w+=1;   h+=n;  }  if(flag){   w-=1;   h-=n;  }   } public void run(){  while(true){   try{    Thread.sleep (5);//间隔   }catch(InterruptedException ie){}      setSize();   repaint();  } } public static void main(String[] args){  Test02 t = new Test02();  Thread th = new Thread(t);  th.start(); }}

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。

Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

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实现图形的放大和缩小,其实就是在画图时,改变图片的长和宽。以下代码参考一下:

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图像缩放和java图片缩略图的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。