public class ImageProcActivity extends Activity {
Button btnBrowser, btnOriginal, btnGray, btnInvert;
TextView tv;
ImageView iv;
static final int ActID =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_proc);
btnBrowser = (Button)findViewById(R.id.btn_gallery);
btnOriginal = (Button)findViewById(R.id.btn_original);
btnGray = (Button)findViewById(R.id.btn_gray);
btnInvert = (Button)findViewById(R.id.btn_invert);
iv = (ImageView)findViewById(R.id.iv);
tv = (TextView)findViewById(R.id.tv);
ImageView image;
btnBrowser.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
Intent intentIG = new Intent(Intent.ACTION_GET_CONTENT);
intentIG.setType("image/*");
ImageProcActivity.this.startActivityForResult(intentIG, ActID);
}
});
btnOriginal.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//iv.setImageURI(onActivityResult.uri);
//iv.setImageURI(OnActivityResultListener(intentIG,ActID));
//我不知道如何在这里使用浏览器中的图像。不能定义为变量......我怎样才能得到它?
}
});
btnGray.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//还有如何让图像变灰?我研究了几个代码但不明白。你能不能用简单的方式解释我几周前就开始学习JAVA ......
}
});
btnInvert.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
}
});
}
@Override
public void onActivityResult(int activityID, int resultCode, Intent intent) {
if(activityID==ActID && resultCode==Activity.RESULT_OK) {
Uri uri = intent.getData();
tv.setText(uri.toString());
iv.setImageURI(uri);
}
}
}
答案 0 :(得分:0)
我认为你是新来的,所以你在格式化你的问题时遇到了一些问题。
顺便说一句,你可以使用下面的代码片段将图像转换为灰度,如果你有任何进一步的混淆,请告诉我:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class GrayScale {
BufferedImage image;
int width;
int height;
public GrayScale() {
try {
File input = new File("digital_image_processing.jpg");
image = ImageIO.read(input);
width = image.getWidth();
height = image.getHeight();
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
Color c = new Color(image.getRGB(j, i));
int red = (int)(c.getRed() * 0.299);
int green = (int)(c.getGreen() * 0.587);
int blue = (int)(c.getBlue() *0.114);
Color newColor = new Color(red+green+blue,
red+green+blue,red+green+blue);
image.setRGB(j,i,newColor.getRGB());
}
}
File ouptut = new File("grayscale.jpg");
ImageIO.write(image, "jpg", ouptut);
} catch (Exception e) {}
}
static public void main(String args[]) throws Exception
{
GrayScale obj = new GrayScale();
}
}