javapng,Javapng转pdf

本文目录一览:

JAVA PNG图片分割,无背景。

怎么会无法呢。java支持图片格式中最好的就是png,别的图片可以不支持,png是默认支持的。用ARGB色彩模型直接对png操作即可,

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

public class Test {

static public void main(String 参数[]){

try{

BufferedImage img=ImageIO.read(new File(“test.png”));

int half_w=img.getWidth()/2;

int rgb[]=new int[half_w*img.getHeight()];

img.getRGB(0, 0, half_w, img.getHeight(), rgb, 0, half_w);

BufferedImage img_half=new BufferedImage(half_w, img.getHeight(), BufferedImage.TYPE_INT_ARGB);

img_half.setRGB(0, 0,half_w,img.getHeight(), rgb,0,half_w);

//保存到新文件half.png里面

ImageIO.write(img_half,”PNG”,new File(“half.png”));

}catch (IOException e){

e.printStackTrace();

}

}

}

======

得到half.png签名图的左半边,保留了透明的背景。

这已经只有5-6行,抛砖引玉,用raster可能代码更简..

java压缩png图片

您转换的是图片的后缀名吧?您这样的方式已经把图片的信息删除了!

您去下载一个java图片压缩器吧

或者直接在Java下编辑代码来实习转换

package com.sun.util.cyw;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**

* 图片工具类

* 压缩图片大小

* @author Cyw

* @version 1.0

*/

public class ZIPImage {

private File file = null;

private String outPutFilePath;

private String inPutFilePath;

private String inPutFileName;

private boolean autoBuildFileName;

private String outPutFileName;

private int outPutFileWidth = 100; // 默认输出图片宽

private int outPutFileHeight = 100; // 默认输出图片高

private static boolean isScaleZoom = true; // 是否按比例缩放

public ZIPImage() {

outPutFilePath = “”;

inPutFilePath = “”;

inPutFileName = “”;

autoBuildFileName = true;

outPutFileName = “”;

}

/**

*

* @param ipfp

* 源文件夹路径

* @param ipfn

* 源文件名

* @param opfp

* 目标文件路径

* @param opfn

* 目标文件名

*/

public ZIPImage(String ipfp, String ipfn, String opfp, String opfn) {

outPutFilePath = opfp;

inPutFilePath = ipfp;

inPutFileName = ipfn;

autoBuildFileName = true;

outPutFileName = opfn;

}

/**

*

* @param ipfp

* 源文件夹路径

* @param ipfn

* 源文件名

* @param opfp

* 目标文件路径

* @param opfn

* 目标文件名

* @param aBFN

* 是否自动生成目标文件名

*/

public ZIPImage(String ipfp, String ipfn, String opfp, String opfn,

boolean aBFN) {

outPutFilePath = opfp;

inPutFilePath = ipfp;

inPutFileName = ipfn;

autoBuildFileName = aBFN;

outPutFileName = opfn;

}

public boolean isAutoBuildFileName() {

return autoBuildFileName;

}

public void setAutoBuildFileName(boolean autoBuildFileName) {

this.autoBuildFileName = autoBuildFileName;

}

public String getInPutFilePath() {

return inPutFilePath;

}

public void setInPutFilePath(String inPutFilePath) {

this.inPutFilePath = inPutFilePath;

}

public String getOutPutFileName() {

return outPutFileName;

}

public void setOutPutFileName(String outPutFileName) {

this.outPutFileName = outPutFileName;

}

public String getOutPutFilePath() {

return outPutFilePath;

}

public void setOutPutFilePath(String outPutFilePath) {

this.outPutFilePath = outPutFilePath;

}

public int getOutPutFileHeight() {

return outPutFileHeight;

}

public void setOutPutFileHeight(int outPutFileHeight) {

this.outPutFileHeight = outPutFileHeight;

}

public int getOutPutFileWidth() {

return outPutFileWidth;

}

public void setOutPutFileWidth(int outPutFileWidth) {

this.outPutFileWidth = outPutFileWidth;

}

public boolean isScaleZoom() {

return isScaleZoom;

}

public void setScaleZoom(boolean isScaleZoom) {

this.isScaleZoom = isScaleZoom;

}

public String getInPutFileName() {

return inPutFileName;

}

public void setInPutFileName(String inPutFileName) {

this.inPutFileName = inPutFileName;

}

/**

* 压缩图片大小

*

* @return boolean

*/

public boolean compressImage() {

boolean flag = false;

try {

if (inPutFilePath.trim().equals(“”)) {

throw new NullPointerException(“源文件夹路径不存在。”);

}

if (inPutFileName.trim().equals(“”)) {

throw new NullPointerException(“图片文件路径不存在。”);

}

if (outPutFilePath.trim().equals(“”)) {

throw new NullPointerException(“目标文件夹路径地址为空。”);

} else {

if (!ZIPImage.mddir(outPutFilePath)) {

throw new FileNotFoundException(outPutFilePath

+ ” 文件夹创建失败!”);

}

}

if (this.autoBuildFileName) { // 自动生成文件名

String tempFile[] = getFileNameAndExtName(inPutFileName);

outPutFileName = tempFile[0] + “_cyw.” + tempFile[1];

compressPic();

} else {

if (outPutFileName.trim().equals(“”)) {

throw new NullPointerException(“目标文件名为空。”);

}

compressPic();

}

} catch (Exception e) {

flag = false;

e.printStackTrace();

return flag;

}

return flag;

}

// 图片处理

private void compressPic() throws Exception {

try {

// 获得源文件

file = new File(inPutFilePath + inPutFileName);

if (!file.exists()) {

throw new FileNotFoundException(inPutFilePath + inPutFileName

+ ” 文件不存在!”);

}

Image img = ImageIO.read(file);

// 判断图片格式是否正确

if (img.getWidth(null) == -1) {

throw new Exception(“文件不可读!”);

} else {

int newWidth;

int newHeight;

// 判断是否是等比缩放

if (ZIPImage.isScaleZoom == true) {

// 为等比缩放计算输出的图片宽度及高度

double rate1 = ((double) img.getWidth(null))

/ (double) outPutFileWidth + 0.1;

double rate2 = ((double) img.getHeight(null))

/ (double) outPutFileHeight + 0.1;

// 根据缩放比率大的进行缩放控制

double rate = rate1 rate2 ? rate1 : rate2;

newWidth = (int) (((double) img.getWidth(null)) / rate);

newHeight = (int) (((double) img.getHeight(null)) / rate);

} else {

newWidth = outPutFileWidth; // 输出的图片宽度

newHeight = outPutFileHeight; // 输出的图片高度

}

BufferedImage tag = new BufferedImage((int) newWidth,

(int) newHeight, BufferedImage.TYPE_INT_RGB);

/*

* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢

*/

tag.getGraphics().drawImage(

img.getScaledInstance(newWidth, newHeight,

Image.SCALE_SMOOTH), 0, 0, null);

FileOutputStream out = new FileOutputStream(outPutFilePath

+ outPutFileName);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(tag);

out.close();

}

} catch (IOException ex) {

ex.printStackTrace();

}

}

/**

* 创建文件夹目录

*

* @param filePath

* @return

* @throws Exception

*/

@SuppressWarnings(“unused”)

private static boolean mddir(String filePath) throws Exception {

boolean flag = false;

File f = new File(filePath);

if (!f.exists()) {

flag = f.mkdirs();

} else {

flag = true;

}

return flag;

}

/**

* 获得文件名和扩展名

*

* @param fullFileName

* @return

* @throws Exception

*/

private String[] getFileNameAndExtName(String fullFileName)

throws Exception {

String[] fileNames = new String[2];

if (fullFileName.indexOf(“.”) == -1) {

throw new Exception(“源文件名不正确!”);

} else {

fileNames[0] = fullFileName.substring(0, fullFileName

.lastIndexOf(“.”));

fileNames[1] = fullFileName

.substring(fullFileName.lastIndexOf(“.”) + 1);

}

return fileNames;

}

public Image getSourceImage() throws IOException{

//获得源文件

file = new File(inPutFilePath + inPutFileName);

if (!file.exists()) {

throw new FileNotFoundException(inPutFilePath + inPutFileName

+ ” 文件不存在!”);

}

Image img = ImageIO.read(file);

return img;

}

/*

* 获得图片大小

* @path :图片路径

*/

public long getPicSize(String path) {

File file = new File(path);

return file.length();

}

}

