iLEDについて

Javaサンプル

Netbeans

Libre Office Basicマクロ

その他


Author of This Site:
M. Kom. (kom9kmail@gmail.com)
Spam対策のため@マークは全角になっていますから、メール送信時には半角にしてください。

以下のような「逆引き」系の本は、パラパラと読みながら、この機能でこんなことができそうだというアイディアを練るのにいいです。

Java Jpeg画像ファイル一括サイズ変換 GUI版

Java Imageクラス使い方でコマンドライン版でのサイズ変換プログラムを 掲載しました。これをGUI化したものを掲載します。例によってNetBeansで作成したものです。
ソースは、メインプログラムのSizeConv.javaと、イメージ処理を担当するImgPrc.javaから成っています。 動作時の画面構成は次のようになります。

fig

SizeConv.java実行時

InputFolderに指定されたフォルダ内の拡張子jpgのファイルはすべてOutput Folderに指定された フォルダ内にサイズ変換されて格納されます。
出力縦サイズのみを指定するようになっており、横サイズは元ファイルの縦横比を維持するように決定されます。 ぼかしをかけられるようになっており、そのレベルを0から5までで設定できます。
コントラスト調整は3乗根関数を使用しています。中心の値(閾値)を指定できます。閾値より明るいピクセルはより明るく、閾値より 暗いピクセルはより暗くなります。傾きの強度を指定できます。全体の明度調整をシフトの値として設定できます。
生成画像の回転をすることができます。90度刻みです。
こうした画像処理関連の多くは、ImgPrcクラスで行う形をとっています。

実行用Jarファイルはこちら。JpgConverter.jar
ソースコードはいつものように自動生成された部分は基本的に省略し、人力で作成した部分を示します。
まずはメインプログラムのSizeConv.javaですが、その前に、GUIの部品の使用状況は次のようになっています。
    private javax.swing.JTextField ContrastShiftPlus;
    private javax.swing.JTextField ContrastThresh;
    private javax.swing.JButton ConvBtn;
    private javax.swing.JTextField InputFolderName;
    private javax.swing.JTextField OutputFolderName;
    private javax.swing.JRadioButton Rotate0;
    private javax.swing.JRadioButton Rotate180;
    private javax.swing.JRadioButton Rotate270;
    private javax.swing.JRadioButton Rotate90;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JList fileList;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JCheckBox jCheckContrast;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JSlider jSliderBokashi;
    private javax.swing.JTextField jTextBokashi;
    private javax.swing.JTextField jTextContrastLevel;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextWidth;

メインのソースコードです。


/**
 *
 * @author Kom.
 */

import java.io.*;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.util.*;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;

public class SizeConv extends javax.swing.JFrame {
  int targetWidth; //画像横幅がこのサイズになるよう拡縮
  int imageWidth; //現在の画像幅
  int imageHeight;
    DefaultListModel model;
    
    ImgPrc imgprc; 
    /**
     * Creates new form SizeConv
     */

 @SuppressWarnings("unchecked")
    public SizeConv() {
        initComponents();
        model = new DefaultListModel();
       
    fileList.setModel(model);
    }

// initComponents()は省略 .......................

   private void ConvBtnActionPerformed(java.awt.event.ActionEvent evt) {  
    //3種類の方法を記述しています。どれでも出来上がりに大差はありません。
    //convert1();
    //convert2();
    convert3();
    } 

