Skip to content

Instantly share code, notes, and snippets.

@mancdevcarl
Last active August 29, 2015 14:13
Show Gist options
  • Save mancdevcarl/9f0d3230b4c381ce7a4d to your computer and use it in GitHub Desktop.
Save mancdevcarl/9f0d3230b4c381ce7a4d to your computer and use it in GitHub Desktop.
Simple Green Screen images for Android
//This demo app Assumes there are 2 jpg files on the sdcard in the root directory
public class MainActivity extends ActionBarActivity {
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap greenBitmap = BitmapFactory.decodeFile(path + "/green.jpg",
options);
Bitmap bkgBitmap = BitmapFactory.decodeFile(path + "/bkg.jpg", options);
Bitmap[] arr = new Bitmap[] { greenBitmap, bkgBitmap };
new Async().execute(arr);
}
private class Async extends AsyncTask<Bitmap[], Void, Bitmap> {
@Override
protected Bitmap doInBackground(Bitmap[]... params) {
Bitmap newBitmap = null;
try {
newBitmap = getCompositedImage(params[0]);
} catch (Exception e) {
e.printStackTrace();
}
return newBitmap;
}
@Override
protected void onPostExecute(Bitmap newBitmap) {
super.onPostExecute(newBitmap);
img.setImageBitmap(newBitmap);
}
}
private static Bitmap getCompositedImage(Bitmap[] images) throws Exception {
if (images[0].getWidth() != images[1].getWidth()
&& images[0].getHeight() != images[1].getHeight())
throw new Exception("The images are different sized...");
Bitmap compositeBitmap = Bitmap.createBitmap(images[0].getWidth(),
images[0].getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(compositeBitmap);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
for (int x = 0; x < canvas.getWidth(); x++) {
for (int y = 0; y < canvas.getHeight(); y++) {
int greenScreenPixel = images[0].getPixel(x, y);
int bgPixel = images[1].getPixel(x, y);
int pixelGreenVal = Color.green(greenScreenPixel);
int pixelRedVal = Color.red(greenScreenPixel);
int pixelBlueVal = Color.blue(greenScreenPixel);
if (pixelGreenVal > pixelRedVal + pixelBlueVal) {
paint.setColor(bgPixel);
canvas.drawPoint(x, y, paint);
} else {
paint.setColor(greenScreenPixel);
canvas.drawPoint(x, y, paint);
}
}
}
return compositeBitmap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment