Java code for generating high-quality vertices

Source: Internet
Author: User

Import java. AWT. image. bufferedimage;

Public class imagescale {

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

/**
* Determines the image size.
*/
Private int determineresultsize (int w, int h ){
Double scaleh, scalev;
Scaleh = (double) W/(double) width;
Scalev = (double) h/(double) height;
// You Need To determine scaleh and scalev. Do not zoom in.
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 ()

// Process the 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 <16) + (greenvalue <8) + bluevalue;
}

// Horizontal row 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 ()

// Horizontal Image 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; X ++ ){
Value = verticalfilter (pbimage, starty, stopy, start, stop,
X, tmpcontrib );
Pbout. setrgb (X, Y, value );
}
} Else {
For (INT x = 0; x <IW; X ++ ){
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 static void main (string [] ARGs ){
}
}
}

-----------------------------------------------------------------------
Public bufferedimage imagezoomout (bufferedimage srcbufferimage, int W, int H)
I changed this method
Public bufferedimage imagezoomout (bufferedimage srcbufferimage, float W, float H)
To facilitate proportional scaling, change W and H to the flaot type as the scaling ratio, and change it elsewhere.

If it is displayed on a webpage, you need to create a servlet. The Code is as follows:
Package servlet;

Import java. AWT. image. bufferedimage;
Import java. Io. file;
Import java. Io. ioexception;

Import javax. ImageIO. ImageIO;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

Import com. imagescale;

Public class imageservlet extends httpservlet {

Private Static final long serialversionuid = 1l;

Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}

Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setheader ("cache-control", "No-store ");
Response. setheader ("Pragma", "No-Cache ");
Response. setdateheader ("expires", 0 );
Response. setcontenttype ("image/JPEG ");
    
// Put the image in the images directory of the current application
String Path = getservletcontext (). getrealpath ("images/2.jpg ");
Bufferedimage image1 = ImageIO. Read (new file (PATH ));
    
If (request. getparameter ("X") = NULL ){
ImageIO. Write (image1, "Jpeg", response. getoutputstream ());
} Else {
Float W = float. parsefloat (request. getparameter ("X "));
Float h;
If (request. getparameter ("Y") = NULL ){
H = W;
} Else {
H = float. parsefloat (request. getparameter ("Y "));
}
Imagescale is = new imagescale ();
Bufferedimage image2 = is. imagezoomout (image1, W, H );
ImageIO. Write (image2, "Jpeg", response. getoutputstream ());
}
}
}

Add the corresponding configuration in Web. xml:
<Servlet>
<Servlet-Name> imageservlet </servlet-Name>
<Servlet-class> servlet. imageservlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> imageservlet </servlet-Name>
<URL-pattern>/imageservlet </url-pattern>
</Servlet-mapping>

Test page:
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK"/>
</Head>
<Body>
Image Scaling Test <br/>












</Body>
</Html>
 

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.