Java Generation compression diagram

Source: Internet
Author: User
Tags sin

Link Address: http://blog.sina.com.cn/s/blog_407a68fc0100nrba.html

Package util;

Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;

Import Javax.imageio.ImageIO;


public class Imgthumb {
private int width;
private int height;
private int scalewidth;
Double support = (double) 3.0;
Double PI = (double) 3.14159265358978;
Double[] contrib;
Double[] Normcontrib;
Double[] Tmpcontrib;
int Startcontrib, Stopcontrib;
int nDots;
int nhalfdots;


Public BufferedImage imagezoomout (bufferedimage srcbufferimage, int w, int h) {
width = Srcbufferimage.getwidth ();
Height = srcbufferimage.getheight ();
ScaleWidth = W;

if (Determineresultsize (w, h) = = 1) {
return srcbufferimage;
}
Calcontrib ();
BufferedImage pbout = horizontalfiltering (Srcbufferimage, W);
BufferedImage pbfinalout = verticalfiltering (Pbout, h);
return pbfinalout;
}


private int Determineresultsize (int w, int h) {
Double Scaleh, Scalev;

Update by Libra
Double wt = w > width? Width:w;
Double ht = h > Height? Height:h;

Scaleh = (double) wt/(double) width;
Scalev = (double) HT/(double) height;

Need to judge Scaleh,scalev, do not enlarge operation
if (Scaleh >= 1.0 && Scalev >= 1.0) {
return 1;
}
return 0;

}//End of Determineresultsize ()

Private double Lanczos (int i, int inwidth, int outwidth, double support) {
Double X;

x = (double) I * (double) outwidth/(double) inwidth;

return Math.sin (x * pi)/(x * pi) * Math.sin (x * pi/support)
/(x * pi/support);

}//End of Lanczos ()

//
Assumption:same horizontal and vertical scaling factor
//
private void Calcontrib () {
Nhalfdots = (int) ((double) Width * Support/(double) scalewidth);
NDots = nhalfdots * 2 + 1;
try {
contrib = new Double[ndots];
Normcontrib = new Double[ndots];
Tmpcontrib = new Double[ndots];
} catch (Exception e) {
System.out.println ("init contrib,normcontrib,tmpcontrib" + e);
}

int center = nhalfdots;
Contrib[center] = 1.0;

Double weight = 0.0;
int i = 0;
for (i = 1; I <= Center; i++) {
Contrib[center + i] = Lanczos (i, Width, scalewidth, support);
Weight + = Contrib[center + i];
}

for (i = center-1; I >= 0; i--) {
Contrib[i] = Contrib[center * 2-i];
}

Weight = Weight * 2 + 1.0;

for (i = 0; I <= Center; i++) {
Normcontrib[i] = contrib[i]/weight;
}

for (i = center + 1; i < nDots; i++) {
Normcontrib[i] = Normcontrib[center * 2-i];
}
}//End of Calcontrib ()

Processing Edge
private void Caltempcontrib (int start, int stop) {
Double weight = 0;

int i = 0;
for (i = start; I <= stop; i++) {
Weight + = Contrib[i];
}

for (i = start; I <= stop; i++) {
Tmpcontrib[i] = contrib[i]/weight;
}

}//End of Caltempcontrib ()

private int Getredvalue (int rgbvalue) {
int temp = rgbvalue & 0x00ff0000;
return temp >> 16;
}

private int Getgreenvalue (int rgbvalue) {
int temp = rgbvalue & 0x0000ff00;
return temp >> 8;
}

private int Getbluevalue (int rgbvalue) {
return Rgbvalue & 0x000000ff;
}

private int Comrgb (int redvalue, int greenvalue, int bluevalue) {

Return (Redvalue << +) + (Greenvalue << 8) + Bluevalue;
}

Line Horizontal filtering
private int Horizontalfilter (bufferedimage bufimg, int startX, int stopx,
int start, int stop, int y, double[] pcontrib) {
Double valuered = 0.0;
Double valuegreen = 0.0;
Double valueblue = 0.0;
int Valuergb = 0;
int I, J;

for (i = StartX, j = start; I <= stopx; i++, J + +) {
Valuergb = Bufimg.getrgb (i, y);

Valuered + = Getredvalue (VALUERGB) * PCONTRIB[J];
Valuegreen + = Getgreenvalue (VALUERGB) * PCONTRIB[J];
Valueblue + = Getbluevalue (VALUERGB) * PCONTRIB[J];
}

Valuergb = Comrgb (clip ((int) valuered), clip ((int) valuegreen),
Clip ((int) valueblue));
return VALUERGB;

}//End of Horizontalfilter ()

Image horizontal Filtering
Private BufferedImage horizontalfiltering (bufferedimage bufimage, int ioutw) {
int dwinw = Bufimage.getwidth ();
int dwinh = Bufimage.getheight ();
int value = 0;
BufferedImage pbout = new BufferedImage (IOUTW, Dwinh,
BUFFEREDIMAGE.TYPE_INT_RGB);

for (int x = 0; x < IOUTW; × x + +) {

int StartX;
int start;
int x = (int) ((double) x) * ((double) DWINW)/((double) IOUTW) + 0.5);
int y = 0;

StartX = X-nhalfdots;
if (StartX < 0) {
StartX = 0;
start = Nhalfdots-x;
} else {
start = 0;
}

   int stop;
   int stopx = X + nhalfdots;
   if (Stopx > (dwInW-1)) {
    stopx = dwInW-1;
    stop = Nhalfdots + (dwInW-1-X);
   } else {
    stop = nhalfdots * 2;
   }

if (Start > 0 | | Stop < NDOTS-1) {
Caltempcontrib (start, stop);
for (y = 0; y < Dwinh; y++) {
Value = Horizontalfilter (Bufimage, StartX, STOPX, start,
Stop, y, tmpcontrib);
Pbout.setrgb (x, y, value);
}
} else {
for (y = 0; y < Dwinh; y++) {
Value = Horizontalfilter (Bufimage, StartX, STOPX, start,
Stop, y, normcontrib);
Pbout.setrgb (x, y, value);
}
}
}

return pbout;

}//End of horizontalfiltering ()

private int Verticalfilter (bufferedimage pbinimage, int starty, int stopy,
int start, int stop, int x, double[] pcontrib) {
Double valuered = 0.0;
Double valuegreen = 0.0;
Double valueblue = 0.0;
int Valuergb = 0;
int I, J;

for (i = starty, j = start; I <= stopy; i++, J + +) {
Valuergb = Pbinimage.getrgb (x, i);

Valuered + = Getredvalue (VALUERGB) * PCONTRIB[J];
Valuegreen + = Getgreenvalue (VALUERGB) * PCONTRIB[J];
Valueblue + = Getbluevalue (VALUERGB) * PCONTRIB[J];
System.out.println (valuered+ "," +clip (int) valuered) + "<-");
//
System.out.println (valuegreen+ "," +clip (int) valuegreen) + "<-");
System.out.println (valueblue+ "," +clip (int) valueblue) + "<-" + "--");
}

Valuergb = Comrgb (clip ((int) valuered), clip ((int) valuegreen),
Clip ((int) valueblue));
System.out.println (VALUERGB);
return VALUERGB;

}//End of Verticalfilter ()

Private BufferedImage verticalfiltering (bufferedimage pbimage, int iouth) {
int iW = Pbimage.getwidth ();
int iH = Pbimage.getheight ();
int value = 0;
BufferedImage pbout = new BufferedImage (IW, Iouth,
BUFFEREDIMAGE.TYPE_INT_RGB);

for (int y = 0; y < Iouth; y++) {

int starty;
int start;
int y = (int) ((double) y) * ((double) IH)/((double) iouth) + 0.5);

Starty = Y-nhalfdots;
if (Starty < 0) {
Starty = 0;
start = nhalfdots-y;
} else {
start = 0;
}

   int stop;
   int stopy = Y + nhalfdots;
   if (stopy > (int) (iH-1)) {
    stopy = iH-1;
    stop = Nhalfdots + (iH-1-Y);
   } else {
    stop = nhalfdots * 2;
   }

if (Start > 0 | | Stop < NDOTS-1) {
Caltempcontrib (start, stop);
for (int x = 0; x < IW; + +) {
Value = Verticalfilter (Pbimage, Starty, Stopy, start, stop,
x, Tmpcontrib);
Pbout.setrgb (x, y, value);
}
} else {
for (int x = 0; x < IW; + +) {
Value = Verticalfilter (Pbimage, Starty, Stopy, start, stop,
x, Normcontrib);
Pbout.setrgb (x, y, value);
}
}

}

