October 21, 2018
Hot Topics:

# Working with Images in Android, Page 2

## Manipulating Image Pixels in Android

Now, you can proceed to work on individual pixels. GetPixels in the android.graphics.Bitmap API is used to load the pixels into an integer array. In this example, you are tinting each image pixel by an angle in degrees. After the process, all pixels are made sure to fall within the byte range from 0 to 255. SetPixels in the android.graphics.Bitmap API is used to load the integer array onto an image. Before overwriting a bitmap image, it is always a good practice to release the old bitmap by using `Bitmap.recycle()`.

The last step is to update the screen through the ImageView variable mIV. Here is the code segment for performing the tinting process.

``````int[] pix = new int[mPhotoWidth * mPhotoHeight];
mBitmap.getPixels(pix, 0, mPhotoWidth, 0, 0, mPhotoWidth, mPhotoHeight);

double angle = (3.14159d * (double)deg) / 180.0d;
int S = (int)(256.0d * Math.sin(angle));
int C = (int)(256.0d * Math.cos(angle));

int r, g, b, index;
int RY, BY, RYY, GYY, BYY, R, G, B, Y;

for (int y = 0; y < mPhotoHeight; y++) {
for (int x = 0; x < mPhotoWidth; x++) {
index = y * mPhotoWidth + x;
r = (pix[index] >> 16) & 0xff;
g = (pix[index] >> 8) & 0xff;
b = pix[index] & 0xff;
RY = (70 * r - 59 * g - 11 * b) / 100;
BY = (-30 * r - 59 * g + 89 * b) / 100;
Y = (30 * r + 59 * g + 11 * b) / 100;
RYY = (S * BY + C * RY) / 256;
BYY = (C * BY - S * RY) / 256;
GYY = (-51 * RYY - 19 * BYY) / 100;
R = Y + RYY;
R = (R < 0) ? 0 : ((R > 255) ? 255 : R);
G = Y + GYY;
G = (G < 0) ? 0 : ((G > 255) ? 255 : G);
B = Y + BYY;
B = (B < 0) ? 0 : ((B > 255) ? 255 : B);
pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
}
}

Bitmap bm = Bitmap.createBitmap(mPhotoWidth, mPhotoHeight, Bitmap.Config.ARGB_8888);
bm.setPixels(pix, 0, mPhotoWidth, 0, 0, mPhotoWidth, mPhotoHeight);

if (null != mBitmap) {
mBitmap.recycle();
}
mBitmap = bm;

// Put the updated bitmap into the main view
mIV.setImageBitmap(mBitmap);
mIV.invalidate();

pix = null;``````

Figure 2 shows the result after a user touches the screen.

Figure 2: Result After Touching the Screen

## Putting All the Pieces Together

The previous sections explained how to import and export an image file, create a view for it, and process each pixel in the image. One thing you should add is a simple user interaction so that a sequence of events can happen in response to the user's input. `OnTouchEvent()` allows you to handle the screen touches that are key to the modern devices. The software is set to wait for the end of the screen touch event. When the touch is released, the picture will be tinted by a color angle of 60 degrees and its result will be saved onto an external storage file.

``````public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
// Perform the tinting operation
TintThePicture(60);

// Display a short message on screen
Toast.makeText(TutorialOnImages.this, "Picture was tinted", Toast.LENGTH_SHORT).show();

// Save the result
SaveThePicture();

return (true);
case MotionEvent.ACTION_DOWN:
break;
}

return super.onTouchEvent(event);
}``````

Page 2 of 3

Comment and Contribute

(Maximum characters: 1200). You have characters left.

## Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

## Most Popular Developer Stories

Thanks for your registration, follow us on our social networks to keep up-to-date