//下面是测试程序

package com.sun.util.cyw;

import java.awt.Image;

import java.io.IOException;

public class ImageTest {

public static void main(String[] args) throws IOException {

ZIPImage zip=new ZIPImage(“d:\\”,”1.jpg”,”d:\\test\\”,”处理后的图片.jpg”,false);

zip.setOutPutFileWidth(1000);

zip.setOutPutFileHeight(1000);

Image image=zip.getSourceImage();

long size=zip.getPicSize(“d:\\1.jpg”);

System.out.println(“处理前的图片大小 width:”+image.getWidth(null));

System.out.println(“处理前的图片大小 height:”+image.getHeight(null));

System.out.println(“处理前的图片容量:”+ size +” bit”);

zip.compressImage();

}

}

您好!请问用java怎么将截取png的图片中间一部分,以及如何压缩一个png图片?

getSubimage方法是进行图片裁剪。

举例:

public static void main(String[] args) {

try {

//从特定文件载入

BufferedImage bi = ImageIO.read(new File(“c:\\test.png”));

bi.getSubimage(0, 0, 10, 10);//前两个值是坐标位置X、Y,后两个是长和宽

} catch (IOException e) {

e.printStackTrace();

}

}

以下是进行的图片压缩,涉及到多个工具类。

/**

* 图片工具类

* 压缩图片大小

* @author Cyw

* @version 1.0

*/

