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.jraf.android.piclabel.app.form.FormActivity.java

private Uri processAndSaveImage() throws Exception {
    BitmapFactory.Options options = new BitmapFactory.Options();

    Bitmap bitmap = BitmapUtil.tryDecodeFile(mImageFile, options);
    if (bitmap == null) {
        Log.w(TAG, "processImage Could not decode file, returning null");
        return null;
    }//from   w  w w.  j  a v a2s .c om

    // Rotate if necessary
    int rotation = BitmapUtil.getExifRotation(mImageFile);
    if (rotation != 0) {
        if (Config.LOGD)
            Log.d(TAG, "processImage rotating bitmap");
        Matrix matrix = new Matrix();
        matrix.postRotate(rotation);
        Bitmap rotatedBitmap = null;
        try {
            rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                    false);
            bitmap.recycle();
            bitmap = rotatedBitmap;
        } catch (OutOfMemoryError exception) {
            Log.w(TAG, "processImage Could not rotate bitmap, keeping original orientation", exception);
        }
    }

    // To draw text we need a mutable bitmap
    try {
        bitmap = BitmapUtil.asImmutable(bitmap);
    } catch (Throwable t) {
        throw new Exception(t);
    }
    Canvas canvas = new Canvas(bitmap);

    // Draw text
    drawText(canvas);

    // Save the new bitmap to a file
    File processedImageFile = createProcessedFile();
    try {
        saveBitmap(bitmap, mImageFile, processedImageFile);
        bitmap.recycle();
    } catch (IOException e) {
        Log.e(TAG, "processImage Could not save bitmap, returning null", e);
        return null;
    }

    // Scan it
    return MediaScannerUtil.scanFileNow(this, processedImageFile);
}

From source file:com.example.photoremark.MainActivity.java

/**
 * ?/* w  w  w  . java  2  s  . c  o  m*/
 *
 * @param filepath ?
 * @param options  BitmapFactory.Options
 * @return
 */
public Bitmap scalePicture(String filepath, BitmapFactory.Options options, int screenWidth, int scaleHeight) {
    // ?
    int rotate = getPictureDegree(filepath);
    Bitmap bm = null;
    try {
        bm = BitmapFactory.decodeFile(filepath, options);
    } catch (OutOfMemoryError err) {

    }
    // ?
    if (bm != null) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 
        float scaleWidth = ((float) screenWidth) / width;
        Matrix matrix = new Matrix();
        // 
        matrix.postScale(scaleWidth, scaleWidth);
        //  
        matrix.postRotate(rotate);
        bm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
    }
    return bm;
}

From source file:com.zzti.fyg.widgets.SwipeProgressBar.java

