user1197941
user1197941

Reputation: 179

Saving a photo in black and white

Is it possible to save a photo taken with a camera in color mode as black and white or must the camera paramaters be changed so the viewfinder sees black and white when the picture is taken? Here is the code I am using to save an image in color, I want to save it as black and white to save space.

   PictureCallback jpegCallback = new PictureCallback() {
    @Override
        public void onPictureTaken(byte[] data, Camera camera) { 
        Log.e("Pic Taken","Callback");
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;

        options.inDither = false; // Disable Dithering mode
        options.inPurgeable = true; // Tell to gc that whether it needs free
                                    // memory, the Bitmap can be cleared
        options.inInputShareable = true; // Which kind of reference will be
                                            // used to recover the Bitmap
                                            // data after being clear, when
                                            // it will be used in the future
        options.inTempStorage = new byte[32 * 1024];
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length, options);

        int orientation;
        // others devices
        if(bMap.getHeight() < bMap.getWidth()){
            orientation = 90;
        } else {
            orientation = 0;
        }

        Bitmap bMapRotate;
        if (orientation != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(orientation);
            bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(),
                    bMap.getHeight(), matrix, true);
        } else
            bMapRotate = Bitmap.createScaledBitmap(bMap, bMap.getWidth(),
                    bMap.getHeight(), true);


        FileOutputStream out;
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
        try {
              Log.e("Saving","Pic");
        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        String fileName = "/" + System.currentTimeMillis() + ".jpg";

            out = new FileOutputStream(baseDir + fileName);

            bMapRotate.compress(Bitmap.CompressFormat.JPEG, 25, out);
            if (bMapRotate != null) {
                bMapRotate.recycle();
                bMapRotate = null;
            }


        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
};

Upvotes: 1

Views: 1791

Answers (3)

Tushar
Tushar

Reputation: 8049

You can convert an existing photo into Black and White. After:

else
        bMapRotate = Bitmap.createScaledBitmap(bMap, bMap.getWidth(),
                bMap.getHeight(), true);

Add this:

Bitmap newBitmap = Bitmap.createBitmap(bMapRotate.getWidth(), bMapRotate.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(newBitmap);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter cmFilter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(cmFilter);
canvas.drawBitmap(bMapRotate, 0, 0, paint);

And don't forget to call .recycle() on bMapRotate.

Upvotes: 1

Vyacheslav Shylkin
Vyacheslav Shylkin

Reputation: 9801

You can convert Bitmap:

public Bitmap convertToGrayScale(Bitmap original) {        
    Bitmap result = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    ColorMatrix matrix = new ColorMatrix();
    matrix.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(matrix);
    paint.setColorFilter(f);
    canvas.drawBitmap(original, 0, 0, paint);
    return result;
}

Upvotes: 4

shkschneider
shkschneider

Reputation: 18243

It should be available in the Camera.parameters.

Depending on if the device's camera supports it, you could use it.

Use this code to retrieve them:

Camera.Parameters params = cam.getParameters();
try {           
    for (String e : params.getSupportedColorEffects()) {
        Log.d(TAG, "Effect: " + e);
    }
}
catch (NullPointerException npe) {
    Log.d(TAG, "No color effects supported by this camera.");           
}

And read the documentation:

http://developer.android.com/reference/android/hardware/Camera.Parameters.html

Upvotes: 0

Related Questions