public class ZIPImage {

private File file = null;

private String outPutFilePath;

private String inPutFilePath;

private String inPutFileName;

private boolean autoBuildFileName;

private String outPutFileName;

private int outPutFileWidth = 100; // 默认输出图片宽

private int outPutFileHeight = 100; // 默认输出图片高

private static boolean isScaleZoom = true; // 是否按比例缩放

public ZIPImage() {

outPutFilePath = “”;

inPutFilePath = “”;

inPutFileName = “”;

autoBuildFileName = true;

outPutFileName = “”;

}

/**

* @param ipfp

* 源文件夹路径

* @param ipfn

* 源文件名

* @param opfp

* 目标文件路径

* @param opfn

* 目标文件名

*/

public ZIPImage(String ipfp, String ipfn, String opfp, String opfn) {

outPutFilePath = opfp;

inPutFilePath = ipfp;

inPutFileName = ipfn;

autoBuildFileName = true;

outPutFileName = opfn;

}

/**

* @param ipfp

* 源文件夹路径

* @param ipfn

* 源文件名

* @param opfp

* 目标文件路径

* @param opfn

* 目标文件名

* @param aBFN

* 是否自动生成目标文件名

*/

public ZIPImage(String ipfp, String ipfn, String opfp, String opfn,

boolean aBFN) {

outPutFilePath = opfp;

inPutFilePath = ipfp;

inPutFileName = ipfn;

autoBuildFileName = aBFN;

outPutFileName = opfn;

}

public boolean isAutoBuildFileName() {

return autoBuildFileName;

}

public void setAutoBuildFileName(boolean autoBuildFileName) {

this.autoBuildFileName = autoBuildFileName;

}

public String getInPutFilePath() {

return inPutFilePath;

}

public void setInPutFilePath(String inPutFilePath) {

this.inPutFilePath = inPutFilePath;

}

public String getOutPutFileName() {

return outPutFileName;

}

public void setOutPutFileName(String outPutFileName) {

this.outPutFileName = outPutFileName;

}

public String getOutPutFilePath() {

return outPutFilePath;

}

public void setOutPutFilePath(String outPutFilePath) {

this.outPutFilePath = outPutFilePath;

}

public int getOutPutFileHeight() {

return outPutFileHeight;

}

public void setOutPutFileHeight(int outPutFileHeight) {

this.outPutFileHeight = outPutFileHeight;

}

public int getOutPutFileWidth() {

return outPutFileWidth;

}

public void setOutPutFileWidth(int outPutFileWidth) {

this.outPutFileWidth = outPutFileWidth;

}

public boolean isScaleZoom() {

return isScaleZoom;

}

public void setScaleZoom(boolean isScaleZoom) {

this.isScaleZoom = isScaleZoom;

}

public String getInPutFileName() {

return inPutFileName;

}

public void setInPutFileName(String inPutFileName) {

this.inPutFileName = inPutFileName;

}

/**

* 压缩图片大小

* @return boolean

*/

public boolean compressImage() {

boolean flag = false;

try {

if (inPutFilePath.trim().equals(“”)) {

throw new NullPointerException(“源文件夹路径不存在。”);

}

if (inPutFileName.trim().equals(“”)) {

throw new NullPointerException(“图片文件路径不存在。”);

}

if (outPutFilePath.trim().equals(“”)) {

throw new NullPointerException(“目标文件夹路径地址为空。”);

} else {

if (!ZIPImage.mddir(outPutFilePath)) {

throw new FileNotFoundException(outPutFilePath

+ ” 文件夹创建失败!”);

}

}

if (this.autoBuildFileName) { // 自动生成文件名

String tempFile[] = getFileNameAndExtName(inPutFileName);

outPutFileName = tempFile[0] + “_cyw.” + tempFile[1];

compressPic();

} else {

if (outPutFileName.trim().equals(“”)) {

throw new NullPointerException(“目标文件名为空。”);

}

compressPic();

}

} catch (Exception e) {

flag = false;

e.printStackTrace();

return flag;

}

return flag;

}

// 图片处理

private void compressPic() throws Exception {

try {

// 获得源文件

file = new File(inPutFilePath + inPutFileName);

if (!file.exists()) {

throw new FileNotFoundException(inPutFilePath + inPutFileName

+ ” 文件不存在!”);

}

Image img = ImageIO.read(file);

// 判断图片格式是否正确

if (img.getWidth(null) == -1) {

throw new Exception(“文件不可读!”);

} else {

int newWidth;

int newHeight;

// 判断是否是等比缩放

if (ZIPImage.isScaleZoom == true) {

// 为等比缩放计算输出的图片宽度及高度

double rate1 = ((double) img.getWidth(null))

/ (double) outPutFileWidth + 0.1;

double rate2 = ((double) img.getHeight(null))

/ (double) outPutFileHeight + 0.1;

// 根据缩放比率大的进行缩放控制

double rate = rate1 rate2 ? rate1 : rate2;

newWidth = (int) (((double) img.getWidth(null)) / rate);

newHeight = (int) (((double) img.getHeight(null)) / rate);

} else {

newWidth = outPutFileWidth; // 输出的图片宽度

newHeight = outPutFileHeight; // 输出的图片高度

}

BufferedImage tag = new BufferedImage((int) newWidth,

(int) newHeight, BufferedImage.TYPE_INT_RGB);

/*

* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢

*/

tag.getGraphics().drawImage(

img.getScaledInstance(newWidth, newHeight,

Image.SCALE_SMOOTH), 0, 0, null);

FileOutputStream out = new FileOutputStream(outPutFilePath

+ outPutFileName);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(tag);

out.close();

}

} catch (IOException ex) {

ex.printStackTrace();

}

}

/**

* 创建文件夹目录

* @param filePath

* @return

* @throws Exception

*/

@SuppressWarnings(“unused”)

private static boolean mddir(String filePath) throws Exception {

boolean flag = false;

File f = new File(filePath);

if (!f.exists()) {

flag = f.mkdirs();

} else {

flag = true;

}

return flag;

}

/**

* 获得文件名和扩展名

* @param fullFileName

* @return

* @throws Exception

*/

private String[] getFileNameAndExtName(String fullFileName)

throws Exception {

String[] fileNames = new String[2];

if (fullFileName.indexOf(“.”) == -1) {

throw new Exception(“源文件名不正确!”);

} else {

fileNames[0] = fullFileName.substring(0, fullFileName

.lastIndexOf(“.”));

fileNames[1] = fullFileName

.substring(fullFileName.lastIndexOf(“.”) + 1);

}

return fileNames;

}

public Image getSourceImage() throws IOException{

//获得源文件

file = new File(inPutFilePath + inPutFileName);

if (!file.exists()) {

throw new FileNotFoundException(inPutFilePath + inPutFileName

+ ” 文件不存在!”);

}

Image img = ImageIO.read(file);

return img;

}

/*

* 获得图片大小 

* @path :图片路径

*/

public long getPicSize(String path) {

File file = new File(path);

return file.length();

}

}

