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:com.ttxgps.zoom.GestureImageView.java

@Override
public void setImageURI(Uri mUri) {
    if ("content".equals(mUri.getScheme())) {
        try {/*www  .ja v a2  s  . c  o m*/
            String[] orientationColumn = { MediaStore.Images.Media.ORIENTATION };

            Cursor cur = getContext().getContentResolver().query(mUri, orientationColumn, null, null, null);

            if (cur != null && cur.moveToFirst()) {
                imageOrientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
            }

            InputStream in = null;

            try {
                in = getContext().getContentResolver().openInputStream(mUri);
                Bitmap bmp = BitmapFactory.decodeStream(in);

                if (imageOrientation != 0) {
                    Matrix m = new Matrix();
                    m.postRotate(imageOrientation);
                    Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
                    bmp.recycle();
                    setImageDrawable(new BitmapDrawable(getResources(), rotated));
                } else {
                    setImageDrawable(new BitmapDrawable(getResources(), bmp));
                }
            } finally {
                if (in != null) {
                    in.close();
                }

                if (cur != null) {
                    cur.close();
                }
            }
        } catch (Exception e) {
            Log.w("GestureImageView", "Unable to open content: " + mUri, e);
        }
    } else {
        setImageDrawable(Drawable.createFromPath(mUri.toString()));
    }

    if (drawable == null) {
        Log.e("GestureImageView", "resolveUri failed on bad bitmap uri: " + mUri);
        // Don't try again.
        mUri = null;
    }
}

From source file:com.cloverstudio.spika.CameraCropActivity.java

protected void onPhotoTaken(String path) {

    String fileName = Uri.parse(path).getLastPathSegment();
    mFilePath = CameraCropActivity.this.getExternalCacheDir() + "/" + fileName;

    if (!path.equals(mFilePath)) {
        copy(new File(path), new File(mFilePath));
    }/*from  w  w w.  ja  v a  2s.c  o  m*/

    new AsyncTask<String, Void, byte[]>() {
        boolean loadingFailed = false;

        @Override
        protected byte[] doInBackground(String... params) {
            try {

                if (params == null)
                    return null;

                File f = new File(params[0]);
                ExifInterface exif = new ExifInterface(f.getPath());
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);

                int angle = 0;

                if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                    angle = 90;
                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                    angle = 180;
                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                    angle = 270;
                }

                Matrix mat = new Matrix();
                mat.postRotate(angle);

                BitmapFactory.Options optionsMeta = new BitmapFactory.Options();
                optionsMeta.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(f.getAbsolutePath(), optionsMeta);

                BitmapFactory.Options options = new BitmapFactory.Options();

                options.inSampleSize = BitmapManagement.calculateInSampleSize(optionsMeta, 640, 640);
                options.inPurgeable = true;
                options.inInputShareable = true;
                mBitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
                mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), mat,
                        true);
                _scaleBitmap();
                return null;
            } catch (Exception ex) {
                loadingFailed = true;
                finish();
            }

            return null;
        }

        @Override
        protected void onPostExecute(byte[] result) {
            super.onPostExecute(result);

            if (null != mBitmap) {
                mImageView.setImageBitmap(mBitmap);
                mImageView.setScaleType(ScaleType.MATRIX);
                translateMatrix.setTranslate(-(mBitmap.getWidth() - crop_container_size) / 2f,
                        -(mBitmap.getHeight() - crop_container_size) / 2f);
                mImageView.setImageMatrix(translateMatrix);

                matrix = translateMatrix;

            }

        }
    }.execute(mFilePath);

}

From source file:org.appcelerator.titanium.view.TiDrawableReference.java

private Bitmap getRotatedBitmap(Bitmap src, int orientation) {
    Matrix m = new Matrix();
    m.postRotate(orientation);
    return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, false);
}

From source file:org.mdc.chess.ChessBoard.java

