Example usage for android.graphics Matrix postRotate

List of usage examples for android.graphics Matrix postRotate

Introduction

In this page you can find the example usage for android.graphics Matrix postRotate.

Prototype

public boolean postRotate(float degrees) 

Source Link

Document

Postconcats the matrix with the specified rotation.

Usage

From source file:org.exoplatform.utils.ExoDocumentUtils.java

/**
 * Rotate the bitmap by a certain angle. Uses {@link Matrix#postRotate(int)}
 * //from   ww  w.j  a va 2  s  .c om
 * @param source the bitmap to rotate
 * @param angle the rotation angle
 * @return a new rotated bitmap
 */
public static Bitmap rotateBitmapByAngle(Bitmap source, int angle) {
    Bitmap ret = source;
    int w, h;
    w = source.getWidth();
    h = source.getHeight();
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    try {
        ret = Bitmap.createBitmap(source, 0, 0, w, h, matrix, true);
    } catch (OutOfMemoryError e) {
        Log.d(ExoDocumentUtils.class.getSimpleName(), "Exception : ", e, Log.getStackTraceString(e));
    }
    return ret;
}

From source file:com.example.d062654.faciliman._3f_FacilityDetailed.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ll = (RelativeLayout) inflater.inflate(R.layout.facilitydetailed, container, false);
    facarchive = (Button) ll.findViewById(R.id.facarchive);

    factitle = (TextView) ll.findViewById(R.id.factitle);
    factitle.setText(incident.getTitle());
    facplace = (TextView) ll.findViewById(R.id.facplace);
    facplace.setText(incident.getLocation());
    facdetailed_location = (TextView) ll.findViewById(R.id.facdetailed_location);
    facdetailed_location.setText(incident.getExactLocation());
    facdetailed_description = (TextView) ll.findViewById(R.id.facdetailed_description);
    facdetailed_description.setText("Lampe Kaputt");
    facimage = (ImageView) ll.findViewById(R.id.facimage);
    facarchive.setOnClickListener(new View.OnClickListener() {
        @Override//from   w  ww  .j a  va  2 s .c  o m
        public void onClick(View view) {
            Call<ResponseBody> call = Connection.getApiInterface().archieveIncident(user, incident.getId());
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    if (response.isSuccessful()) {
                        Toast.makeText(fragact.getApplicationContext(), "Archivierungsvorgang abgeschlossen",
                                Toast.LENGTH_SHORT).show();
                        facarchive.setEnabled(false);
                    } else if (response.code() == 401) {
                        // Handle unauthorized
                        Toast.makeText(fragact.getApplicationContext(), "Sorry! Failed to capture image",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        // Handle other responses
                        Toast.makeText(fragact.getApplicationContext(), "Sorry! Failed to capture image",
                                Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {

                }
            });

        }
    });

    Call<ResponseBody> call = Connection.getApiInterface().getFile(user, incident.getId());
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()) {
                // Do awesome stuff
                Bitmap bmp = null;

                try {
                    byte[] imgbytes = response.body().bytes();
                    int targetW = facimage.getWidth();
                    int targetH = facimage.getHeight();
                    // bimatp factory
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;

                    bmp = BitmapFactory.decodeByteArray(imgbytes, 0, imgbytes.length, options);

                    int photoW = options.outWidth;
                    int photoH = options.outHeight;
                    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
                    options.inJustDecodeBounds = false;
                    options.inSampleSize = scaleFactor;
                    options.inPurgeable = true;

                    bmp = BitmapFactory.decodeByteArray(imgbytes, 0, imgbytes.length, options);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ImageView image = (ImageView) ll.findViewById(R.id.facimage);

                Matrix matrix = new Matrix();

                matrix.postRotate(90);

                Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmp, bmp.getWidth(), bmp.getHeight(), true);

                Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(),
                        scaledBitmap.getHeight(), matrix, true);
                facimage.setImageBitmap(rotatedBitmap);

            } else if (response.code() == 401) {
                // Handle unauthorized
                Toast.makeText(fragact.getApplicationContext(), "Sorry! Failed to capture image",
                        Toast.LENGTH_SHORT).show();
            } else {
                // Handle other responses
                Toast.makeText(fragact.getApplicationContext(), "Sorry! Failed to capture image",
                        Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            // Log error here since request failed
            Log.e(TAG, t.toString());

        }
    });
    // Inflate the layout for this fragment
    return ll;
}

From source file:br.com.viniciuscr.notification2android.mediaPlayer.MusicUtils.java