  public void convert1(){
    searchFile(InputFolderName.getText());
        String delim="\\";
        String delimOut="\\";
        String swk=InputFolderName.getText();
        int slen=swk.length();
        if(swk.charAt(slen-1) == '\\'){
                delim="";
        }

        swk=OutputFolderName.getText();
        slen=swk.length();
        if(swk.charAt(slen-1) == '\\'){
            delimOut="";
        }
        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        for (int i = 0; i < model.getSize(); i++) {
            try{
                BufferedImage image = ImageIO.read(
            new File(InputFolderName.getText()+delim+model.getElementAt(i))
          );
        imgprc= new ImgPrc(image);
        //Rotate
        if(Rotate90.isSelected()){
          imgprc.turnImage(1);
        }else if(Rotate270.isSelected()){
          imgprc.turnImage(2);
        }else if(Rotate180.isSelected()){
          imgprc.turnImage(1);
          imgprc.turnImage(1);
        }

        //Soften
        int bokashival=Integer.parseInt(jTextBokashi.getText());
        if(bokashival!=0){
          for(int bi=0; bi < bokashival; bi++){
            soft(1);
          }
        }
        
        //Contrast
        if(jCheckContrast.isSelected()){
          imgprc.contrastChange(
            Integer.parseInt(ContrastThresh.getText()),
            Double.parseDouble(jTextContrastLevel.getText()),
            Integer.parseInt(ContrastShiftPlus.getText())
          );
        }
        //ImageをBufferedImageクラスに変換
        //Imageオブジェクトからピクセルを抽出できるようにする
                PixelGrabber pixelGrabber = new PixelGrabber(
        imgprc.getCurrentImage(),
          0,
          0,
          -1,
          -1,
          false
        );
        pixelGrabber.grabPixels();
        ColorModel cm = pixelGrabber.getColorModel();
        final int w = pixelGrabber.getWidth();
        final int h = pixelGrabber.getHeight();
        WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
        BufferedImage renderedImage = new BufferedImage(
          cm,
          raster,
          cm.isAlphaPremultiplied(),
          new Hashtable()
        );
        renderedImage.getRaster().setDataElements(
          0,
          0,
          w,
          h,
          pixelGrabber.getPixels()
        );
        image = convertToINTRGB(resizeImage(renderedImage));
        float qt=Float.valueOf(jSlider1.getValue())/100f;
        writeImage(
          image,
          OutputFolderName.getText()+delimOut+model.getElementAt(i),
          qt
        );
      }catch(Exception e){
        this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR ));
        System.out.println(e);
      }
    }
    this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR ));
  }

  public void convert2(){
    searchFile(InputFolderName.getText());
    String delim="\\";
    String delimOut="\\";
    ImageWriter writer = null;
    ImageOutputStream imageStream; 
    String swk=InputFolderName.getText();
    int slen=swk.length();
    if(swk.charAt(slen-1) == '\\'){
        delim="";
    }

    swk=OutputFolderName.getText();
    slen=swk.length();
    if(swk.charAt(slen-1) == '\\'){
        delimOut="";
    }
    int targetWidth=Integer.parseInt(jTextWidth.getText());
    this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    for (int i = 0; i < model.getSize(); i++) {
      try {
        BufferedImage orgImage = null;
        orgImage = ImageIO.read(new File(
            InputFolderName.getText()+delim+model.getElementAt(i))
          );

        int width = orgImage.getWidth();
        int height = orgImage.getHeight();
        int targetHeight; 

        targetHeight = (int)(((double)targetWidth / width) * (double)height);
        BufferedImage resizeImage=
          new BufferedImage(
            targetWidth,
            targetHeight,
            BufferedImage.TYPE_3BYTE_BGR
          );
        AffineTransformOp ato = null;
        ato = new AffineTransformOp(
          AffineTransform.getScaleInstance((double)(targetWidth) / orgImage.getWidth(),
          (double)targetHeight / orgImage.getHeight()), null);
        ato.filter(orgImage, resizeImage);
        resizeImage.getGraphics().drawImage(
          orgImage.getScaledInstance(
            targetWidth,
            targetHeight,
            Image.SCALE_AREA_AVERAGING
          ),
          0,
          0,
          targetWidth,
          targetHeight,
          null
        );

        imageStream =
          ImageIO.createImageOutputStream(
            new File(OutputFolderName.getText()+delimOut+model.getElementAt(i))
          );
        writer = null;
        Iterator it = (Iterator) ImageIO.getImageWritersByFormatName("jpg");
        writer = (ImageWriter) it.next();
        writer.setOutput(imageStream);
        JPEGImageWriteParam jpgWriter = new JPEGImageWriteParam(Locale.getDefault());
        jpgWriter.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        float qt=Float.valueOf(jSlider1.getValue())/100f;
        jpgWriter.setCompressionQuality(qt);
        writer.write(null, new IIOImage(resizeImage, null, null), jpgWriter);
        imageStream.flush();
        writer.dispose();
        imageStream.close();
      }catch (Exception e) {
        this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR ));
        System.out.println(e);
      }
    }
    this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR ));
  }

  public void convert3(){
    int targetWidth=Integer.parseInt(jTextWidth.getText());
    searchFile(InputFolderName.getText());
        String delim="\\";
        String delimOut="\\";
        String swk=InputFolderName.getText();
    int targetHeight;
        int slen=swk.length();
    double mag;
        if(swk.charAt(slen-1) == '\\'){
                delim="";
        }

        swk=OutputFolderName.getText();
        slen=swk.length();
        if(swk.charAt(slen-1) == '\\'){
            delimOut="";
        }
        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        for (int i = 0; i < model.getSize(); i++) {
          try{
            BufferedImage wkimage = ImageIO.read(
              new File(InputFolderName.getText()+delim+model.getElementAt(i))
            );
            imgprc= new ImgPrc(wkimage);
            targetHeight=imgprc.getImageHeight();
            //Rotate
            if(Rotate90.isSelected()){
              imgprc.turnImage(1);
            }else if(Rotate270.isSelected()){
              imgprc.turnImage(2);
            }else if(Rotate180.isSelected()){
              imgprc.turnImage(1);
              imgprc.turnImage(1);
            }

            //Soften
            int bokashival=Integer.parseInt(jTextBokashi.getText());
            if(bokashival!=0){
              for(int bi=0; bokashival>bi; bi++){
                soft(1);
              }
            }
        
        //Contrast
            if(jCheckContrast.isSelected()){
              imgprc.contrastChange(
                Integer.parseInt(ContrastThresh.getText()),
                Double.parseDouble(jTextContrastLevel.getText()),
                Integer.parseInt(ContrastShiftPlus.getText())
              );
            }
            BufferedImage resizedImage = null;
            targetWidth=Integer.parseInt(jTextWidth.getText());
            mag = ((double)targetWidth / imgprc.imageWidth);
            targetHeight = (int)(imgprc.getImageHeight() * mag);

            imgprc.scaleImage(targetWidth, targetHeight); 
            resizedImage = imgprc.getBufferedImage();
  
            ImageOutputStream imageStream =
              ImageIO.createImageOutputStream(
                new File(OutputFolderName.getText()+delimOut+model.getElementAt(i))
              );        
            JPEGImageWriteParam param = new JPEGImageWriteParam(Locale.getDefault());
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            float qt=Float.valueOf(jSlider1.getValue())/100f;
            param.setCompressionQuality(qt);
            ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
            writer.setOutput(imageStream);
            writer.write(null, new IIOImage(resizedImage, null, null), param);
            imageStream.flush();
            writer.dispose();
            imageStream.close();
          }catch(Exception e){
            this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR ));
            System.out.println(e);
          }
        }
        this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR ));
  }
  
  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    readSansho ();
  }                                        

  private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { 
    writeSansho();
  }                                        

  private void jSlider1MouseDragged(java.awt.event.MouseEvent evt) {
    jTextField3.setText(Integer.toString(jSlider1.getValue()));
  }                                     

  private void jSliderBokashiMouseDragged(java.awt.event.MouseEvent evt) { 
    // ぼかしスライダーイベント
    jTextBokashi.setText(Integer.toString(jSliderBokashi.getValue()));
  }                                           

  @SuppressWarnings("unchecked")
    public void searchFile(String dirname){
        try{
      String ext=jTextField2.getText();
            File file = new File(dirname);
            File[] files = file.listFiles();
      //System.out.println(ext.toLowerCase());
            model.clear();
      for (int i = 0; i < files.length; i++) {
                File wkfile = files[i];
      // ファイル名(全て小文字に変換)
        String lowname = wkfile.getName().toLowerCase();
        // 拡張子「.jpg」のファイルなら表示
        if(lowname.endsWith(ext.toLowerCase()) && 
          wkfile.isFile() && wkfile.canRead() ){
            //System.out.println(wkfile.getName());
            model.addElement(wkfile.getName());
          }
        }
        } catch (Exception ex) {
      System.out.println(ex);
    }
    }
  
  public static void outputImage(String filename, BufferedImage image) throws IOException {
    JPEGImageWriteParam param = new JPEGImageWriteParam(Locale.getDefault());
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionQuality(0.95f);

    ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
    writer.setOutput(ImageIO.createImageOutputStream(new File(filename)));
    writer.write(null, new IIOImage(image, null, null), param);
    writer.dispose();
  }
  
  public void writeSansho() {
    String delim="\\";  
    //JFileChooser fc=new JFileChooser(System.getProperty("user.dir"));
    JFileChooser fc=new JFileChooser(OutputFolderName.getText());
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    //ファイル選択ダイアログを表示する。選択されたかどうかはfdに
    //返される。
    int fd=fc.showOpenDialog(this);
    try{
      if(fd==JFileChooser.APPROVE_OPTION){
        String swk=fc.getCurrentDirectory().getAbsolutePath();
        int slen=swk.length();
        if(swk.charAt(slen-1) == '\\'){
          delim="";
        }
        String s01=fc.getCurrentDirectory().getAbsolutePath()+delim+fc.getSelectedFile().getName();
        OutputFolderName.setText(s01);
      }
    } catch(Exception ex){System.out.println(ex);}
  }

  public void readSansho() {
    String delim="\\";  
    JFileChooser fc=new JFileChooser(InputFolderName.getText());
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int fd=fc.showOpenDialog(this);
    try{
      if(fd==JFileChooser.APPROVE_OPTION){
        String swk=fc.getCurrentDirectory().getAbsolutePath();
        int slen=swk.length();
        if(swk.charAt(slen-1) == '\\'){
          delim="";
        }
        String s01=fc.getCurrentDirectory().getAbsolutePath()+
            delim+
            fc.getSelectedFile().getName();
        InputFolderName.setText(s01);
      }
    } catch(Exception ex){System.out.println(ex);}
    model.clear();
    searchFile(InputFolderName.getText());
  }
    
  public BufferedImage resizeImage (BufferedImage image) {  
    double mag = 0.0;
    imageWidth = image.getWidth();
    imageHeight = image.getHeight();
    BufferedImage resizedImage = null;
    targetWidth=Integer.parseInt(jTextWidth.getText());
    mag = ((double)targetWidth / imageWidth);
    imageHeight = (int)(imageHeight * mag);
    imageWidth  = (int)(imageWidth * mag);

    resizedImage = new BufferedImage(imageWidth, imageHeight, image.getType());
    resizedImage.getGraphics().drawImage(
      image.getScaledInstance(
        imageWidth,
        imageHeight,
        Image.SCALE_AREA_AVERAGING
      ),
      0,
      0,
      imageWidth,
      imageHeight,
      null
    );
    return resizedImage;
  }

  public void writeImage(BufferedImage image, String filename, float quality) {
    Iterator writers = ImageIO.getImageWritersByFormatName("jpeg");
    if (writers.hasNext()) {
      ImageWriter writer = (ImageWriter)writers.next();
      try {
        ImageOutputStream stream
          = ImageIO.createImageOutputStream(new File(filename));
        writer.setOutput(stream);
        ImageWriteParam param = writer.getDefaultWriteParam();
        if (param.canWriteCompressed()) {
          param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
          param.setCompressionQuality(quality);
        } else {
          System.out.println("Compression is not supported.");
        }
        writer.write(null, new IIOImage(image, null, null), param);
        stream.close();
        writer.dispose();
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }  
  

  //引数imageから、パックされた8ビットRGB色成分のBufferedImageを作成
  public BufferedImage convertToINTRGB (BufferedImage image) {
    BufferedImage convertedImage = new BufferedImage(
      image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
    convertedImage.getGraphics().drawImage(image, 0, 0, null);
    return convertedImage;
  }  

  //ぼかし処理
  void soft(int op) {
    imgprc.softenImage(op);
  }


//main()などを省略。.............

} //End of Class SizeConv

次に画像処理関連クラスImgPrc.javaです。

import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.image.*;
import java.awt.Graphics;
import java.util.*;
import java.awt.geom.*;
import java.awt.Toolkit;
import java.awt.*;
import java.awt.image.*;

public class ImgPrc  {
  private BorderLayout borderLayout1 = new BorderLayout();
  private ImageIcon icon;
  public Image image;
  private Dimension size;
    private int[] pixArray;
  int imageWidth; // = getImageWidth();
  int imageHeight; // = getImageHeight();

  public ImgPrc(){
  }

  public ImgPrc(Image newimage) {
    //newimageをthis.imageにセット。imageWidth,hをセット。pixArrayにimageデータをセット。
    this.image = newimage;
    this.imageWidth = getImageWidth();
    this.imageHeight = getImageHeight();
    this.pixArray = new int[imageWidth*imageHeight];
    PixelGrabber pg = new PixelGrabber(image,0,0,imageWidth,imageHeight,pixArray,0,imageWidth);
    try{
      //Set data into pixelArray[]
      pg.grabPixels();
    } catch(InterruptedException ex){
      System.out.println(ex);
    }
  }

  public void setNewImage(Image newimage){
    this.image = newimage;
    imageWidth = getImageWidth();
    imageHeight = getImageHeight();
    pixArray = new int[imageWidth*imageHeight];
  }

  public Image getCurrentImage(){
    return this.image;
  }

  public BufferedImage getBufferedImage () {
        BufferedImage convertedImage = new BufferedImage(
            getImageWidth(), getImageHeight(), BufferedImage.TYPE_INT_RGB);
        convertedImage.getGraphics().drawImage(this.image, 0, 0, null);
        return convertedImage;
    }      

  //配列から新たなイメージをセット
  //@SuppressWarnings("unchecked")
  public void setPixelsFromArray(int[] pixels,int w,int h){
    MemoryImageSource m = new MemoryImageSource(w,h,pixels,0,w);
    image=Toolkit.getDefaultToolkit().createImage(m);
  }

  //Get image as an Array
  public int[] getPixArray(){
    int[] pixels;
    if (image == null)
      return null;
    int w = getImageWidth();
    int h = getImageHeight();
    pixels = new int[w*h];
    PixelGrabber pg = new PixelGrabber(image,0,0,w,h,pixels,0,w);
    try{
      pg.grabPixels();
    } catch(InterruptedException ex){
    System.out.println(ex);
    }
    return pixels;
  }
 
  public int getImageWidth(){
    if (this.image != null){
      return this.image.getWidth(null);
    } else {
      return -1;
    }
  }

  public int getImageHeight(){
    if (this.image != null){
      return this.image.getHeight(null);
    } else {
      return -1;
    }
  }

  void contrastChange(int thres, double dParam, int shift) {
    int p,red,green,blue;
    int[] adjRGB = new int[3], valRGB = new int [3];
    for (int i = 0; i < imageWidth * imageHeight; i++){
      //各色の切り出し
      p = pixArray[i];
      red = (0xff & (p >> 16));
      green = (0xff & (p >> 8));
      blue = (0xff & p);

      valRGB[0] = red;
      valRGB[1] = green;
      valRGB[2] = blue;
      for (int j = 0; j < 3;j++){
        adjRGB[j]= (int)( dParam * 25.0 * Math.cbrt((double)(valRGB[j]-thres)));
      }
      red += adjRGB[0]+shift;
      green += adjRGB[1]+shift;
      blue += adjRGB[2]+shift;
      if (red > 255) red = 255;
      if (red < 0 ) red = 0;
      if (green > 255) green = 255;
      if (green < 0 ) green = 0;
      if (blue > 255) blue = 255;
      if (blue < 0 ) blue = 0;

      pixArray[i] = (0xff000000 | red << 16 | green << 8 | blue);
    }
    try {
      setPixelsFromArray(pixArray,imageWidth,imageHeight); //新たなイメージをセット
    } catch (Exception ex) {
      ;
    }
  } 
 
  //(x,y)座標のRGBいずれかの強度情報を返す
  //引数rgbは、red=2,green=1,blue=0
  private int getRGBpix(int[] pix,int x,int y,int w,int h,int rgb){
    if (x < 0)
      x = 0;
    if (x > w-1)
      x = w-1;
    if (y < 0)
      y = 0;
    if (y > h-1)
      y = h-1;
    int p = pix[y * w + x];
    return 0xff & (p >> (8 * rgb));
  }


public void scaleImage(int dw, int dh) {
  //this.image のサイズを(dw,dh)に変更する
      BufferedImage resizeImage=
        new BufferedImage(
          dw,
          dh,
          BufferedImage.TYPE_3BYTE_BGR
        );
  
      //imageはこのクラスのメンバ
        resizeImage.getGraphics().drawImage(
        image.getScaledInstance(
          dw,
          dh,
          Image.SCALE_AREA_AVERAGING
        ),
        0,
        0,
        dw,
        dh,
        null
      );
        resizeImage.getGraphics().drawImage(
          image.getScaledInstance(
            dw, 
            dh, 
            Image.SCALE_AREA_AVERAGING
          ), 
          0, 
          0, 
          dw, 
          dh, 
          null
        );
      image=resizeImage;
}  
  
  public void softenImage(int op) {
    int[] pixels = getPixArray(); //現在のイメージを配列で取得
    if (pixels == null) return;
    int w = getImageWidth();
    int h = getImageHeight();
    int i,j,k;

    int[] param = new int[9];
    int m=0;
    for (i = 0;i < 9;i++){
      param[i] = 2;
      m+=param[i];
    }
    m-=param[4];
    param[4] = op;    //中心の重み
    m += op; //分母

    int[] valRGB = new int[3];
    int[] pixels2 = new int[w * h];

    for (i = 0;i < h;i++){
      System.out.println(i);
      for (j = 0;j < w;j++){
        for (k = 0;k < 3;k++){
          valRGB[k] = (
              (getRGBpix(pixels,j-1,i-1,w, h, k) * param[0]) +
              (getRGBpix(pixels,j  ,i-1,w, h, k) * param[1]) +
              (getRGBpix(pixels,j+1,i-1,w, h, k) * param[2]) +
              (getRGBpix(pixels,j-1,i  ,w, h, k) * param[3]) +
              (getRGBpix(pixels,j  ,i  ,w, h, k) * param[4]) +
              (getRGBpix(pixels,j+1,i  ,w, h, k) * param[5]) +
              (getRGBpix(pixels,j-1,i+1,w, h, k) * param[6]) +
              (getRGBpix(pixels,j  ,i+1,w, h, k) * param[7]) +
              (getRGBpix(pixels,j+1,i+1,w, h, k) * param[8])) / m;
          if (valRGB[k] < 0) valRGB[k] = 0;
          if (valRGB[k] > 255) valRGB[k] = 255;
        }
      pixels2[i * w + j] = (0xff000000 | valRGB[2] << 16 | valRGB[1] << 8 | valRGB[0]);
      }
    }
    try {
      //pixels2[]をimageにセット
      setPixelsFromArray(pixels2,w,h);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public void brightImage( double gm) {
    int[] pixels = getPixArray(); //現在のイメージを配列で取得
    if (pixels == null)  return;
    int w = getImageWidth();
    int h = getImageHeight();
    int p,red,green,blue;
    double gamma = gm;  //2.2;//ガンマ値
    for (int i = 0; i < w*h; i++){
      //各色の切り出し
      p = pixels[i];
      red = (0xff & (p >> 16));
      green = (0xff & (p >> 8));
      blue = (0xff & p);
      //ガンマ補正
      red = (int)(255.0 * Math.pow(((double)red / 255.0),(1.0 / gamma)));
      green = (int)(255.0 * Math.pow(((double)green / 255.0),(1.0 / gamma)));
      blue = (int)(255.0 * Math.pow(((double)blue / 255.0),(1.0 / gamma)));
      if (red > 255) red = 255;
      if (red < 0 ) red = 0;
      if (green > 255) green = 255;
      if (green < 0 ) green = 0;
      if (blue > 255) blue = 255;
      if (blue < 0 ) blue = 0;
      pixels[i] = (0xff000000 | red << 16 | green << 8 | blue);
    }
    try {
      setPixelsFromArray(pixels,w,h); //新たなイメージをセット
    } catch (Exception ex) {

    }
  }  
  
  public void sharpImage() {
    //先鋭化
    int[] pixels = getPixArray();
    int w,h;
    int i,j,k;
    pixels = getPixArray();
    if (pixels == null)
      return;
    w = getImageWidth();
    h = getImageHeight();

    int[] rgb = new int[3];
    int[] pixels2 = new int[w * h];
    for (i = 0;i < h;i++){
      for (j = 0;j < w;j++){
      for (k = 0;k < 3;k++){
        rgb[k] = getRGBpix(pixels, j, i, w, h, k) * 5 -
             (getRGBpix(pixels, j  ,i-1, w, h, k) +
            getRGBpix(pixels,j-1,i  , w, h, k) +
            getRGBpix(pixels,j+1,i  , w, h, k) +
            getRGBpix(pixels,j  ,i+1, w, h, k));
        if (rgb[k] < 0) rgb[k] = 0;
        if (rgb[k] > 255) rgb[k] = 255;
      }
      pixels2[i * w + j] = (0xff000000 | rgb[2] << 16 | rgb[1] << 8 | rgb[0]);
      }
    }
      setPixelsFromArray(pixels2,w,h);
  }

  
  private int getRGBpixInt(int x, int y, int w, int h, int colorval){
    //colorval: 0=blue, 1=green, 2=red
    int[] pixels = getPixArray(); //現在のイメージを配列で取得
  if (x < 0)
      x = 0;
    if (x > w-1)
      x = w-1;
    if (y < 0)
      y = 0;
    if (y > h-1)
      y = h-1;
    int p = pixels[y * w + x];
    return 0xff & (p >> (8 * colorval));
  }

  //左右90°回転
  public void turnImage(int direction){
  //direction: 1=右90度  2=左90度 回転
    if (this.image == null)
      return;
    int[] pix1 = this.getPixArray();
    int w1 = this.getImageWidth();
    int h1 = this.getImageHeight();
    int i,j,p1,p2;
    int w2 = h1;
    int h2 = w1;
    int[] pix2 = new int[w2 * h2];
    if (direction == 1){        //右90度回転
      for (i = 0; i < h1; i++){
        for (j = 0; j < w1; j++){
          p1 = i * w1 + j;
          p2 = (h1 - (i + 1)) + (j * h1);
          pix2[p2] = pix1[p1];
        }
      }
    } else if (direction == 2){ //左90度回転
      for (i = 0; i < h1; i++){
        for (j = 0; j < w1; j++){
          p1 = i * w1 + j;
          p2 = (h1 * w1 - (h1 - i)) - (j * h1);
          pix2[p2] = pix1[p1];
        }
      }
    } else {
      return;
    }
    this.setPixelsFromArray(pix2,w2,h2);
  }

}

以下の書籍の最初のものはC#ですが,TCP/IPソケット通信のサンプルが簡潔に記述されていて、参考になると思います。英語ですが、技術文ですから読みやすい。また、ソースコードだけ読んでも十分参考になると思います。