private void drawMoveHints(Canvas canvas) {
    if ((moveHints == null) || blindMode) {
        return;/*from  w  ww  .  ja va  2 s  .  com*/
    }
    float h = (float) (sqSize / 2.0);
    float d = (float) (sqSize / 8.0);
    double v = 35 * Math.PI / 180;
    double cosv = Math.cos(v);
    double sinv = Math.sin(v);
    double tanv = Math.tan(v);
    int n = Math.min(moveMarkPaint.size(), moveHints.size());
    for (int i = 0; i < n; i++) {
        Move m = moveHints.get(i);
        if ((m == null) || (m.from == m.to)) {
            continue;
        }
        float x0 = getXCrd(Position.getX(m.from)) + h;
        float y0 = getYCrd(Position.getY(m.from)) + h;
        float x1 = getXCrd(Position.getX(m.to)) + h;
        float y1 = getYCrd(Position.getY(m.to)) + h;

        float x2 = (float) (Math.hypot(x1 - x0, y1 - y0) + d);
        float y2 = 0;
        float x3 = (float) (x2 - h * cosv);
        float y3 = (float) (y2 - h * sinv);
        float x4 = (float) (x3 - d * sinv);
        float y4 = (float) (y3 + d * cosv);
        float x5 = (float) (x4 + (-d / 2 - y4) / tanv);
        float y5 = -d / 2;
        float x6 = 0;
        float y6 = y5 / 2;
        Path path = new Path();
        path.moveTo(x2, y2);
        path.lineTo(x3, y3);
        //          path.lineTo(x4, y4);
        path.lineTo(x5, y5);
        path.lineTo(x6, y6);
        path.lineTo(x6, -y6);
        path.lineTo(x5, -y5);
        //          path.lineTo(x4, -y4);
        path.lineTo(x3, -y3);
        path.close();
        Matrix mtx = new Matrix();
        mtx.postRotate((float) (Math.atan2(y1 - y0, x1 - x0) * 180 / Math.PI));
        mtx.postTranslate(x0, y0);
        path.transform(mtx);
        Paint p = moveMarkPaint.get(i);
        canvas.drawPath(path, p);
    }
}

From source file:com.snappy.CameraCropActivity.java

protected void onPhotoTaken(final String path) {

    String fileName = Uri.parse(path).getLastPathSegment();
    mFilePath = CameraCropActivity.this.getExternalCacheDir() + "/" + fileName;

    if (!path.equals(mFilePath)) {
        copy(new File(path), new File(mFilePath));
    }//from w  w w. java  2  s  . c  o  m

    new AsyncTask<String, Void, byte[]>() {
        boolean loadingFailed = false;

        @Override
        protected byte[] doInBackground(String... params) {
            try {

                if (!path.equals(mFilePath)) {
                    copy(new File(path), new File(mFilePath));
                }

                if (params == null)
                    return null;

                File f = new File(params[0]);
                ExifInterface exif = new ExifInterface(f.getPath());
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);

                int angle = 0;

                if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                    angle = 90;
                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                    angle = 180;
                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                    angle = 270;
                }

                Matrix mat = new Matrix();
                mat.postRotate(angle);

                BitmapFactory.Options optionsMeta = new BitmapFactory.Options();
                optionsMeta.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(f.getAbsolutePath(), optionsMeta);

                BitmapFactory.Options options = new BitmapFactory.Options();

                options.inSampleSize = BitmapManagement.calculateInSampleSize(optionsMeta, 640, 640);
                options.inPurgeable = true;
                options.inInputShareable = true;
                mBitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
                mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), mat,
                        true);
                _scaleBitmap();
                return null;
            } catch (Exception ex) {
                loadingFailed = true;
                finish();
            }

            return null;
        }

        @Override
        protected void onPostExecute(byte[] result) {
            super.onPostExecute(result);

            if (null != mBitmap) {
                mImageView.setImageBitmap(mBitmap);
                mImageView.setScaleType(ScaleType.MATRIX);
                translateMatrix.setTranslate(-(mBitmap.getWidth() - crop_container_size) / 2f,
                        -(mBitmap.getHeight() - crop_container_size) / 2f);
                mImageView.setImageMatrix(translateMatrix);

                matrix = translateMatrix;

            }

        }
    }.execute(mFilePath);

}

