J2's Image Scaling

Source: Internet
Author: User

 

I used to collect this function on the internet, regardless of the effect or performance. I only think it is very useful.
It took a long time, but unfortunately I don't know who it is. Thank you for the author.
This function uses the getrgb () function of midp2.0, which is efficient. Basically, there is nothing to optimize.

  1. Public static image zoomimage (image SRC, int desw, int Desh ){
  2. Image desimg = NULL;
  3. Int srcw = SRC. getwidth (); // Original Image Width
  4. Int srch = SRC. getheight (); // The height of the original image.
  5. Int [] srcbuf = new int [srcw * srch]; // cache the pixel information of the original image.
  6. SRC. getrgb (srcbuf, 0, srcw, 0, 0, srcw, srch );
  7. // Calculate the interpolation table
  8. Int [] taby = new int [Desh];
  9. Int [] tabx = new int [desw];
  10. Int sb = 0;
  11. Int DB = 0;
  12. Int tems = 0;
  13. Int temd = 0;
  14. Int distance = srch> Desh? Srch: Desh;
  15. For (INT I = 0; I <= distance; I ++) {/* vertical */
  16. Taby [dB] = Sb;
  17. Tems + = srch;
  18. Temd + = Desh;
  19. If (TEMS> distance ){
  20. Tems-= distance;
  21. SB ++;
  22. }
  23. If (temd> distance ){
  24. Temd-= distance;
  25. DB ++;
  26. }
  27. }
  28. SB = 0;
  29. DB = 0;
  30. Tems = 0;
  31. Temd = 0;
  32. Distance = srcw> desw? Srcw: desw;
  33. For (INT I = 0; I <= distance; I ++) {/* horizontal direction */
  34. Tabx [dB] = (short) Sb;
  35. Tems + = srcw;
  36. Temd + = desw;
  37. If (TEMS> distance ){
  38. Tems-= distance;
  39. SB ++;
  40. }
  41. If (temd> distance ){
  42. Temd-= distance;
  43. DB ++;
  44. }
  45. }
  46. // Generate the image pixel Buf after zoom-in and zoom-out
  47. Int [] desbuf = new int [desw * Desh];
  48. Int dx = 0;
  49. Int DY = 0;
  50. Int Sy = 0;
  51. Int Oldy =-1;
  52. For (INT I = 0; I <Desh; I ++ ){
  53. If (Oldy = taby [I]) {
  54. System. arraycopy (desbuf, Dy-desw, desbuf, Dy, desw );
  55. } Else {
  56. DX = 0;
  57. For (Int J = 0; j <desw; j ++ ){
  58. Desbuf [dy + dx] = srcbuf [SY + tabx [J];
  59. DX ++;
  60. }
  61. SY + = (taby [I]-Oldy) * srcw;
  62. }
  63. Oldy = taby [I];
  64. Dy + = desw;
  65. }
  66. // Generate an image
  67. Desimg = image. creatergbimage (desbuf, desw, Desh, false );
  68. Return desimg;
  69. }

 

 

 

Related Article

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.