ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 1bit bitmap 만들기
    과거...../개발일지 2012. 12. 14. 00:54

    1bit bitmap  만들기 


     


    : 관련자료를 찾으니 막상 없고 android 관련 소스만 잔뜩 어차피 기본은 같으니까 아래와 같이 수정하면 됩니다. 단 아래소스로 1비트 비트맵을 다시 프로세싱 하게 되면 검은 이미지가 나온다 그거에 대한 예외처리는 하지 않았다. 


    [관련 자료찾다보면 jAI 라이브러리를 많이 볼 수있다. 하지만 막상 1비트 이미지를 만들려고 하면 라이브러리 답게 편한 방식을 제공하는 것 같지는 않다]


    아래 소스는 특별한 거는 없다 어느정도 이상의 값이면 값을 바꿔라 정도????


    android 관련 1bit bitmap 만드는 부분은 옆에 링크에서 확인할 수 있다 바로가기

    (위 링크는 이미지 사이즈가 고정이다.) 

    import java.awt.image.BufferedImage;
    import java.awt.image.WritableRaster;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    public class TestBmp{
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		
    		System.out.print("start");
    		 try {
    			BufferedImage buffer = ImageIO.read(new File("c://aa.jpg"));
    			BufferedImage image=  processImage(buffer);
    			
    			
    			
    			File outputStream = new File("aa.bmp");
    			String imageType = "BMP";
    			try {
    				ImageIO.write(image, imageType, outputStream);
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			
    			
    		} catch (IOException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		// TODO Auto-generated method stub
    		
    		 System.out.print("end");
    
    	}
    
    	public static BufferedImage processImage(BufferedImage inputImage) {
    
    		// Create a binary image for the results of processing
    
    		int w = inputImage.getWidth();
    		int h = inputImage.getHeight();
    		BufferedImage outputImage = new BufferedImage(w, h,
    				BufferedImage.TYPE_BYTE_BINARY);
    
    		// Work on a copy of input image because it is modified by diffusion
    
    		WritableRaster input = inputImage.copyData(null);
    		WritableRaster output = outputImage.getRaster();
    
    		final int threshold = 128;
    		float value, error;
    
    		for (int y = 0; y < h; ++y)
    			for (int x = 0; x < w; ++x) {
    
    				value = input.getSample(x, y, 0);
    
    				// Threshold value and compute error
    
    				if (value < threshold) {
    					output.setSample(x, y, 0, 0);
    					error = value;
    				} else {
    					output.setSample(x, y, 0, 1);
    					error = value - 255;
    				}
    
    				// Spread error amongst neighbouring pixels
    
    				if ((x > 0) && (y > 0) && (x < (w - 1)) && (y < (h - 1))) {
    					value = input.getSample(x + 1, y, 0);
    					input.setSample(x + 1, y, 0, clamp(value + 0.4375f * error));
    					value = input.getSample(x - 1, y + 1, 0);
    					input.setSample(x - 1, y + 1, 0, clamp(value + 0.1875f
    							* error));
    					value = input.getSample(x, y + 1, 0);
    					input.setSample(x, y + 1, 0, clamp(value + 0.3125f * error));
    					value = input.getSample(x + 1, y + 1, 0);
    					input.setSample(x + 1, y + 1, 0, clamp(value + 0.0625f
    							* error));
    				}
    
    			}
    		return outputImage;
    
    	}
    
    	// Forces a value to a 0-255 integer range
    
    	public static int clamp(float value) {
    		return Math.min(Math.max(Math.round(value), 0), 255);
    	}
    
    }
    


Designed by Tistory.