From source file:com.scm.reader.livescanner.sdk.recognizers.KooabaRecognizer.java

private Bitmap fixRotation(Bitmap img) throws IOException {
    //get WINDOW_SERVICE
    Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    if (isDebugLog()) {
        logDebug("Display orientation is " + display.getOrientation());
    }//from w ww.  j a  va  2 s  . c om

    Matrix m = new Matrix();

    int angle = 0;

    //TODO: refactor
    if (img.getWidth() > img.getHeight()) {
        if (display.getOrientation() == ROTATION_0) {
            angle = 90;
        } else if (display.getOrientation() == ROTATION_270) {
            angle = 180;
        }
    } else {
        if (display.getOrientation() == ROTATION_90) {
            angle = -90;
        } else if (display.getOrientation() == ROTATION_270) {
            angle = 90;
        }
    }

    if (isDebugLog()) {
        logDebug("Preview image will be rotated by " + angle + " degrees");
    }

    m.postRotate(angle);
    return Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), m, true);
}

From source file:com.netcompss.ffmpeg4android_client.BaseVideo.java

@SuppressWarnings("unused")
private String reporteds(String path) {

    ExifInterface exif = null;//  w ww  .j av  a 2 s. c  om
    try {
        exif = new ExifInterface(path);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    Matrix matrix = new Matrix();
    if (orientation == 6) {
        matrix.postRotate(90);
    } else if (orientation == 3) {
        matrix.postRotate(180);
    } else if (orientation == 8) {
        matrix.postRotate(270);
    }

    if (path != null) {

        if (path.contains("http")) {
            try {
                URL url = new URL(path);
                HttpGet httpRequest = null;

                httpRequest = new HttpGet(url.toURI());

                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

                HttpEntity entity = response.getEntity();
                BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
                InputStream input = bufHttpEntity.getContent();

                Bitmap bitmap = BitmapFactory.decodeStream(input);
                input.close();
                return getPath(bitmap);
            } catch (MalformedURLException e) {
                Log.e("ImageActivity", "bad url", e);
            } catch (Exception e) {
                Log.e("ImageActivity", "io error", e);
            }
        } else {

            Options options = new Options();
            options.inSampleSize = 2;
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(context.getResources(), srcBgId, options);
            options.inJustDecodeBounds = false;
            options.inSampleSize = calculateInSampleSize(options, w, h);
            Bitmap unbgbtmp = BitmapFactory.decodeResource(context.getResources(), srcBgId, options);
            Bitmap unrlbtmp = ScalingUtilities.decodeFile(path, w, h, ScalingLogic.FIT);
            unrlbtmp.recycle();
            Bitmap rlbtmp = null;
            if (unrlbtmp != null) {
                rlbtmp = ScalingUtilities.createScaledBitmap(unrlbtmp, w, h, ScalingLogic.FIT);
            }
            if (unbgbtmp != null && rlbtmp != null) {
                Bitmap bgbtmp = ScalingUtilities.createScaledBitmap(unbgbtmp, w, h, ScalingLogic.FIT);
                Bitmap newscaledBitmap = ProcessingBitmapTwo(bgbtmp, rlbtmp);
                unbgbtmp.recycle();
                return getPath(newscaledBitmap);
            }
        }
    }
    return path;

}

From source file:bikebadger.RideFragment.java

private void updateUI() {

    boolean trackingLocations = mRideManager.isTrackingLocations();
    boolean trackingThisRun = mRideManager.isTrackingRun(mRide);
    boolean mRunNotNull = mRide != null;
    String msg = "";

    Log.d(Constants.APP.TAG, "RideFragment::updateUI");
    if (trackingLocations)
        Log.d(Constants.APP.TAG, "trackingLocations=" + trackingLocations);
    else// w  w w  .  j  a  v a 2 s.c o m
        Log.d(Constants.APP.TAG, "trackingLocations is FALSE");
    if (trackingThisRun)
        Log.d(Constants.APP.TAG, "trackingThisRun=" + trackingThisRun);
    else
        Log.d(Constants.APP.TAG, "trackingThisRun is FALSE");

    if (mRunNotNull)
        Log.d(Constants.APP.TAG, "mRide is NOT null!");
    else
        Log.d(Constants.APP.TAG, "mRide is NULL!");

    if (mRide != null) {
        mStartedTextView.setText(mRide.getStartDate().toString());
    }

    int durationSeconds = 0;

    if (mRide != null && mLastLocation != null) {
        Log.d(Constants.APP.TAG, "mRide != null && mLastLocation != null");
        mMessagebarView.setTextColor(Color.WHITE);
        //String msg = "mRide != null && mLastLocation != null";
        double bearing1 = mLastLocation.getBearing();
        double bearing2 = mRideManager.mClosestBearing;
        double bearingDif = bearing2 - bearing1;
        if (mRideManager.mClosestWaypoint != null) {
            Log.d(Constants.APP.TAG, "mRideManager.mClosestWaypoint != null");

            if (mRideManager.mClosestWaypoint.IsTriggered()) {
                mMessagebarView.setTextColor(Color.RED);
                msg = "\"" + mRideManager.mClosestWaypoint.getName() + "\"";
                msg += " (Active)";
                Bitmap activeBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_triggered);
                mArrowView.setImageBitmap(activeBitmap);
            } else {
                mMessagebarView.setTextColor(Color.WHITE);
                msg = "\"" + mRideManager.mClosestWaypoint.getName() + "\"";
                msg += " at ";
                msg += Formatter.FormatDistanceMiles(mRideManager.mClosestDistance);
                //msg += " ft";
                //msg += " (" + Formatter.FormatDecimal(bearing1) + "->";
                // msg += Formatter.FormatDecimal(bearing2) + ":" + Formatter.FormatDecimal(bearingDif);
                // Point the arrow to the bearing of the closest waypoint...
                Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_white);
                Matrix matrix = new Matrix();
                matrix.postRotate((float) (bearingDif));
                final int width = myBitmap.getWidth();
                final int height = myBitmap.getHeight();
                Log.d(Constants.APP.TAG, "myBitmap.getWidth=" + myBitmap.getWidth() + "myBitmap.getHeight="
                        + myBitmap.getHeight());
                Bitmap rotatedBitmap = Bitmap.createBitmap(myBitmap, 0, 0, width, height, matrix, true);
                mArrowView.setImageBitmap(rotatedBitmap);
            }
        }

        // set the top message line
        mMessagebarView.setText(msg);

        durationSeconds = mRide.StopwatchSeconds();
        mSpeedTextView.setText(Formatter.FormatDecimal(mLastLocation.getSpeed() * Ride.METERS_TO_MILES));
        mTargetSpeedTextView.setText(Formatter.FormatDecimal(mRide.GetTargetAvgSpeed()));
        mAverageSpeedTextView.setText(Formatter.FormatDecimal(mRide.GetAverageSpeed()));
        mDurationTextView.setText(Formatter.FormatDuration(durationSeconds));

    } else if (mRide == null && mLastLocation == null) {
        Log.d(Constants.APP.TAG, "mRide == null && mLastLocation == null");
        mTargetSpeedTextView.setText(Formatter.FormatDecimal(mRideManager.GetDefaultTargetAvgSpeed()));

        mMessagebarView.setTextColor(Color.WHITE);
        if (mRideManager.WaypointsLoaded()) {
            mMessagebarView.setText("\"" + mRideManager.GetGPXFileName() + "\" loaded ("
                    + mRideManager.mWaypoints.size() + ")");
        }
    } else if (mRide != null && mLastLocation == null) { // mLastLocation is null
        mMessagebarView.setTextColor(Color.RED);
        mMessagebarView.setText("Waiting on GPS...");
    } else if (mRide == null && mLastLocation != null) {
        Log.d(Constants.APP.TAG, "mRide == null && mLastLocation != null");
    }

    if (!trackingThisRun) {
        mStartStopButton.setBackgroundResource(R.drawable.ic_button_white_play);
        mStartStopButton.setOnClickListener(mPlayButtonClickListener);
    }

    if (trackingThisRun) {
        mStartStopButton.setBackgroundResource(R.drawable.ic_button_white_pause);
        mStartStopButton.setOnClickListener(mPauseButtonClickListener);
    }

    //mStartButton.setEnabled(!started);
    mResetButton.setEnabled(trackingLocations && trackingThisRun);
    mWaypointButton.setEnabled(trackingLocations);
    //mTargetEditButton.setEnabled(trackingLocations && trackingThisRun);
}