private void drawBar(Canvas canvas, int height) {
    mPaint.setAntiAlias(true);//from w  w  w . j  av a  2 s .c  o m
    Bitmap bitmap = null;
    if (state == FINISHED) {
        bitmap = getFinishedBitmap();
    } else {
        bitmap = getBitmap();
    }

    Matrix matrix = new Matrix();
    matrix.setTranslate(bitmap.getWidth() >> 1, bitmap.getHeight() >> 1);

    if (mRunning) {
        long now = AnimationUtils.currentAnimationTimeMillis();
        matrix.postRotate((now - mStartTime) % 360);
    } else {
        if (type == ROTATE) {
            if (state != FINISHED) {
                matrix.postRotate(height % 360);
            }
        } else {
            if (direction == 0) {
                if (state == RELEASE_TO_REFRESH) {
                    matrix.postRotate(180);
                }

            } else if (direction == 1) {
                if (state == PULL_TO_REFRESH) {
                    matrix.postRotate(180);
                }
            }
        }

    }

    dstbmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    int originalWidth = bitmap.getWidth();
    int originalHeight = bitmap.getHeight();

    int dstWidth = dstbmp.getWidth();
    int dstHeight = dstbmp.getHeight();

    int fontHeight = (int) DisplayUtil.getFontHeight(textSize);
    mPaint.setTextSize(textSize);
    mPaint.setColor(textColor);
    if (direction == 0) {
        //canvas.drawBitmap(dstbmp, (width >> 1) - ((int)mPaint.measureText(latestRefreshTime) >> 1) - drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)), (mBounds.bottom - (originalHeight >> 1)) + ((originalHeight >> 1) - (dstHeight >> 1)) - (fontHeight << 1) , mPaint);
        canvas.drawBitmap(dstbmp, drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)),
                (mBounds.bottom - (originalHeight >> 1)) + ((originalHeight >> 1) - (dstHeight >> 1))
                        - (fontHeight << 1),
                mPaint);
        mPaint.setFakeBoldText(true);

        canvas.drawText(tips, (width >> 1) - ((int) mPaint.measureText(tips) >> 1),
                (mBounds.bottom - (fontHeight << 1)), mPaint);
        mPaint.setFakeBoldText(false);
        canvas.drawText(latestRefreshTime, (width >> 1) - ((int) mPaint.measureText(latestRefreshTime) >> 1),
                (mBounds.bottom - fontHeight), mPaint);
    } else {
        float y = mBounds.top + ((textMarginTop + (fontHeight << 1)) >> 1) + textMarginTop - (dstHeight >> 1);
        //          canvas.drawBitmap(dstbmp, (width >> 1) - ((int)mPaint.measureText(latestRefreshTime) >> 1) - drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)), y , mPaint);
        canvas.drawBitmap(dstbmp, drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)), y, mPaint);
        mPaint.setFakeBoldText(true);
        canvas.drawText(tips, (width >> 1) - ((int) mPaint.measureText(tips) >> 1),
                (mBounds.top + textMarginTop + fontHeight), mPaint);
        mPaint.setFakeBoldText(false);
        canvas.drawText(latestRefreshTime, (width >> 1) - ((int) mPaint.measureText(latestRefreshTime) >> 1),
                (mBounds.top + (fontHeight << 1) + (textMarginTop << 1)), mPaint);
    }

}

From source file:com.limxing.library.PullToRefresh.SwipeProgressBar.java

private void drawBar(Canvas canvas, int height) {
    mPaint.setAntiAlias(true);/*from  w  ww .ja  v a 2 s.  c o m*/
    Bitmap bitmap = null;
    if (state == FINISHED) {
        bitmap = getFinishedBitmap();
    } else {
        bitmap = getBitmap();
    }

    Matrix matrix = new Matrix();
    matrix.setTranslate(bitmap.getWidth() >> 1, bitmap.getHeight() >> 1);

    if (mRunning) {
        long now = AnimationUtils.currentAnimationTimeMillis();
        matrix.postRotate(((now - mStartTime) % 360) / 2);
    } else {
        if (type == ROTATE) {
            if (state != FINISHED) {
                matrix.postRotate(height % 360);
            }
        } else {
            if (direction == 0) {
                if (state == RELEASE_TO_REFRESH) {
                    matrix.postRotate(180);
                }

            } else if (direction == 1) {
                if (state == PULL_TO_REFRESH) {
                    matrix.postRotate(180);
                }
            }
        }

    }

    dstbmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    int originalWidth = bitmap.getWidth();
    int originalHeight = bitmap.getHeight();

    int dstWidth = dstbmp.getWidth();
    int dstHeight = dstbmp.getHeight();

    int fontHeight = (int) DisplayUtil.getFontHeight(textSize);
    mPaint.setTextSize(textSize);
    mPaint.setColor(textColor);
    if (direction == 0) {
        // canvas.drawBitmap(dstbmp, (width >> 1) -
        // ((int)mPaint.measureText(latestRefreshTime) >> 1) -
        // drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)),
        // (mBounds.bottom - (originalHeight >> 1)) + ((originalHeight >> 1)
        // - (dstHeight >> 1)) - (fontHeight << 1) , mPaint);
        canvas.drawBitmap(dstbmp, drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)),
                (mBounds.bottom - (originalHeight >> 1)) + ((originalHeight >> 1) - (dstHeight >> 1))
                        - (fontHeight << 1),
                mPaint);
        mPaint.setFakeBoldText(true);

        canvas.drawText(tips, (width >> 1) - ((int) mPaint.measureText(tips) >> 1),
                (mBounds.bottom - (fontHeight << 1)), mPaint);
        mPaint.setFakeBoldText(false);
        canvas.drawText(latestRefreshTime, (width >> 1) - ((int) mPaint.measureText(latestRefreshTime) >> 1),
                (mBounds.bottom - fontHeight), mPaint);
    } else {
        float y = mBounds.top + ((textMarginTop + (fontHeight << 1)) >> 1) + textMarginTop - (dstHeight >> 1);
        // canvas.drawBitmap(dstbmp, (width >> 1) -
        // ((int)mPaint.measureText(latestRefreshTime) >> 1) -
        // drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)),
        // y , mPaint);
        canvas.drawBitmap(dstbmp, drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)), y, mPaint);
        mPaint.setFakeBoldText(true);
        canvas.drawText(tips, (width >> 1) - ((int) mPaint.measureText(tips) >> 1),
                (mBounds.top + textMarginTop + fontHeight), mPaint);
        mPaint.setFakeBoldText(false);
        canvas.drawText(latestRefreshTime, (width >> 1) - ((int) mPaint.measureText(latestRefreshTime) >> 1),
                (mBounds.top + (fontHeight << 1) + (textMarginTop << 1)), mPaint);
    }

}

From source file:org.apache.cordova.todataurl.ToDataURL.java

/**
 * Executes the request and returns PluginResult.
 *
 * @param action         The action to execute.
 * @param args           JSONArry of arguments for the plugin.
 * @param callbackContext   The callback id used when calling back into JavaScript.
 * @return              True if the action was valid, false otherwise.
 *//*  www.j  ava  2  s.c  om*/
@Override
public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext)
        throws JSONException {
    Log.v(LOG_TAG, "Executing action: " + action);

    if ("getImageData".equals(action)) {
        try {
            // String encodedData = args.getString (0).replaceFirst ("^data:image/(png|jpg|jpeg);base64,", "");
            // Log.i(LOG_TAG, "getImageData[" + encodedData.length () + "] = " + encodedData);

            byte[] data = Base64.decode(
                    args.getString(0).replaceFirst("^data:image/(png|jpg|jpeg);base64,", ""), Base64.DEFAULT);// args.getArrayBuffer (0);
            /*
            int width = args.getInt (1);
            int height = args.getInt (2);
            */
            String type = args.optString(1, "image/png");
            int quality = args.optInt(2, 100);
            int orientation = args.optInt(3, 0);

            // Log.i(LOG_TAG, "getImageData[" + type + "][" + quality + "] = " + width + "x" + height);
            // Log.i(LOG_TAG, "getImageData[" + data.length + "] = " + data);

            Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
            /*
            bmp = Bitmap.createBitmap (width, height, Bitmap.Config.ARGB_8888);
            bmp.copyPixelsFromBuffer (ByteBuffer.wrap (data));
            */
            // Log.i(LOG_TAG, "getImageData::bmp = " + bmp);

            try {
                final Matrix bitmapMatrix = new Matrix();

                switch (orientation) {
                case 1:
                    break; // top left
                case 2:
                    bitmapMatrix.postScale(-1, 1);
                    break; // top right
                case 3:
                    bitmapMatrix.postRotate(180);
                    break; // bottom right
                case 4:
                    bitmapMatrix.postRotate(180);
                    bitmapMatrix.postScale(-1, 1);
                    break; // bottom left
                case 5:
                    bitmapMatrix.postRotate(90);
                    bitmapMatrix.postScale(-1, 1);
                    break; // left top
                case 6:
                    bitmapMatrix.postRotate(90);
                    break; // right top
                case 7:
                    bitmapMatrix.postRotate(270);
                    bitmapMatrix.postScale(-1, 1);
                    break; // right bottom
                case 8:
                    bitmapMatrix.postRotate(270);
                    break; // left bottom
                default:
                    break; // Unknown
                }

                // Create new bitmap.
                bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), bitmapMatrix, false);

                ByteArrayOutputStream out = new ByteArrayOutputStream();
                bmp.compress((type.endsWith("jpeg") ? Bitmap.CompressFormat.JPEG : Bitmap.CompressFormat.PNG),
                        quality, out);

                String dataURL = "data:" + type + ";base64," + Base64.encodeToString(out.toByteArray(), 0);
                // Log.i(LOG_TAG, "getImageData::dataURL = " + dataURL);

                PluginResult r = new PluginResult(PluginResult.Status.OK, dataURL);
                callbackContext.sendPluginResult(r);

                return true;
            } catch (Exception e) {
                // TODO: handle exception
                callbackContext.error("Create Bitmap Matrix");

                PluginResult r = new PluginResult(PluginResult.Status.ERROR);
                callbackContext.sendPluginResult(r);

                return true;
            }
        } catch (IllegalArgumentException e) {
            callbackContext.error("Illegal Argument Exception");

            PluginResult r = new PluginResult(PluginResult.Status.ERROR);
            callbackContext.sendPluginResult(r);

            return true;
        }
        /*
        PluginResult r = new PluginResult (PluginResult.Status.NO_RESULT);
        r.setKeepCallback (true);
        callbackContext.sendPluginResult (r);
                
        return true;
        */
    }

    return false;
}

