Windowsで写真を適切なサイズに圧縮する

お正月の休暇中に私に起こった1つのITの静かな話をHabraPeopleと共有したいと思います。 私の新年のプレゼントの1つは、DICOMのデジタルフォトフレームでした(ちなみに、まともなものです)。 128 MBの内部メモリと、写真をアップロードするための便利なインターフェイス(別名フラッシュドライブ)が含まれています。 しかし、これは問題ありません-コンピューターのホームアーカイブでは写真はフルサイズで保存されますが、USBフラッシュドライブでは小さなサイズに圧縮され、触れたユーザーに表示されます。 どういうわけか無駄に対処する必要があります-ペンですべての写真を絞るのは面倒なことです!



ここで、私はWindowsを実際に使用していないことに注意してください。Linuxはより馴染み深いので...そして、そこでimagemagick、はい、bashを使用し、スクリプトを作成します。 しかし、Windowsの状況は私を考えさせました。



もちろん、特定のソフトウェアは確かにありますが、プログラマの魂は体系的なアプローチを必要とします)一般に、Windowsでそのような問題をどのように解決できるか疑問に思っていました。 私はすぐにWindowsシェルの使用を拒否しました(理由は聞かないでください)。 私はJavaでprogkuを書くことにしました。 実際には、これが行われました-フォルダーが入力に送られ、目的の拡張子を持つすべてのファイルが再帰的に検索されます(圧縮してスケーリングする必要があるかどうか)。

コードは次のとおりです(私はJavaが初めてなので、scるな、しかし建設的な批判は非常に興味深い)。



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

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

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

import javax.swing.*;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.util.LinkedList;



public class ImageConverter {



static int MAX_WIDTH = 480;

static int MAX_HEIGHT = 360;



public static void main(String[] args) {

String indir = "" ;

if (args.length > 0){

indir = args[0];

} else {

System. out .println( "Usage: imageconv path [maxwidth] [maxheight]" );

return ;

}

if (args.length > 2){

MAX_WIDTH = Integer.parseInt(args[1]);

MAX_HEIGHT = Integer.parseInt(args[2]);

}

System. out .println( "Input directory: " + indir);

LinkedList<File> files = getTree( new File(indir));

if (files.size() == 0){

JOptionPane.showMessageDialog( null , "Nothing to process" ,

"Image Convert Error" ,JOptionPane.ERROR_MESSAGE);

return ;

}

int startSize = 0, endSize = 0;

int res = JOptionPane.showConfirmDialog( null , "Will process " + files.size() + " files on drive " + indir,

"Image Convert" ,JOptionPane.OK_CANCEL_OPTION);

if (res == JOptionPane.CANCEL_OPTION) return ;

for (File original: files){

startSize += original.length();

System. out .print( "Will process " + original.getName() + "..." );

try {

if (resize(original, original)){

System. out .println( "Done!" );

} else {

System. out .println( "No conversion." );

}

} catch (Exception e){

System. out .println( " Error: " + e.getMessage());

}

}

for (File result: files){

endSize += result.length();

}

System. out .println( "Result is: start size " + startSize + ", end size is "

+ endSize);

System. out .println( "Comperession is " + (100.0*(startSize - endSize) / startSize) + "%." );



return ;

}

public static LinkedList<File> getTree(File dir){

LinkedList<File> result = new LinkedList<File>();

if (dir.isFile()){

String fname = dir.getName();

String extension = fname;

if (fname.length()>3)

extension = fname.substring(fname.length() - 3).toLowerCase();

if ( "jpg" .equals(extension)){

result.add(dir);

}

return result;

}

File[] children = dir.listFiles();

if (children == null ) return result;

for (File s: children){

result.addAll(getTree(s));

}

return result;

}



public static boolean resize(File originalFile, File resizedFile) throws IOException {



ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());

Image i = ii.getImage();

Image resizedImage = null ;



int newWidth = i.getWidth( null );

int newHeight = i.getHeight( null );



double aspectW = 1.0 * newWidth / ImageConverter.MAX_WIDTH;

double aspectH = 1.0 * newHeight / ImageConverter.MAX_HEIGHT;

System. out .println( "AspectW: " + aspectW + "; AspectH: " + aspectH);

if (aspectW > 1.0 || aspectH > 1.0){

if (aspectW >= aspectH){ // resize by width

newHeight = new Double(newHeight / aspectW).intValue();

newWidth = ImageConverter.MAX_WIDTH;

} else { //resize by height

newWidth = new Double(newWidth / aspectH).intValue();

newHeight = ImageConverter.MAX_HEIGHT;

}



resizedImage = i.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);

Image temp = new ImageIcon(resizedImage).getImage();



BufferedImage bufferedImage = new BufferedImage(temp.getWidth( null ), temp.getHeight( null ),

BufferedImage.TYPE_INT_RGB);

Graphics g = bufferedImage.createGraphics();

g.setColor(Color.white);

g.fillRect(0, 0, temp.getWidth( null ), temp.getHeight( null ));

g.drawImage(temp, 0, 0, null );

g.dispose();



FileOutputStream out = new FileOutputStream(resizedFile);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( out );

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);

