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/zh-tw/n/145921.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
VKOB的頭像VKOB
上一篇 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

發表回復

登錄後才能評論