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

java識別psd文件筆記

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

前言

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

需要預覽一些奇奇怪怪的文件

先記錄下psd文件如何生成預覽圖

例子

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

直接解析psd

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

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


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;

//用來接住unsignedByte饶碘,byte不存作負數(shù)(否則拋異常,說越過顏色范圍)

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

//圖像深度(每個通道的顏色位數(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);

//--------第二部分:色彩模式信息购桑,這部分的長度通常為0----

// int lenOfColorModel = raf.readInt();

intlenOfColorModel =this.mbbi.getInt();

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

// this.raf.seek(lenOfColorModel+this.raf.getFilePointer());//長度信息占4個字節(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提供對ImageIo的增強直接讀取 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字體庫安裝到linux下 否則會報錯

個人建議還是 twelvemonkeys + thumbnailator 讀取反肋、打水印亂七八糟的操作 都可以簡單直接快速處理

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市踏施,隨后出現(xiàn)的幾起案子石蔗,更是在濱河造成了極大的恐慌,老刑警劉巖读规,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件抓督,死亡現(xiàn)場離奇詭異燃少,居然都是意外死亡束亏,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進店門阵具,熙熙樓的掌柜王于貴愁眉苦臉地迎上來碍遍,“玉大人,你說我怎么就攤上這事阳液∨戮矗” “怎么了?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵帘皿,是天一觀的道長东跪。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么虽填? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任丁恭,我火速辦了婚禮,結(jié)果婚禮上斋日,老公的妹妹穿的比我還像新娘牲览。我一直安慰自己,他們只是感情好恶守,可當我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布第献。 她就那樣靜靜地躺著,像睡著了一般兔港。 火紅的嫁衣襯著肌膚如雪庸毫。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天衫樊,我揣著相機與錄音岔绸,去河邊找鬼。 笑死橡伞,一個胖子當著我的面吹牛盒揉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播兑徘,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼刚盈,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了挂脑?” 一聲冷哼從身側(cè)響起藕漱,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎崭闲,沒想到半個月后肋联,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡刁俭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年橄仍,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片牍戚。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡侮繁,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出如孝,到底是詐尸還是另有隱情宪哩,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布第晰,位于F島的核電站锁孟,受9級特大地震影響彬祖,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜品抽,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一涧至、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧桑包,春花似錦南蓬、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至弱左,卻和暖如春窄陡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背拆火。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工跳夭, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人们镜。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓币叹,卻偏偏與公主長得像,于是被迫代替她去往敵國和親模狭。 傳聞我的和親對象是個殘疾皇子颈抚,可洞房花燭夜當晚...
    茶點故事閱讀 44,927評論 2 355