param.setQuality(1.0f, true );

encoder.setJPEGEncodeParam(param);

encoder.encode(bufferedImage);

return true ;

} else { ///nothing to do

return false ;

}

}

}



* This source code was highlighted with Source Code Highlighter .









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

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

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

import javax.swing.*;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.util.LinkedList;



public class ImageConverter {



static int MAX_WIDTH = 480;

static int MAX_HEIGHT = 360;



public static void main(String[] args) {

String indir = "" ;

if (args.length > 0){

indir = args[0];

} else {

System. out .println( "Usage: imageconv path [maxwidth] [maxheight]" );

return ;

}

if (args.length > 2){

MAX_WIDTH = Integer.parseInt(args[1]);

MAX_HEIGHT = Integer.parseInt(args[2]);

}

System. out .println( "Input directory: " + indir);

LinkedList<File> files = getTree( new File(indir));

if (files.size() == 0){

JOptionPane.showMessageDialog( null , "Nothing to process" ,

"Image Convert Error" ,JOptionPane.ERROR_MESSAGE);

return ;

}

int startSize = 0, endSize = 0;

int res = JOptionPane.showConfirmDialog( null , "Will process " + files.size() + " files on drive " + indir,

"Image Convert" ,JOptionPane.OK_CANCEL_OPTION);

if (res == JOptionPane.CANCEL_OPTION) return ;

for (File original: files){

startSize += original.length();

System. out .print( "Will process " + original.getName() + "..." );

try {

if (resize(original, original)){

System. out .println( "Done!" );

} else {

System. out .println( "No conversion." );

}

} catch (Exception e){

System. out .println( " Error: " + e.getMessage());

}

}

for (File result: files){

endSize += result.length();

}

System. out .println( "Result is: start size " + startSize + ", end size is "

+ endSize);

System. out .println( "Comperession is " + (100.0*(startSize - endSize) / startSize) + "%." );



return ;

}

public static LinkedList<File> getTree(File dir){

LinkedList<File> result = new LinkedList<File>();

if (dir.isFile()){

String fname = dir.getName();

String extension = fname;

if (fname.length()>3)

extension = fname.substring(fname.length() - 3).toLowerCase();

if ( "jpg" .equals(extension)){

result.add(dir);

}

return result;

}

File[] children = dir.listFiles();

if (children == null ) return result;

for (File s: children){

result.addAll(getTree(s));

}

return result;

}



public static boolean resize(File originalFile, File resizedFile) throws IOException {



ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());

Image i = ii.getImage();

Image resizedImage = null ;



int newWidth = i.getWidth( null );

int newHeight = i.getHeight( null );



double aspectW = 1.0 * newWidth / ImageConverter.MAX_WIDTH;

double aspectH = 1.0 * newHeight / ImageConverter.MAX_HEIGHT;

System. out .println( "AspectW: " + aspectW + "; AspectH: " + aspectH);

if (aspectW > 1.0 || aspectH > 1.0){

if (aspectW >= aspectH){ // resize by width

newHeight = new Double(newHeight / aspectW).intValue();

newWidth = ImageConverter.MAX_WIDTH;

} else { //resize by height

newWidth = new Double(newWidth / aspectH).intValue();

newHeight = ImageConverter.MAX_HEIGHT;

}



resizedImage = i.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);

Image temp = new ImageIcon(resizedImage).getImage();



BufferedImage bufferedImage = new BufferedImage(temp.getWidth( null ), temp.getHeight( null ),

BufferedImage.TYPE_INT_RGB);

Graphics g = bufferedImage.createGraphics();

g.setColor(Color.white);

g.fillRect(0, 0, temp.getWidth( null ), temp.getHeight( null ));

g.drawImage(temp, 0, 0, null );

g.dispose();



FileOutputStream out = new FileOutputStream(resizedFile);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( out );

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);

param.setQuality(1.0f, true );

encoder.setJPEGEncodeParam(param);

encoder.encode(bufferedImage);

return true ;

} else { ///nothing to do

return false ;

}

}

}



* This source code was highlighted with Source Code Highlighter .









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

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

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

import javax.swing.*;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.util.LinkedList;



