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

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

前言

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

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

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

例子

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

直接解析psd

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

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

~~~Java


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

? ? ? ? ? ? }

? ? ? ? }

? ? }

}

~~~

~~~Java

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

~~~pom

<!-- 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

~~~


~~~java

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

~~~pom


AsposeJavaAPI

Aspose Java API

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

贵涵。。珊蟀。远寸。。

com.aspose

aspose-psd

20.9

jdk16


~~~

~~~java

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");

//按需選擇不同類(lèi)型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)聲明

ming創(chuàng)作并維護(hù),博客采用CC協(xié)議拣度。

本文首發(fā)于ming?博客(?https://blog.xujiuming.com?),版權(quán)所有螃壤,轉(zhuǎn)載請(qǐng)注明出處抗果!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市奸晴,隨后出現(xiàn)的幾起案子冤馏,更是在濱河造成了極大的恐慌,老刑警劉巖寄啼,帶你破解...
    沈念sama閱讀 218,204評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件逮光,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡墩划,警方通過(guò)查閱死者的電腦和手機(jī)涕刚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)乙帮,“玉大人杜漠,你說(shuō)我怎么就攤上這事〔炀唬” “怎么了驾茴?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,548評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)氢卡。 經(jīng)常有香客問(wèn)我锈至,道長(zhǎng),這世上最難降的妖魔是什么译秦? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,657評(píng)論 1 293
  • 正文 為了忘掉前任峡捡,我火速辦了婚禮击碗,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘棋返。我一直安慰自己延都,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布睛竣。 她就那樣靜靜地躺著晰房,像睡著了一般。 火紅的嫁衣襯著肌膚如雪射沟。 梳的紋絲不亂的頭發(fā)上殊者,一...
    開(kāi)封第一講書(shū)人閱讀 51,554評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音验夯,去河邊找鬼猖吴。 笑死,一個(gè)胖子當(dāng)著我的面吹牛挥转,可吹牛的內(nèi)容都是我干的海蔽。 我是一名探鬼主播,決...
    沈念sama閱讀 40,302評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼绑谣,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼党窜!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起借宵,我...
    開(kāi)封第一講書(shū)人閱讀 39,216評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤幌衣,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后壤玫,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體豁护,經(jīng)...
    沈念sama閱讀 45,661評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評(píng)論 3 336
  • 正文 我和宋清朗相戀三年欲间,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了楚里。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,977評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡猎贴,死狀恐怖腻豌,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情嘱能,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評(píng)論 5 347
  • 正文 年R本政府宣布虱疏,位于F島的核電站惹骂,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏做瞪。R本人自食惡果不足惜对粪,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評(píng)論 3 330
  • 文/蒙蒙 一右冻、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧著拭,春花似錦纱扭、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,898評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至鄙币,卻和暖如春肃叶,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背十嘿。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,019評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工因惭, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人绩衷。 一個(gè)月前我還...
    沈念sama閱讀 48,138評(píng)論 3 370
  • 正文 我出身青樓蹦魔,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親咳燕。 傳聞我的和親對(duì)象是個(gè)殘疾皇子勿决,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容