안드로이드 개발 질문/답변
(글 수 45,052)
그레이 스케일과 R G B별로 각각 찍어서
그레이 버튼을 누르면 그림이 흑백이되고
R값을 누르면 빨간색 부분은 희게 나머지는 검정으로
G값을 누르면 녹색 부분은 희게 나머지는 검정
B값을 누르면 푸른색은 희게 나머지는 검정
이런식으로 만들었는데요
각각의 이미지를 sdcard에 저장하고 싶습니다.
제가 하고 싶은 것은
그레이 버튼을 눌렀을 때 저장버튼을 누르면 그때의 이미지가 저장되고
마찬가지로 RGB값 각각을 눌렀을 때 저장버튼을 누르면 그에 따른 이미지를 저장하고 싶습니다.
도무지 해결이 안되서 부탁합니다. ㅠㅠ..
소스코드는 첨부하겠습니다 ㅇㅅㅇ
package com.tomato;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Tomato extends Activity {
/** Called when the activity is first created. */
final String tag = "MySdcardWriteTest";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView imageView = (ImageView) findViewById(R.id.iv);
Button Original_button = (Button)findViewById(R.id.bt1);
Button Gray_button = (Button)findViewById(R.id.bt2);
Button Red_button = (Button)findViewById(R.id.bt3);
Button Green_button = (Button)findViewById(R.id.bt4);
Button Blue_button = (Button)findViewById(R.id.bt5);
Button Save_button = (Button)findViewById(R.id.bt6);
Bitmap src = BitmapFactory.decodeFile("/sdcard/tomato1.jpg");
Bitmap resized = Bitmap.createScaledBitmap(src, 400, 290, true);
imageView.setImageBitmap(resized);
String path ="/sdcard/";
File file = new File(path+"save.jpg");
try{
FileOutputStream fos = new FileOutputStream(file);
fos.write("this is test~!!\nThis Is Test~!!".getBytes());
fos.close();
}catch(IOException e){
Log.i(tag , e.getMessage());
}
Original_button.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
Bitmap src = BitmapFactory.decodeFile("/sdcard/tomato1.jpg");
Bitmap resized = Bitmap.createScaledBitmap(src, 400, 290, true);
imageView.setImageBitmap(resized);
}
}
);
Gray_button.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
Bitmap src = BitmapFactory.decodeFile("/sdcard/tomato1.jpg");
Bitmap resized = Bitmap.createScaledBitmap(src, 400, 290, true);
imageView.setImageBitmap(Gray(resized));
}
}
);
Red_button.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
Bitmap src = BitmapFactory.decodeFile("/sdcard/tomato1.jpg");
Bitmap resized = Bitmap.createScaledBitmap(src, 400, 290, true);
imageView.setImageBitmap(Red(resized));
}
}
);
Green_button.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
Bitmap src = BitmapFactory.decodeFile("/sdcard/tomato1.jpg");
Bitmap resized = Bitmap.createScaledBitmap(src, 400, 290, true);
imageView.setImageBitmap(Green(resized));
}
}
);
Blue_button.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
Bitmap src = BitmapFactory.decodeFile("/sdcard/tomato1.jpg");
Bitmap resized = Bitmap.createScaledBitmap(src, 400, 290, true);
imageView.setImageBitmap(Blue(resized));
}
}
);
Save_button.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
}
}
);
}
public Bitmap Gray(Bitmap imgBit) {
int mWidth = imgBit.getWidth();
int mHeight = imgBit.getHeight();
for(int x = 0; x < mWidth; x++) {
for(int y = 0; y < mHeight; y++) {
int color = imgBit.getPixel(x, y);
int alpha = ( color & 0xFF000000 );
int red = (color & 0x00FF0000) >> 16;
int green = (color & 0x0000FF00) >> 8;
int blue = (color & 0x000000FF);
double red2 = red * 0.2989;
double green2 = green * 0.587;
double blue2 = blue * 0.114;
int newRGB = (int)( red2 + green2 + blue2 );
int NewColor = alpha | (newRGB<<16)|(newRGB<<8)| newRGB;
imgBit.setPixel(x, y, NewColor);
}
}
return imgBit;
}
public Bitmap Red(Bitmap imgBit) {
int mWidth = imgBit.getWidth();
int mHeight = imgBit.getHeight();
for(int x = 0; x < mWidth; x++) {
for(int y = 0; y < mHeight; y++) {
int color = imgBit.getPixel(x, y);
int alpha = ( color & 0xFF000000 );
int red = (color & 0x00FF0000) >> 16;
double red2 = red * 0.2989;
int newRGB = (int) ( red2 + red2 + red2);
int NewColor = alpha | (newRGB<<16)|(newRGB<<8)| newRGB;
imgBit.setPixel(x, y, NewColor);
}
}
return imgBit;
}
public Bitmap Green(Bitmap imgBit) {
int mWidth = imgBit.getWidth();
int mHeight = imgBit.getHeight();
for(int x = 0; x < mWidth; x++) {
for(int y = 0; y < mHeight; y++) {
int color = imgBit.getPixel(x, y);
int alpha = ( color & 0xFF000000 );
int green = (color & 0x0000FF00) >> 8;
double green2 = green * 0.587;
int newRGB = (int) ( green2 + green2 + green2);
int NewColor = alpha | (newRGB<<16)|(newRGB<<8)| newRGB;
imgBit.setPixel(x, y, NewColor);
}
}
return imgBit;
}
public Bitmap Blue(Bitmap imgBit) {
int mWidth = imgBit.getWidth();
int mHeight = imgBit.getHeight();
for(int x = 0; x < mWidth; x++) {
for(int y = 0; y < mHeight; y++) {
int color = imgBit.getPixel(x, y);
int alpha = ( color & 0xFF000000 );
int blue = (color & 0x000000FF);
double blue2 = blue * 0.114;
int newRGB = (int) ( blue2 + blue2 + blue2);
int NewColor = alpha | (newRGB<<16)|(newRGB<<8)| newRGB;
imgBit.setPixel(x, y, NewColor);
}
}
return imgBit;
}
}




Red, Green, Blue 메소드 실행후에, ImageView 의 enableDrawaingCache 호출하신 후, getDrawaingCache 호출하시면
현재 그려진 상태의 Bitmap 반환됩니다. 그 Bitmap 저장하시면 되겠네요.