//下面是测试程序

package com.sun.util.cyw;

import java.awt.Image;

import java.io.IOException;

public class ImageTest {

public static void main(String[] args) throws IOException {

ZIPImage zip=new ZIPImage(“d:\\”,”1.jpg”,”d:\\test\\”,”处理后的图片.jpg”,false);

zip.setOutPutFileWidth(1000);

zip.setOutPutFileHeight(1000);

Image image=zip.getSourceImage();

long size=zip.getPicSize(“d:\\1.jpg”);

System.out.println(“处理前的图片大小 width:”+image.getWidth(null));

System.out.println(“处理前的图片大小 height:”+image.getHeight(null));

System.out.println(“处理前的图片容量:”+ size +” bit”);

zip.compressImage();

}

}

原创文章,作者:VKOB,如若转载,请注明出处:https://www.506064.com/n/145921.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
VKOBVKOB
上一篇 2024-10-29 18:59
下一篇 2024-10-29 18:59

相关推荐

  • Python基础教程第三版PDF下载

    熟练掌握Python编程语言可以让你轻松地用代码解决很多问题,Python基础教程第三版是一本适合初学者的Python教程。本文将从几个方面详细介绍Python基础教程第三版PDF…

    编程 2025-04-29
  • 使用Spire.PDF进行PDF文档处理

    Spire.PDF是一款C#的PDF库,它可以帮助开发者快速、简便地处理PDF文档。本篇文章将会介绍Spire.PDF库的一些基本用法和常见功能。 一、PDF文档创建 创建PDF文…

    编程 2025-04-29
  • Python零基础PDF下载

    本文将为大家介绍如何使用Python下载PDF文件,适合初学者上手实践。 一、安装必要的库 在Python中,我们需要使用urllib和requests库来获取PDF文件的链接,并…

    编程 2025-04-29
  • 智能风控 Python金融风险PDF

    在金融交易领域,风险控制是一项重要任务。智能风控是指通过人工智能技术和算法模型,对金融交易进行风险识别、风险预警、风险控制等操作。Python是一种流行的编程语言,具有方便、易用、…

    编程 2025-04-29
  • Python编程与数据分析应用PDF

    Python编程是一门功能强大的编程语言,其易读易写、可扩展性强等优点使得它在各个领域都有着广泛的应用。而数据分析也是当今各行各业的基本需求,Python语言通过优秀的数据分析库也…

    编程 2025-04-28
  • Python语言设计基础第2版PDF

    Python语言设计基础第2版PDF是一本介绍Python编程语言的经典教材。本篇文章将从多个方面对该教材进行详细的阐述和介绍。 一、基础知识 本教材中介绍了Python编程语言的…

    编程 2025-04-28
  • 文本数据挖掘与Python应用PDF

    本文将介绍如何使用Python进行文本数据挖掘,并将着重介绍如何应用PDF文件进行数据挖掘。 一、Python与文本数据挖掘 Python是一种高级编程语言,具有简单易学、代码可读…

    编程 2025-04-28
  • Python生成PDF文档

    Python是一门广泛使用的高级编程语言,它可以应用于各种领域,包括Web开发、数据分析、人工智能等。在这些领域的应用中,有很多需要生成PDF文档的需求。Python有很多第三方库…

    编程 2025-04-28
  • 使用Python为PDF添加书签

    Python是一种强大灵活的编程语言,它支持大量的库和模块,其中就包括pdf模块。使用Python处理PDF文件可以有效地提高处理效率和减轻工作量。其中,添加书签是PDF处理的常见…

    编程 2025-04-28
  • 电子琴入门教程pdf下载

    作为一名电子琴爱好者,了解电子琴的基础知识是必要的,而电子琴入门教程PDF的下载则是学习电子琴知识的好方法。 一、找到可靠的PDF下载网站 在互联网上能够找到很多电子琴入门教程的P…

    编程 2025-04-27

发表回复

登录后才能评论