原文:https://blog.xujiuming.com/ming/94809d83.html

java識(shí)別psd文件筆記

發(fā)表于?2021-04-09?|?更新于?2021-10-11?|?分類于?筆記?|?評(píng)論數(shù):?0?|?閱讀次數(shù):

前言

最近在做網(wǎng)盤的一些需求

需要預(yù)覽一些奇奇怪怪的文件

先記錄下psd文件如何生成預(yù)覽圖

例子

由于jdk的ImageIO相關(guān)class 無(wú)法直接解析psd 所以要另尋方案

直接解析psd

參考文檔:?https://blog.csdn.net/WASONE_WU/article/details/27695723

直接按照psd文件格式進(jìn)行解析 具體的參考文檔博文


importjava.awt.image.BufferedImage;

importjava.io.File;

importjava.io.FileNotFoundException;

importjava.io.IOException;

importjava.io.RandomAccessFile;

importjava.nio.MappedByteBuffer;

importjava.nio.channels.FileChannel;

publicclassPsdReader{

privateBufferedImage img =null;

privateint[] pixels;

privateRandomAccessFile raf;

privateint[] byteArray;

//用來(lái)接住unsignedByte,byte不存作負(fù)數(shù)(否則拋異常,說(shuō)越過(guò)顏色范圍)

privateint[][][] channelColor;

privateint[][] numOfBytePerLine;

// private final static int RED = 0;

// private final static int GREEN = 1;

// private final static int BLUE = 2;

privateshortnumOfChannel;

privateintheight;

privateintwidth;

privateshortisRle;

privateMappedByteBuffer mbbi;

publicPsdReader(File file){

FileChannel fc =null;

try{

this.raf =newRandomAccessFile(file,"r");

? ? ? ? ? ? fc = raf.getChannel();

longsize = fc.size();

this.mbbi = fc.map(FileChannel.MapMode.READ_ONLY,0, size);

}catch(FileNotFoundException e) {

? ? ? ? ? ? e.printStackTrace();

}catch(IOException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

this.readFile();

img =newBufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

pixels =newint[width*height];

this.initPixels(pixels);

this.setRGB(img,0,0, width, height, pixels);

try{

? ? ? ? ? ? fc.close();

this.raf.close();

}catch(IOException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }

publicBufferedImagegetImg(){

returnimg;

? ? }

privatevoidinitPixels(int[] pixels){

intindex =0;

inta =255;

for(inth=0; h

for(intw=0; w

intr =this.channelColor[0][h][w];

intg =this.channelColor[1][h][w];

intb =this.channelColor[2][h][w];

if(this.numOfChannel>3) {

a =this.channelColor[3][h][w];

? ? ? ? ? ? ? ? }

pixels[index] = (a<<24) | (r<<16)

| (g<<8) | b;

? ? ? ? ? ? ? ? index++;

? ? ? ? ? ? }

? ? ? ? }

? ? }

privatevoidsetRGB( BufferedImage image,intx,inty,intwidth,intheight,int[] pixels ){

inttype = image.getType();

if( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )

? ? ? ? ? ? image.getRaster().setDataElements( x, y, width, height, pixels );

else

image.setRGB( x, y, width, height, pixels,0, width );

? ? }

privatevoidreadFile(){

try{

//-------第一部分:文件頭------------------

//通道數(shù)量

// this.raf.seek(0x0c);

this.mbbi.position(0x0c);

// numOfChannel = this.raf.readShort();

numOfChannel =this.mbbi.getShort();

//System.out.println("numOfChannel="+numOfChannel);

//圖像高度

// height = this.raf.readInt();

height =this.mbbi.getInt();

//System.out.println("height="+height);

//圖像寬度

// width = this.raf.readInt();

width =this.mbbi.getInt();

//System.out.println("width="+width);

//圖像深度(每個(gè)通道的顏色位數(shù))

// short depth = this.raf.readShort();

shortdepth =this.mbbi.getShort();

//System.out.println("depth="+depth);

//是rgb模式則type=3

// short type = this.raf.readShort();

shorttype =this.mbbi.getShort();

//System.out.println("type="+type);

//--------第二部分:色彩模式信息,這部分的長(zhǎng)度通常為0----

// int lenOfColorModel = raf.readInt();

intlenOfColorModel =this.mbbi.getInt();

//System.out.println("lenOfColorModel="+lenOfColorModel);

// this.raf.seek(lenOfColorModel+this.raf.getFilePointer());//長(zhǎng)度信息占4個(gè)字節(jié),但是不用加,下同

this.mbbi.position(lenOfColorModel+this.mbbi.position());

//--------第三部分:圖像資源數(shù)據(jù)------------------

// int lenOfImageResourceBlock = raf.readInt();

intlenOfImageResourceBlock =this.mbbi.getInt();

//System.out.println("lenOfImageResourceBlock="+lenOfImageResourceBlock);

// this.raf.seek(lenOfImageResourceBlock+this.raf.getFilePointer());

this.mbbi.position(lenOfImageResourceBlock+this.mbbi.position());

//--------第四部分:圖層與蒙版信息----------------

// int lenOfLayerInfo = raf.readInt();

intlenOfLayerInfo =this.mbbi.getInt();

//System.out.println("lenOfLayer="+lenOfLayerInfo);

// this.raf.seek(lenOfLayerInfo+raf.getFilePointer());

this.mbbi.position(lenOfLayerInfo+this.mbbi.position());

//--------第五部分:圖像數(shù)據(jù)--------------------

// isRle = raf.readShort();

isRle =this.mbbi.getShort();

//System.out.println("isRle="+isRle);

// //System.out.println("nowPosition="+this.raf.getFilePointer());

//System.out.println("nowPosition="+this.mbbi.position());

}catch(Exception e1) {

? ? ? ? ? ? e1.printStackTrace();

? ? ? ? }

this.channelColor =newint[numOfChannel][height][width];

if(isRle==1){

this.numOfBytePerLine =newint[numOfChannel][height];

for(inti=0; i

for(intj=0; j

try{

//TODO

// this.numOfBytePerLine[i][j] = this.raf.readUnsignedShort();

intti =this.mbbi.getShort();

if(ti<0) { ti +=65536; }

this.numOfBytePerLine[i][j] = ti;

}catch(Exception e) {

? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

for(intc=0; c

for(inth=0; h

this.unpackbits(numOfBytePerLine[c][h],channelColor[c][h]);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

}elseif(isRle==0) {

for(intc=0; c

for(inth=0; h

for(intw=0; w

try{

// this.channelColor[c][h][w] = this.raf.readUnsignedByte();

intti =this.mbbi.get();

if(ti<0) { ti +=256; }

this.channelColor[c][h][w] = ti;

}catch(Exception e) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? }

privatevoidunpackbits(intlenOfInput,int[] channelColor){

shortn =0;

intlast =0;

while(lenOfInput>0){

try{

// n = raf.readByte();

n =this.mbbi.get();

? ? ? ? ? ? ? ? lenOfInput--;

}catch(Exception e) {

? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? }

if(0<=n && n<=127) {

intrepeatTime = n;

? ? ? ? ? ? ? ? ++repeatTime;

for(intt=0; t

try{

// channelColor[last+t] = raf.readUnsignedByte();

intti =this.mbbi.get();

if(ti<0) { ti +=256; }

? ? ? ? ? ? ? ? ? ? ? ? channelColor[last+t] = ti;

? ? ? ? ? ? ? ? ? ? ? ? lenOfInput--;

}catch(Exception e) {

? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? last += repeatTime;

? ? ? ? ? ? }

elseif(-1>=n && n>=-127) {

intval =0;

intrepeatTime = -n;

? ? ? ? ? ? ? ? ++repeatTime;

try{

// val = raf.readUnsignedByte();

intti =this.mbbi.get();

if(ti<0) { ti +=256; }

? ? ? ? ? ? ? ? ? ? val = ti;

//System.out.println(val);

? ? ? ? ? ? ? ? ? ? lenOfInput--;

}catch(Exception e) {

? ? ? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? ? ? }

for(intt=0; t

? ? ? ? ? ? ? ? ? ? channelColor[last+t] = val;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? last += repeatTime;

? ? ? ? ? ? }

elseif(n==-128) {

//noop

? ? ? ? ? ? }

? ? ? ? }

? ? }

}


importnet.coobird.thumbnailator.Thumbnails;

importjavax.imageio.ImageIO;

importjava.awt.image.BufferedImage;

importjava.io.File;

importjava.io.IOException;

publicclassmain{

publicstaticvoidmain(String[] args)throwsIOException{

longnow = System.currentTimeMillis();

PsdReader psdReader =newPsdReader(newFile("02.psd"));

? ? ? ? BufferedImage bufferedImage = psdReader.getImg();

ImageIO.write(bufferedImage,"png",newFile("02.png"));

? ? ? ? System.out.println((System.currentTimeMillis() - now));

? ? }

}

twelvemonkeys + thumbnailator

參考文檔:

https://www.cnblogs.com/interdrp/p/7076202.html

https://github.com/haraldk/TwelveMonkeys

https://github.com/coobird/thumbnailator

利用twelevemonkeys提供對(duì)ImageIo的增強(qiáng)直接讀取 psd


<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->

net.coobird

thumbnailator

0.4.14

com.twelvemonkeys.imageio

imageio-core

3.6.4

<!-- https://mvnrepository.com/artifact/com.twelvemonkeys.imageio/imageio-psd -->

com.twelvemonkeys.imageio

imageio-psd

3.6.4


importnet.coobird.thumbnailator.Thumbnails;

importjava.io.File;

importjava.io.IOException;

publicclassmain{

publicstaticvoidmain(String[] args){

longnow = System.currentTimeMillis();

try{

Thumbnails.of(newFile("03.psd"))

.size(1080,720)

.toFile(newFile("03.png"));

}catch(IOException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? System.out.println((System.currentTimeMillis() - now));

? ? }

}

Aspose.psd for java

參考文檔:?https://downloads.aspose.com/psd/java


AsposeJavaAPI

Aspose Java API

http://repository.aspose.com/repo/

。。。。。

com.aspose

aspose-psd

20.9

jdk16


importnet.coobird.thumbnailator.Thumbnails;

importjava.io.File;

importjava.io.IOException;

publicclassmain{

publicstaticvoidmain(String[] args){

longnow = System.currentTimeMillis();

// Load image

Image img = Image.load("03.psd");

//按需選擇不同類型options 例如 png pdf jpeg等等

PngOptions options =newPngOptions();

// Convert PSD to png

img.save("03.png", options);

? ? ? ? System.out.println((System.currentTimeMillis() - now));

? ? }

}

總結(jié)

如果要直接粗暴讀取 直接按照psd 格式硬讀取即可

如果有其他的一些需求 建議 twelvemonkeys + thumbnailator組合 不僅僅是讀取psd 還可以做一些奇奇怪怪的事情

Aspose.psd for java方案的話 如果是部署在windows下 可以使用 如果要部署linux下 需要將windows字體庫(kù)安裝到linux下 否則會(huì)報(bào)錯(cuò)

個(gè)人建議還是 twelvemonkeys + thumbnailator 讀取、打水印亂七八糟的操作 都可以簡(jiǎn)單直接快速處理

------ 本文結(jié)束 ------

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容