Tuesday, December 11, 2012

Soucecode memperbesar ukuran gambar pada JAVA (J2ME)



import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class PerbesarGambar extends MIDlet
  implements CommandListener {
   
  private Display display;
  private Form form;
  private Image image;

  private final Command cmdKeluar =
    new Command("Keluar", Command.EXIT, 1);
  private final Command cmdPerbesar =
    new Command("Perbesar", Command.SCREEN, 1);
 
  public PerbesarGambar() {
    display = Display.getDisplay(this);   

    form = new Form("Memperbesar Gambar"); 
   
    form.addCommand(cmdPerbesar);
    form.addCommand(cmdKeluar);
    form.setCommandListener(this);
  }
   
  public void startApp() {
    image = null;
    try {
        image = Image.createImage("/image5.jpg");
    } catch(IOException ioe) {
        ioe.printStackTrace();
    }
    form.append(image);
    display.setCurrent(form);
  }
   
  public void pauseApp() {
  }
   
  public void destroyApp(boolean unconditional) {
  }
   
  public void commandAction(Command c, Displayable s) {
    if (c == cmdKeluar) {
      destroyApp(false);
      notifyDestroyed();
    } else if (c == cmdPerbesar) {       
      form.deleteAll();
      image = perbesarGambar(image);
      form.append(image);
      display.setCurrent(form);
    } 
  }
 
  public Image perbesarGambar(Image img) {
    Image imgOutput = null;
   
    int[] rgbOutput = null;
    Graphics g = null;
    int lebar = 0;
    int tinggi = 0;
   
    try {       
      lebar  =  img.getWidth();
      tinggi = img.getHeight();       
      imgOutput  = Image.createImage(lebar << 1, tinggi << 1);
       
      int rgbInput[] = new int[lebar * tinggi];
      rgbOutput = new int[(lebar << 1) * (tinggi << 1)];
       
      img.getRGB(rgbInput, 0, lebar, 0, 0, lebar, tinggi);
       
      int i,j,k;
      k=0;
      for(i=0; i<(tinggi << 1); i+=2) {
        for(j=0; j<(lebar << 1); j+=2) {
          rgbOutput[i* (lebar << 1) + j]      = rgbInput[k] ;
          rgbOutput[(i+1) * (lebar << 1) + j] = rgbInput[k];
          rgbOutput[i* (lebar << 1) + j+1]    = rgbInput[k];
          rgbOutput[(i+1)*(lebar << 1) + j+1] = rgbInput[k];
          k++;
        }
      }       
      g = imgOutput.getGraphics();
    } catch(Exception e){}
   
    return imgOutput.createRGBImage(rgbOutput,
                     lebar << 1,
                     tinggi << 1,
                     true);
  }   
}

No comments:

Post a Comment