static void setBackground(View v, Bitmap bm) {

    if (bm == null) {
        v.setBackgroundResource(0);/*  w  w w .  ja v a2  s  .  co  m*/
        return;
    }

    int vwidth = v.getWidth();
    int vheight = v.getHeight();
    int bwidth = bm.getWidth();
    int bheight = bm.getHeight();
    float scalex = (float) vwidth / bwidth;
    float scaley = (float) vheight / bheight;
    float scale = Math.max(scalex, scaley) * 1.3f;

    Bitmap.Config config = Bitmap.Config.ARGB_8888;
    Bitmap bg = Bitmap.createBitmap(vwidth, vheight, config);
    Canvas c = new Canvas(bg);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    ColorMatrix greymatrix = new ColorMatrix();
    greymatrix.setSaturation(0);
    ColorMatrix darkmatrix = new ColorMatrix();
    darkmatrix.setScale(.3f, .3f, .3f, 1.0f);
    greymatrix.postConcat(darkmatrix);
    ColorFilter filter = new ColorMatrixColorFilter(greymatrix);
    paint.setColorFilter(filter);
    Matrix matrix = new Matrix();
    matrix.setTranslate(-bwidth / 2, -bheight / 2); // move bitmap center to origin
    matrix.postRotate(10);
    matrix.postScale(scale, scale);
    matrix.postTranslate(vwidth / 2, vheight / 2); // Move bitmap center to view center
    c.drawBitmap(bm, matrix, paint);
    v.setBackgroundDrawable(new BitmapDrawable(bg));
}

From source file:com.bilibili.boxing.utils.CameraPickerHelper.java

private boolean rotateSourceFile(File file) throws IOException {
    if (file == null || !file.exists()) {
        return false;
    }//from  w  w  w . jav a2  s  .  c  o m
    FileOutputStream outputStream = null;
    Bitmap bitmap = null;
    Bitmap outBitmap = null;
    try {
        int degree = BoxingExifHelper.getRotateDegree(file.getAbsolutePath());
        Matrix matrix = new Matrix();
        if (degree != 0) {
            matrix.postRotate(degree);
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        outBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        outputStream = new FileOutputStream(file);
        outBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.flush();
        return true;
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                BoxingLog.d("IOException when output stream closing!");
            }
        }
        if (bitmap != null) {
            bitmap.recycle();
        }
        if (outBitmap != null) {
            outBitmap.recycle();
        }
    }
}

From source file:org.que.activities.fragments.MapImageFragment.java

/**
 * Rotates the bitmap and returns the rotated bitmap, uses the drawable
 * resource id to create a bitmap.//from   ww  w  .j a  v  a2 s.  c om
 * 
 * @return the rotated bitmap
 */
private Bitmap rotatedImage() {
    Matrix m = new Matrix();
    m.postRotate(270);

    Bitmap b = BitmapFactory.decodeResource(getResources(), imgRes);
    Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
    if (b != b2) {
        b.recycle();
    }
    return b2;
}

From source file:com.example.d062654.faciliman._2_IncidentPicture.java

private void previewCapturedImage(Intent data) {
    try {//ww  w . j ava 2  s . com

        //Bundle extras = data.getExtras();
        imgPreview.setVisibility(View.VISIBLE);
        int targetW = imgPreview.getWidth();
        int targetH = imgPreview.getHeight();
        // bimatp factory
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mCurrentPhotoPath, options);
        int photoW = options.outWidth;
        int photoH = options.outHeight;
        int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
        options.inJustDecodeBounds = false;
        options.inSampleSize = scaleFactor;
        options.inPurgeable = true;
        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, options);
        Matrix matrix = new Matrix();

        matrix.postRotate(90);

        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);

        Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(),
                scaledBitmap.getHeight(), matrix, true);
        imgPreview.setImageBitmap(rotatedBitmap);

    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

From source file:com.fractview.ImageViewFragment.java

private void initImageMatrix() {
    // Set initMatrix + inverse to current view-size
    float vw = imageView.getWidth();
    float vh = imageView.getHeight();

    // Right after creation, this view might not 
    // be inside anything and therefore have size 0.
    if (vw <= 0 && vh <= 0)
        return; // do nothing.

    float bw = taskFragment.bitmap().getWidth();
    float bh = taskFragment.bitmap().getHeight();

    RectF viewRect = new RectF(0f, 0f, vw, vh);
    RectF bitmapRect;/* ww  w. jav a2  s  . c o  m*/

    if (vw > vh) {
        // if width of view is bigger, match longer side to it
        bitmapRect = new RectF(0f, 0f, Math.max(bw, bh), Math.min(bw, bh));
    } else {
        bitmapRect = new RectF(0f, 0f, Math.min(bw, bh), Math.max(bw, bh));
    }

    viewToImageMatrix.setRectToRect(bitmapRect, viewRect, viewPolicy);

    if (vw > vh ^ bw > bh) {
        // Turn centerImageMatrix by 90 degrees
        Matrix m = new Matrix();
        m.postRotate(90f);
        m.postTranslate(bh, 0);

        viewToImageMatrix.preConcat(m);
    }

    if (!viewToImageMatrix.invert(inverseViewToImageMatrix)) {
        throw new IllegalArgumentException("matrix cannot be inverted...");
    }

    imageView.setImageMatrix(viewToImageMatrix);
}