From source file:com.marshalchen.common.uimodule.modifysys.PagerTitleStrip.java

SpannableString changeSpanString(String temp, int position, int color) {

    try {//  w  w w .j av a  2 s .c  o m
        temp = temp.replaceAll("-", " - ");

        SpannableString ss = new SpannableString(temp);
        Matrix matrix = new Matrix();

        matrix.postRotate(180);

        Bitmap bitmaporg = BitmapFactory.decodeResource(getResources(), R.drawable.ico_arrow2x);
        if (color == 2) {
            bitmaporg = BitmapFactory.decodeResource(getResources(), R.drawable.ico_arrow_grey2x);
        }
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmaporg, 0, 0, bitmaporg.getWidth(), bitmaporg.getHeight(),
                matrix, true);
        BitmapDrawable bmd = new BitmapDrawable(getResources(), resizedBitmap);
        BitmapDrawable bm = new BitmapDrawable(getResources(), bitmaporg);
        Drawable drawable = position == 0 ? bm : bmd;
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth() * 12 / 10,
                drawable.getIntrinsicHeight() * 12 / 10);
        ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);

        ss.setSpan(imageSpan, temp.indexOf("-"), temp.indexOf("-") + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return ss;
    } catch (Exception e) {
        e.printStackTrace();
        Logs.e(e, "");
        return null;
    }
}

From source file:com.mk4droid.IMC_Activities.Fragment_NewIssueA.java

/**
 *        Check Image Orientation  /*from   w  ww  .  j a  v a 2 s . co m*/
 */
