Home > Java > javaTutorial > body text

java image enlargement and reduction

黄舟
Release: 2016-12-30 11:51:04
Original
1933 people have browsed it

To enlarge the image, you need to supplement the missing pixels. Commonly used methods are

1. Nearest Neighbor algorithm (Nearest Neighbor)

2. Bilinear interpolation algorithm (Bilinear Interpolation )

3. Bicubic Interpolation Algorithm (Bicubic Interpolation)

Wait, please see (Image Magnification Algorithm) for detailed introduction
Now we will use the nearest neighbor point interpolation method to magnify the image The effect of twice the code

<span style="font-size:14px;"><span style="font-size:10px;">p<span style="font-family:Courier New;">ublic void Todouble(){
		int[] doubleData = new int[2*w*2*h];
		for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
            	doubleData[2*x + 2*y *2* w] = data[x + y * w];
            }
		}
		
		this.h = 2*h;
		this.w = 2*w;
		for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
            	if(y%2 == 1)
            		doubleData[x + y*w] = doubleData[x + (y-1)*w];
            	if(x%2 == 1)
            		doubleData[x + y*w] = doubleData[x-1 + y*w];
            }
		}
		this.data = doubleData;
		
	}</span></span></span>
Copy after login

is as follows:

java image enlargement and reduction

java image enlargement and reduction

## Reduction is relatively simple, and reduction is to remove some pixels.


Minified code:

<span   style="max-width:90%"><span style="font-size:10px;">public void reduce(int a){//a是缩小的<span style="font-family:Times New Roman;">倍数</span>
		int nw = w/a;
		int nh = h/a;
		int[] d = new int[nw*nh];
		
		for (int y = 0; y < nh; y++) {
                  for (int x = 0; x < nw; x++) {
            	   d[x + y*nw] = data[a*x + a*y * w];
                  }
		}
		this.h = nh;
		this.w = nw;
		this.data = d;
	}</span></span>
Copy after login
The running effect is as follows:

java image enlargement and reduction

The above is the content of enlarging and reducing the java image, more related Please pay attention to the PHP Chinese website (www.php.cn) for content!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template