return pbout;

}//End of verticalfiltering ()

int Clip (int x) {
if (x < 0)
return 0;
if (x > 255)
return 255;
return x;
}

 public Boolean Scale (string source, string target, int width, int height) {
  file f = new File (sour CE);
  try {
   bufferedimage bi = imageio.read (f);
   bufferedimage out = null;
   imgthumb scal = new Imgthumb ();
   int _width = Bi.getwidth ();//width
   int _height = Bi.getheight ();//High
    int[] _arr = Getimagewidthandheight (_width, _height, width, height);
   out = scal.imagezoomout (Bi, _arr[0], _arr[1]);
   file t = new File (target);
   imageio.write (out, "jpg", t);
   return true;
  } catch (IOException e) {
   e.printstacktrace ();
   return false;
  }
 }

Public int[] Picscale (string source, string target, int w, int h) {
File F = new file (source);
Int[] arr = {0, 0};
try {
BufferedImage bi = imageio.read (f);
BufferedImage out = null;
Imgthumb scal = new Imgthumb ();
arr = Getimagewidthandheight (Bi.getwidth (), Bi.getheight (), W, h);
out = Scal.imagezoomout (bi, arr[0], arr[1]);
File T = new file (target);
Imageio.write (out, "jpg", t);
} catch (IOException e) {
E.printstacktrace ();
}
return arr;
}


private static int[] getimagewidthandheight (int orgw, int orgh, int avW,
int AvH) {
int width = 0;
int height = 0;

if (ORGW > 0 && orgh > 0) {
if (Orgw/orgh >= AVW/AVH) {
if (ORGW > AvW) {
width = AvW;
Height = (Orgh * avW)/ORGW;
} else {
width = ORGW;
Height = Orgh;
}
System.out.println ("++WIDHT::::" + width + "Height::::"
+ height);
} else {
if (Orgh > AvH) {
Height = AvH;
width = (ORGW * avH)/Orgh;
} else {
width = ORGW;
Height = Orgh;
}
System.out.println ("++WIDHT::::" + width + "Height::::"
+ height);
}
}
int[] arr = new int[2];
Arr[0] = width;
ARR[1] = height;

Long start = System.currenttimemillis ();
int width = 0;
int height = 0;
if ((W/TARW) >= (H/tarh)) {//Width reduction ratio greater than high
width = tarw;
Height = H * TARW/W;
System.out.println (width + "" + height);
} else {
Height = Tarh;
width = W * tarh/h;
System.out.println (width + "" + height);
// }
int[] arr = new int[2];
Arr[0] = width;
ARR[1] = height;
Long end = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Wide-height Processing:" + (End-start));
return arr;
}

public static void Main (string[] args) {
Imgthumb is = new Imgthumb ();
Long start = System.currenttimemillis ();
Is.scale ("D:/1.gif", "D:/2.gif", 227, 400);
Long end = System.currenttimemillis ();
System.out.println ("Time:" + (End-start));
}
}

Java Generation compression diagram

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.