下面显示的是从SD卡读取图像并将其转换为灰色图像的代码。它没有进度条实现,效果很好。然而,在我实现进度条之后,我无法获得任何图像。
public class MainActivity extends AppCompatActivity {
ProgressDialog barProgressDialog;
Handler updateBarHandler;
public static final int REQUEST_CODE = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rawImage = (ImageView) findViewById(R.id.rawImage);
updateBarHandler = new Handler();
}
public void rawImageClicked(View v) {
Intent rawIntent = new Intent(Intent.ACTION_PICK);
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
String pictureDirectoryPath = pictureDirectory.getPath();
Uri data=Uri.parse(pictureDirectoryPath);
rawIntent.setDataAndType(data, "image/*");
startActivityForResult(rawIntent, REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK){
if(requestCode==REQUEST_CODE){
final float red = (float) 0.299;
final float green = (float) 0.587;
final float blue = (float) 0.114;
final Uri imagef = data.getData();
final InputStream[] streamI = new InputStream[1];
//InputStream streamI;
final ProgressDialog ringProgressDialog =ProgressDialog.show(MainActivity.this, "Wait", "Downloading Image ...", true);
ringProgressDialog.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
try {
streamI[0] = getContentResolver().openInputStream(imagef);
//Create bitmap from selected image
Bitmap imgb = BitmapFactory.decodeStream(streamI[0]);
//Define rows and columns of selected image
int rows = imgb.getHeight();
int cols = imgb.getWidth();
operation =Bitmap.createBitmap(cols, rows, imgb.getConfig());
//Convert original image to Gray Image
for (int i=0;i<cols;i++){
for(int j=0;j<rows;j++) {
int p = imgb.getPixel(i, j);
int r = Color.red(p);
int g = Color.green(p);
int b = Color.blue(p);
r = (int) (red * r);
g = (int) (green * g);
b = (int) (blue * b);
int gray = (int) (r * 0.299 + g * 0.587 + b * 0.114);
operation.setPixel(i, j, Color.argb(Color.alpha(p), gray, gray, gray));
}
}
rawImage.setImageBitmap(operation);
Thread.sleep(100);
}catch (Exception e) {
e.printStackTrace();
}ringProgressDialog.dismiss();
}
}).start();
}
}
}
下面显示的是工作代码:
Uri imagef = data.getData();
InputStream streamI;
try{
streamI = getContentResolver().openInputStream(imagef);
//Create bitmap from selected image
Bitmap imgb = BitmapFactory.decodeStream(streamI);
//Define rows and columns of selected image
int rows = imgb.getHeight();
int cols = imgb.getWidth();
Log.d("rows",Integer.toString(rows)+ " "+Integer.toString(cols));
operation =Bitmap.createBitmap(cols, rows, imgb.getConfig());
//Convert original image to Gray Image
for (int i=0;i<cols;i++){
for(int j=0;j<rows;j++){
int p = imgb.getPixel(i,j);
int r = Color.red(p);
int g = Color.green(p);
int b = Color.blue(p);
r = (int) (red*r);
g = (int) (green*g);
b = (int) (blue*b);
int gray = (int) (r*0.299+g*0.587+b*0.114);
operation.setPixel(i, j, Color.argb(Color.alpha(p), gray, gray, gray));
}
}
rawImage.setImageBitmap(operation);
} catch (FileNotFoundException e) {
e.printStackTrace();
}