Home Ask Login Register

Developers Planet

Your answer is one click away!

Francisco Durdin Garcia February 2016

Android: Streched Bitmap after Matrix Rotation

Im trying to rotate a JPEG data Array converting it into a Bitmap and rotating the matrix. It is Working fine but my problem appear when Im trying to rotate a 16:9 image(with 4:3 I think it happen too, but I can appreciate it so well). Cause the matrix rotate the image but dont resize it, so my bitmap is looking streched to width or height depending of the orientation that Im sending with the dataArray.

  protected Bitmap doInBackground(Void... params) {

        // Decode image in background.

        Bitmap bitmap = CameraUtil.downSample(mData, mDownSampleFactor);
        Matrix m = new Matrix();
        Log.i(TAG, "Bitmap=" + bitmap.getWidth() + "x" + bitmap.getHeight());
        if ((mOrientation != 0 || mMirror) && (bitmap != null)) {              

            if (mMirror) {

            m.setScale(1f, -1f);

            return Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),m,false);

        return bitmap;


 public static Bitmap downSample(final byte[] data, int downSampleFactor) {
        final BitmapFactory.Options opts = new BitmapFactory.Options();
        // Downsample the image
        opts.inSampleSize = downSampleFactor;

    return  BitmapFactory.decodeByteArray(data, 0, data.length, opts);

I already tried to resize the image manually, also to convert the array to a NV21 image and rotate the array to convert it later to bitmap and send it. But it is giving me problems(so slow and also sometimes it look totally green and pink(RAIMBOWS!)). I tryed to use Pre,Set and PostRotate too, but it didnt give me any differente between the tests that i made.

Thx in advance!


Francisco Durdin Garcia February 2016

I already found the way to fix it. It was a problem inside the crop method, which use a static measures to create the BitMap, I change the method to addapt depending of the aspect ratio.

 if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
            //Depending of the Height and Width we get the Aspect Ratio to scale the image before crop it
            if (bmp.getHeight() > bmp.getWidth()) {
                mAspectRatio = (float) bmp.getHeight() / (float) bmp.getWidth();
                sbmp = Bitmap.createScaledBitmap(bmp, radius, (int) (radius * mAspectRatio), false);
            } else {
                mAspectRatio = (float) bmp.getWidth() / (float) bmp.getHeight();
                sbmp = Bitmap.createScaledBitmap(bmp, (int) (radius * mAspectRatio), radius, false);

    } else {
        sbmp = bmp;
    //output Bitmap will have the final Scale of the Thumbnail, which will be radius x radius
    Bitmap output = Bitmap.createBitmap(radius,radius, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
//.....Draw canvas, etc etc

Post Status

Asked in February 2016
Viewed 2,703 times
Voted 14
Answered 1 times


Leave an answer

Quote of the day: live life