public void CheckOrient() {

    BitmapFactory.Options options = new BitmapFactory.Options(); // Resize is needed otherwize outofmemory exception
    options.inSampleSize = 6;

    //------------- read tmp file ------------------ 
    Image_BMP = BitmapFactory.decodeFile(image_path_source_temp, options); // , options

    //---------------- find exif header --------
    ExifInterface exif;
    String exifOrientation = "0"; // 0 = exif not working
    try {
        exif = new ExifInterface(image_path_source_temp);
        exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    //---------------- Resize ---------------------
    if (exifOrientation.equals("0")) {
        if (Image_BMP.getWidth() < Image_BMP.getHeight() && Image_BMP.getWidth() > 400) {
            Image_BMP = Bitmap.createScaledBitmap(Image_BMP, 400, 640, true); // <- To sent
        } else if (Image_BMP.getWidth() > Image_BMP.getHeight() && Image_BMP.getWidth() > 640) {
            Image_BMP = Bitmap.createScaledBitmap(Image_BMP, 640, 400, true); // <- To sent
        }
    } else {

        if (exifOrientation.equals("1") && Image_BMP.getWidth() > 640) { // normal

            Image_BMP = Bitmap.createScaledBitmap(Image_BMP, 640, 400, true); // <- To sent

        } else if (exifOrientation.equals("6") && Image_BMP.getWidth() > 400) { // rotated 90 degrees

            // Rotate
            Matrix matrix = new Matrix();

            int bmwidth = Image_BMP.getWidth();
            int bmheight = Image_BMP.getHeight();

            matrix.postRotate(90);

            Image_BMP = Bitmap.createBitmap(Image_BMP, 0, 0, bmwidth, bmheight, matrix, true);

            Image_BMP = Bitmap.createScaledBitmap(Image_BMP, 400, 640, true); // <- To sent
        }
    }

    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

    //------------ now store as jpg over the temp jpg
    File imagef = new File(image_path_source_temp);
    try {
        Image_BMP.compress(Bitmap.CompressFormat.JPEG, 95, new FileOutputStream(imagef));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:com.rnd.snapsplit.view.OcrCaptureFragment.java

public Bitmap byteStreamToBitmap(byte[] data) {
    int imageWidth = mCameraSource.getPreviewSize().getWidth();
    int imageHeight = mCameraSource.getPreviewSize().getHeight();
    //            YuvImage yuvimage=new YuvImage(data, ImageFormat.NV16, previewSizeW, previewSizeH, null);
    //            ByteArrayOutputStream baos = new ByteArrayOutputStream();
    //            yuvimage.compressToJpeg(new Rect(0, 0, previewSizeW, previewSizeH), 80, baos);
    //            byte[] jdata = baos.toByteArray();

    Bitmap bitmap = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
    int numPixels = imageWidth * imageHeight;

    // the buffer we fill up which we then fill the bitmap with
    IntBuffer intBuffer = IntBuffer.allocate(imageWidth * imageHeight);
    // If you're reusing a buffer, next line imperative to refill from the start,
    // if not good practice
    intBuffer.position(0);/*from  ww  w . j a  va 2  s. c  om*/

    // Set the alpha for the image: 0 is transparent, 255 fully opaque
    final byte alpha = (byte) 255;

    // Get each pixel, one at a time
    for (int y = 0; y < imageHeight; y++) {
        for (int x = 0; x < imageWidth; x++) {
            // Get the Y value, stored in the first block of data
            // The logical "AND 0xff" is needed to deal with the signed issue
            int Y = data[y * imageWidth + x] & 0xff;

            // Get U and V values, stored after Y values, one per 2x2 block
            // of pixels, interleaved. Prepare them as floats with correct range
            // ready for calculation later.
            int xby2 = x / 2;
            int yby2 = y / 2;

            // make this V for NV12/420SP
            float U = (float) (data[numPixels + 2 * xby2 + yby2 * imageWidth] & 0xff) - 128.0f;

            // make this U for NV12/420SP
            float V = (float) (data[numPixels + 2 * xby2 + 1 + yby2 * imageWidth] & 0xff) - 128.0f;

            // Do the YUV -> RGB conversion
            float Yf = 1.164f * ((float) Y) - 16.0f;
            int R = (int) (Yf + 1.596f * V);
            int G = (int) (Yf - 0.813f * V - 0.391f * U);
            int B = (int) (Yf + 2.018f * U);

            // Clip rgb values to 0-255
            R = R < 0 ? 0 : R > 255 ? 255 : R;
            G = G < 0 ? 0 : G > 255 ? 255 : G;
            B = B < 0 ? 0 : B > 255 ? 255 : B;

            // Put that pixel in the buffer
            intBuffer.put(alpha * 16777216 + R * 65536 + G * 256 + B);
        }
    }

    // Get buffer ready to be read
    intBuffer.flip();

    // Push the pixel information from the buffer onto the bitmap.
    bitmap.copyPixelsFromBuffer(intBuffer);

    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, imageWidth, imageHeight, true);
    Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(),
            scaledBitmap.getHeight(), matrix, true);
    return rotatedBitmap;
}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * //from   w w w  .  ja v  a 2 s  .  c om
 * 
 * @param angle
 *            
 * @param bitmap
 *            ?
 * @return ?
 */
public static Bitmap rotate(Bitmap bitmap, int angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}