public class ImageConverter {



static int MAX_WIDTH = 480;

static int MAX_HEIGHT = 360;



public static void main(String[] args) {

String indir = "" ;

if (args.length > 0){

indir = args[0];

} else {

System. out .println( "Usage: imageconv path [maxwidth] [maxheight]" );

return ;

}

if (args.length > 2){

MAX_WIDTH = Integer.parseInt(args[1]);

MAX_HEIGHT = Integer.parseInt(args[2]);

}

System. out .println( "Input directory: " + indir);

LinkedList<File> files = getTree( new File(indir));

if (files.size() == 0){

JOptionPane.showMessageDialog( null , "Nothing to process" ,

"Image Convert Error" ,JOptionPane.ERROR_MESSAGE);

return ;

}

int startSize = 0, endSize = 0;

int res = JOptionPane.showConfirmDialog( null , "Will process " + files.size() + " files on drive " + indir,

"Image Convert" ,JOptionPane.OK_CANCEL_OPTION);

if (res == JOptionPane.CANCEL_OPTION) return ;

for (File original: files){

startSize += original.length();

System. out .print( "Will process " + original.getName() + "..." );

try {

if (resize(original, original)){

System. out .println( "Done!" );

} else {

System. out .println( "No conversion." );

}

} catch (Exception e){

System. out .println( " Error: " + e.getMessage());

}

}

for (File result: files){

endSize += result.length();

}

System. out .println( "Result is: start size " + startSize + ", end size is "

+ endSize);

System. out .println( "Comperession is " + (100.0*(startSize - endSize) / startSize) + "%." );



return ;

}

public static LinkedList<File> getTree(File dir){

LinkedList<File> result = new LinkedList<File>();

if (dir.isFile()){

String fname = dir.getName();

String extension = fname;

if (fname.length()>3)

extension = fname.substring(fname.length() - 3).toLowerCase();

if ( "jpg" .equals(extension)){

result.add(dir);

}

return result;

}

File[] children = dir.listFiles();

if (children == null ) return result;

for (File s: children){

result.addAll(getTree(s));

}

return result;

}



public static boolean resize(File originalFile, File resizedFile) throws IOException {



ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());

Image i = ii.getImage();

Image resizedImage = null ;



int newWidth = i.getWidth( null );

int newHeight = i.getHeight( null );



double aspectW = 1.0 * newWidth / ImageConverter.MAX_WIDTH;

double aspectH = 1.0 * newHeight / ImageConverter.MAX_HEIGHT;

System. out .println( "AspectW: " + aspectW + "; AspectH: " + aspectH);

if (aspectW > 1.0 || aspectH > 1.0){

if (aspectW >= aspectH){ // resize by width

newHeight = new Double(newHeight / aspectW).intValue();

newWidth = ImageConverter.MAX_WIDTH;

} else { //resize by height

newWidth = new Double(newWidth / aspectH).intValue();

newHeight = ImageConverter.MAX_HEIGHT;

}



resizedImage = i.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);

Image temp = new ImageIcon(resizedImage).getImage();



BufferedImage bufferedImage = new BufferedImage(temp.getWidth( null ), temp.getHeight( null ),

BufferedImage.TYPE_INT_RGB);

Graphics g = bufferedImage.createGraphics();

g.setColor(Color.white);

g.fillRect(0, 0, temp.getWidth( null ), temp.getHeight( null ));

g.drawImage(temp, 0, 0, null );

g.dispose();



FileOutputStream out = new FileOutputStream(resizedFile);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( out );

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);

param.setQuality(1.0f, true );

encoder.setJPEGEncodeParam(param);

encoder.encode(bufferedImage);

return true ;

} else { ///nothing to do

return false ;

}

}

}



* This source code was highlighted with Source Code Highlighter .












しかし、このプログラムを実行する方法は? このようなタスクのユーザーインターフェイスを作成するのも無駄です。 Googlingは、このプログラムをデバイスのコンテキストメニューに埋め込むことに決めました。 ただし、印刷のデバッグも見たいです(デバッグ用ではなく、プロセスが動いていることを理解するため)。 私はそうすることに決めました-プログラムのラッパーのようなcmd-file(まだねじシェルがねじ込まれています)。 そのため、jarクラスからファイルを作成し、C:/ Program Files / imgconv / imgconv.jarに配置し、cmdファイルConvertImages.cmdを作成します。

java -jar "C:\Program Files\imgconv\imgconv.jar" %1

pause







(当然、java実行可能ファイルはPATH変数を介してアクセス可能でなければなりません)

ここで、メニュー項目を追加します。 これを行います:



非常に便利でした-開始し、サイズを変更するかどうかを確認し、[OK]をクリックしてプロセスを瞑想します。

PSこの作品は、アドバイスやガイダンスなど、同じようなものではありません。 自由時間を持ちながら、標準的な問題に対する非標準的な解決策を思いついた方法についての話です)



PPS明けましておめでとうございます!



All Articles