From source file:com.example.user.lstapp.CreatePlaceFragment.java

private byte[] processPhoto(byte[] data) {
    Log.d(TAG, "picture processing");
    Bitmap originalImg = BitmapFactory.decodeByteArray(data, 0, data.length);

    //resize image
    Bitmap resizedImg = Bitmap.createScaledBitmap(originalImg, (int) (originalImg.getWidth() * 0.2),
            (int) (originalImg.getHeight() * 0.2), true);
    //rotate image to match view for user
    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    Bitmap rotatedImg = Bitmap.createBitmap(resizedImg, 0, 0, resizedImg.getWidth(), resizedImg.getHeight(),
            matrix, true);//from   w  w w.  j  av a  2s .c o  m
    //convert to byte format for sending to server
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    rotatedImg.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

From source file:mobi.tattu.utils.image.ImageWorker.java

/**
 * Get the correctly oriented bitmap if orientation is different fro 0 value Idea and code from
 * http://stackoverflow.com/a/11081918/527759
 * /*from   ww w.  j  a v  a  2  s.  c  o m*/
 * @param bitmap
 *            - bitmap to orient properly
 * @param orientation
 *            - rotation degrees
 * @return
 */
public Bitmap getCorrectlyOrientedBitmap(Bitmap bitmap, int orientation) {
    if (orientation != 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(orientation);

        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
    return bitmap;
}

From source file:com.ferid.app.frequentcontacts.MainActivity.java

/**
 * After photo process return//from  www .  j a v  a 2  s .  c  o  m
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE) {
        //a photo picked from the gallery
        if (resultCode == RESULT_OK) {
            try {
                Uri photoUri = data.getData();

                InputStream is = this.getContentResolver().openInputStream(photoUri);
                BitmapFactory.Options dbo = new BitmapFactory.Options();
                dbo.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(is, null, dbo);
                if (is != null)
                    is.close();

                int rotatedWidth, rotatedHeight;
                int orientation = getOrientation(this, photoUri);

                if (orientation == 90 || orientation == 270) {
                    rotatedWidth = dbo.outHeight;
                    rotatedHeight = dbo.outWidth;
                } else {
                    rotatedWidth = dbo.outWidth;
                    rotatedHeight = dbo.outHeight;
                }

                Bitmap srcBitmap;
                is = this.getContentResolver().openInputStream(photoUri);
                int MAX_IMAGE_HEIGHT = 250;
                int MAX_IMAGE_WIDTH = 250;
                if (rotatedWidth > MAX_IMAGE_WIDTH || rotatedHeight > MAX_IMAGE_HEIGHT) {
                    float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_WIDTH);
                    float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_HEIGHT);
                    float maxRatio = Math.max(widthRatio, heightRatio);

                    // Create the bitmap from file
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = (int) maxRatio;
                    srcBitmap = BitmapFactory.decodeStream(is, null, options);
                } else {
                    srcBitmap = BitmapFactory.decodeStream(is);
                }
                if (is != null)
                    is.close();

                //if the orientation is not 0 (or -1, which means we don't know), we have to do a rotation.
                if (orientation > 0) {
                    Matrix matrix = new Matrix();
                    matrix.postRotate(orientation);

                    srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
                            srcBitmap.getHeight(), matrix, true);
                }

                //srcBitmap is ready now
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byte[] byteArray = stream.toByteArray();

                // byte[] to String convert.
                String encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);

                //anti-memory leak
                srcBitmap.recycle();

                //ready to set photo
                contact.setPhoto(encodedImage);
                save_refresh();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else if (requestCode == SELECT_NUMBER) {
        //a contact selected from the list
        if (resultCode == RESULT_OK) {
            try {
                Contact contact = (Contact) data.getSerializableExtra("contact");
                if (contact != null) {
                    contactsList.add(contact);
                    save_refresh();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}