From source file:com.bai.android.ui.OtherActivity.java

private void rotateImageIfNecessary(int rotation) {
    if (rotation != 0) {
        // if image was stored rotated - rotate to normal
        Matrix matrix = new Matrix();
        matrix.postRotate(rotation);

        avatarPicture = Bitmap.createBitmap(avatarPicture, 0, 0, avatarPicture.getWidth(),
                avatarPicture.getHeight(), matrix, true);
    } else {//w w w.  ja  v a  2 s. c om
        // not necessary to rotate  image
        //avatarPicture = Bitmap.createScaledBitmap(avatarPicture, avatarPicture.getWidth(), avatarPicture.getHeight(), true);
    }

}

From source file:com.sim2dial.dialer.ChatFragment.java

private void uploadAndSendImage(final String filePath, final Bitmap image, final ImageSize size) {
    uploadLayout.setVisibility(View.VISIBLE);
    textLayout.setVisibility(View.GONE);

    uploadThread = new Thread(new Runnable() {
        @Override/*from  w w  w .j  av  a2 s.c o  m*/
        public void run() {
            Bitmap bm = null;
            String url = null;

            if (!uploadThread.isInterrupted()) {
                if (filePath != null) {
                    bm = BitmapFactory.decodeFile(filePath);
                    if (bm != null && size != ImageSize.REAL) {
                        int pixelsMax = size == ImageSize.SMALL ? SIZE_SMALL
                                : size == ImageSize.MEDIUM ? SIZE_MEDIUM : SIZE_LARGE;
                        if (bm.getWidth() > bm.getHeight() && bm.getWidth() > pixelsMax) {
                            bm = Bitmap.createScaledBitmap(bm, pixelsMax,
                                    (pixelsMax * bm.getHeight()) / bm.getWidth(), false);
                        } else if (bm.getHeight() > bm.getWidth() && bm.getHeight() > pixelsMax) {
                            bm = Bitmap.createScaledBitmap(bm, (pixelsMax * bm.getWidth()) / bm.getHeight(),
                                    pixelsMax, false);
                        }
                    }
                } else if (image != null) {
                    bm = image;
                }
            }

            // Rotate the bitmap if possible/needed, using EXIF data
            try {
                if (filePath != null) {
                    ExifInterface exif = new ExifInterface(filePath);
                    int pictureOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
                    Matrix matrix = new Matrix();
                    if (pictureOrientation == 6) {
                        matrix.postRotate(90);
                    } else if (pictureOrientation == 3) {
                        matrix.postRotate(180);
                    } else if (pictureOrientation == 8) {
                        matrix.postRotate(270);
                    }
                    bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            if (bm != null) {
                bm.compress(CompressFormat.JPEG, COMPRESSOR_QUALITY, outStream);
            }

            if (!uploadThread.isInterrupted() && bm != null) {
                url = uploadImage(filePath, bm, COMPRESSOR_QUALITY, outStream.size());
                File file = new File(Environment.getExternalStorageDirectory(),
                        getString(R.string.temp_photo_name));
                file.delete();
            }

            if (!uploadThread.isInterrupted()) {
                final Bitmap fbm = bm;
                final String furl = url;
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        uploadLayout.setVisibility(View.GONE);
                        textLayout.setVisibility(View.VISIBLE);
                        progressBar.setProgress(0);
                        if (furl != null) {
                            sendImageMessage(furl, fbm);
                        } else {
                            Toast.makeText(getActivity(), getString(R.string.error), Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        }
    });
    uploadThread.start();
}