Example usage for android.graphics Matrix Matrix

List of usage examples for android.graphics Matrix Matrix

Introduction

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

Prototype

public Matrix() 

Source Link

Document

Create an identity matrix

Usage

From source file:com.socialize.util.ImageUtils.java

public byte[] scaleImage(Context context, Uri photoUri) throws IOException {

    BitmapFactory.Options dbo = new BitmapFactory.Options();
    dbo.inJustDecodeBounds = true;/*from  w  ww .  java  2 s. c  o m*/
    InputStream is = getStreamToUri(context, photoUri);
    BitmapFactory.decodeStream(is, null, dbo);
    is.close();

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

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

    Bitmap bitmap;
    is = getStreamToUri(context, photoUri);
    if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
        float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
        float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
        float maxRatio = Math.max(widthRatio, heightRatio);

        // Create the bitmap from file
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = (int) maxRatio;
        bitmap = BitmapFactory.decodeStream(is, null, options);
    } else {
        bitmap = BitmapFactory.decodeStream(is);
    }
    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);

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

    String type = "image/png";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (type.equals("image/png")) {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    } else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    }
    byte[] bMapArray = baos.toByteArray();
    baos.close();
    return bMapArray;
}

From source file:bmcx.aiton.com.passenger.view.activity.SmsLoginActivity.java

private void initAnim() {
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int screenW = dm.widthPixels;// ?
    offset = screenW / 2;// ???
    Matrix matrix = new Matrix();
    matrix.postTranslate(offset, 0);/*  ww w.  j  ava  2 s  .c  o  m*/
    mImageView_tiao01.setImageMatrix(matrix);// ??
}

From source file:com.pablog178.pdfcreator.android.PdfcreatorModule.java

private void generateImageFunction(HashMap args) {
    if (args.containsKey("fileName")) {
        Object fileName = args.get("fileName");
        if (fileName instanceof String) {
            this.fileName = (String) fileName;
            Log.i(PROXY_NAME, "fileName: " + this.fileName);
        }/*from   w  ww.j av a  2s  . c o  m*/
    } else
        return;

    if (args.containsKey("view")) {
        Object viewObject = args.get("view");
        if (viewObject instanceof TiViewProxy) {
            TiViewProxy viewProxy = (TiViewProxy) viewObject;
            this.view = viewProxy.getOrCreateView();
            if (this.view == null) {
                Log.e(PROXY_NAME, "NO VIEW was created!!");
                return;
            }
            Log.i(PROXY_NAME, "view: " + this.view.toString());
        }
    } else
        return;

    TiBaseFile file = TiFileFactory.createTitaniumFile(this.fileName, true);
    Log.i(PROXY_NAME, "file full path: " + file.nativePath());
    try {
        final int PDF_WIDTH = 612;
        final int PDF_HEIGHT = 792;
        Resources appResources = app.getResources();
        OutputStream outputStream = file.getOutputStream();
        int viewWidth = 1600;
        int viewHeight = 1;

        WebView view = (WebView) this.view.getNativeView();

        if (TiApplication.isUIThread()) {

            viewWidth = view.capturePicture().getWidth();
            viewHeight = view.capturePicture().getHeight();

            if (viewWidth <= 0) {
                viewWidth = 1300;
            }

            if (viewHeight <= 0) {
                viewHeight = 2300;
            }

        } else {
            Log.e(PROXY_NAME, "NO UI THREAD");
        }

        Log.i(PROXY_NAME, "viewWidth: " + viewWidth);
        Log.i(PROXY_NAME, "viewHeight: " + viewHeight);

        Bitmap viewBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888);
        float density = appResources.getDisplayMetrics().density;

        Canvas canvas = new Canvas(viewBitmap);
        Matrix matrix = new Matrix();

        Drawable bgDrawable = view.getBackground();
        if (bgDrawable != null) {
            bgDrawable.draw(canvas);
        } else {
            canvas.drawColor(Color.WHITE);
        }
        view.draw(canvas);

        float scaleFactorWidth = 1 / ((float) viewWidth / (float) PDF_WIDTH);
        float scaleFactorHeight = 1 / ((float) viewHeight / (float) PDF_HEIGHT);

        Log.i(PROXY_NAME, "scaleFactorWidth: " + scaleFactorWidth);
        Log.i(PROXY_NAME, "scaleFactorHeight: " + scaleFactorHeight);

        matrix.setScale(scaleFactorWidth, scaleFactorWidth);

        Bitmap imageBitmap = Bitmap.createBitmap(PDF_WIDTH, PDF_HEIGHT, Bitmap.Config.ARGB_8888);
        Canvas imageCanvas = new Canvas(imageBitmap);
        imageCanvas.drawBitmap(viewBitmap, matrix, null);
        imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

        sendCompleteEvent();

    } catch (Exception exception) {
        Log.e(PROXY_NAME, "Error: " + exception.toString());
        sendErrorEvent(exception.toString());
    }
}

From source file:com.haru.ui.image.workers.MediaProcessorThread.java

private String compressAndSaveImage(String fileImage, int scale) throws Exception {
    try {/* ww w  . ja v  a2s  . c o  m*/
        ExifInterface exif = new ExifInterface(fileImage);
        String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        String length = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;
        if (/* TODO: DEBUG */ true) {
            Log.i(TAG, "Before: " + width + "x" + length);
        }

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = -90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

        int w = Integer.parseInt(width);
        int l = Integer.parseInt(length);

        int what = w > l ? w : l;

        Options options = new Options();
        if (what > 1500) {
            options.inSampleSize = scale * 4;
        } else if (what > 1000 && what <= 1500) {
            options.inSampleSize = scale * 3;
        } else if (what > 400 && what <= 1000) {
            options.inSampleSize = scale * 2;
        } else {
            options.inSampleSize = scale;
        }
        if (/* TODO: DEBUG */ true) {
            Log.i(TAG, "Scale: " + (what / options.inSampleSize));
            Log.i(TAG, "Rotate: " + rotate);
        }
        Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options);
        File original = new File(fileImage);
        File file = new File((original.getParent() + File.separator
                + original.getName().replace(".", "_fact_" + scale + ".")));
        FileOutputStream stream = new FileOutputStream(file);
        if (rotate != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(rotate);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        if (/* TODO: DEBUG */ true) {
            ExifInterface exifAfter = new ExifInterface(file.getAbsolutePath());
            String widthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
            String lengthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
            if (/* TODO: DEBUG */ true) {
                Log.i(TAG, "After: " + widthAfter + "x" + lengthAfter);
            }
        }
        stream.flush();
        stream.close();
        return file.getAbsolutePath();

    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Corrupt or deleted file???");
    }
}

From source file:com.cars.manager.utils.imageChooser.threads.MediaProcessorThread.java

private String compressAndSaveImage(String fileImage, int scale) throws Exception {
    try {//w ww  .jav  a 2  s .c o  m
        ExifInterface exif = new ExifInterface(fileImage);
        String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        String length = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;
        if (Config.DEBUG) {
            Log.i(TAG, "Before: " + width + "x" + length);
        }

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = -90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

        int w = Integer.parseInt(width);
        int l = Integer.parseInt(length);

        int what = w > l ? w : l;

        Options options = new Options();
        if (what > 1500) {
            options.inSampleSize = scale * 4;
        } else if (what > 1000 && what <= 1500) {
            options.inSampleSize = scale * 3;
        } else if (what > 400 && what <= 1000) {
            options.inSampleSize = scale * 2;
        } else {
            options.inSampleSize = scale;
        }
        if (Config.DEBUG) {
            Log.i(TAG, "Scale: " + (what / options.inSampleSize));
            Log.i(TAG, "Rotate: " + rotate);
        }
        Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options);
        File original = new File(fileImage);
        File file = new File((original.getParent() + File.separator
                + original.getName().replace(".", "_fact_" + scale + ".")));
        FileOutputStream stream = new FileOutputStream(file);
        if (rotate != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(rotate);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        if (Config.DEBUG) {
            ExifInterface exifAfter = new ExifInterface(file.getAbsolutePath());
            String widthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
            String lengthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
            if (Config.DEBUG) {
                Log.i(TAG, "After: " + widthAfter + "x" + lengthAfter);
            }
        }
        stream.flush();
        stream.close();
        return file.getAbsolutePath();

    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Corrupt or deleted file???");
    }
}

From source file:edu.stanford.mobisocial.dungbeetle.ImageViewerActivity.java

@Override
public void onResume() {
    super.onResume();
    if (mIntent.hasExtra("image_url")) {
        String url = mIntent.getStringExtra("image_url");
        ((App) getApplication()).objectImages.lazyLoadImage(url.hashCode(), Uri.parse(url), im);
        bitmap = mgr.getBitmap(url.hashCode(), url);
    } else if (mIntent.hasExtra("b64Bytes")) {
        String b64Bytes = mIntent.getStringExtra("b64Bytes");
        ((App) getApplication()).objectImages.lazyLoadImage(b64Bytes.hashCode(), b64Bytes, im);
        bitmap = mgr.getBitmapB64(b64Bytes.hashCode(), b64Bytes);
    } else if (mIntent.hasExtra("bytes")) {
        byte[] bytes = mIntent.getByteArrayExtra("bytes");
        ((App) getApplication()).objectImages.lazyLoadImage(bytes.hashCode(), bytes, im);
        bitmap = mgr.getBitmap(bytes.hashCode(), bytes);
    } else if (mIntent.hasExtra("obj")) {
        try {//from  w  w w.ja  v a  2 s. c  om
            final JSONObject content = new JSONObject(mIntent.getStringExtra("obj"));
            byte[] bytes = FastBase64.decode(content.optString(PictureObj.DATA));
            ((App) getApplication()).objectImages.lazyLoadImage(bytes.hashCode(), bytes, im);
            bitmap = mgr.getBitmap(bytes.hashCode(), bytes);
        } catch (JSONException e) {
        }
    }

    if (mIntent.hasExtra("objHash")) {
        if (!ContentCorral.CONTENT_CORRAL_ENABLED) {
            return;
        }

        long objHash = mIntent.getLongExtra("objHash", -1);
        final DbObj obj = App.instance().getMusubi().objForHash(objHash);
        final JSONObject json = obj.getJson();
        if (json.has(CorralClient.OBJ_LOCAL_URI)) {
            // TODO: this is a proof-of-concept.
            new Thread() {
                public void run() {
                    try {
                        if (!mCorralClient.fileAvailableLocally(obj)) {
                            //toast("Trying to go HD...");
                        }
                        // Log.d(TAG, "Trying to go HD...");
                        final Uri fileUri = mCorralClient.fetchContent(obj);
                        if (fileUri == null) {
                            try {
                                Log.d(TAG, "Failed to go HD for " + json.getString(CorralClient.OBJ_LOCAL_URI));
                            } catch (JSONException e) {
                                Log.d(TAG, "Failed to go HD for " + json);
                            }
                            return;
                        }
                        // Log.d(TAG, "Opening HD file " + fileUri);

                        InputStream is = getContentResolver().openInputStream(fileUri);
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inSampleSize = 4;

                        Matrix matrix = new Matrix();
                        float rotation = PhotoTaker.rotationForImage(ImageViewerActivity.this, fileUri);
                        if (rotation != 0f) {
                            matrix.preRotate(rotation);
                        }
                        bitmap = BitmapFactory.decodeStream(is, null, options);

                        int width = bitmap.getWidth();
                        int height = bitmap.getHeight();
                        bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                im.setImageBitmap(bitmap);
                            }
                        });
                    } catch (IOException e) {
                        // toast("Failed to go HD");
                        Log.e(TAG, "Failed to get hd content", e);
                        // continue
                    }
                };
            }.start();
        }
    }
}

From source file:de.tlabs.ssr.g1.client.SourcesView.java

private void init() {
    // make this view focusable
    setFocusable(true);/*from w w w. ja v  a2 s.  co m*/

    // init fields
    viewportTransformation = new Matrix();
    newViewportTransformation = new Matrix();
    inverseViewportTransformation = new Matrix();

    selectionOffset = new float[2];
    touchPoint = new float[2];

    buffer = ByteBuffer.allocate(1024);

    scalingInterpolator = new TimedInterpolator();
    scalingInterpolator.setDuration(800);
    translationXInterpolator = new TimedInterpolator();
    translationXInterpolator.setDuration(800);
    translationYInterpolator = new TimedInterpolator();
    translationYInterpolator.setDuration(800);
    rotationInterpolator = new TimedInterpolator();
    rotationInterpolator.setDuration(800);
    centerRotationInterpolator = new TimedInterpolator();
    centerRotationInterpolator.setDuration(800);

    currentOrientation = getContext().getResources().getConfiguration().orientation;
    setOrientationFlag(true);

    if (SourcesView.paint == null) {
        SourcesView.paint = new Paint();
        SourcesView.paint.setAntiAlias(false);
        SourcesView.paint.setStrokeWidth(0);
        SourcesView.paint.setTextAlign(Paint.Align.CENTER);
        SourcesView.paint.setTextSize(9.0f);
    }

    // set up orientation event listener
    orientationEventListener = new OrientationEventListener(getContext(), SensorManager.SENSOR_DELAY_NORMAL) {
        @Override
        public void onOrientationChanged(int orientation) {
            if ((orientation >= 80 && orientation <= 100) || (orientation >= 260 && orientation <= 280)) { // landscape
                setOrientation(Configuration.ORIENTATION_LANDSCAPE);
            } else if ((orientation >= 350 || orientation <= 10)
                    || (orientation >= 170 && orientation <= 190)) { // portrait
                setOrientation(Configuration.ORIENTATION_PORTRAIT);
            }
        }
    };
    if (!GlobalData.orientationTrackingEnabled) // orientation tracking and screen rotation tracking don't go together
        orientationEventListener.enable();

    // set up gesture detector
    gestureDetector = new GestureDetector(getContext(), this);
    gestureDetector.setIsLongpressEnabled(false);
    gestureDetector.setOnDoubleTapListener(this);

    // init viewport transformation matrix
    recalculateViewportTransformation();
}

From source file:com.nloko.android.Utils.java

public static Bitmap resize(Bitmap bitmap, int maxHeight, int maxWidth) {
    if (bitmap == null) {
        throw new IllegalArgumentException("bitmap");
    }//from w w w  .  j  a  v  a 2  s  .c o m

    int height = bitmap.getHeight();
    int width = bitmap.getWidth();

    if ((maxHeight > 0 && height <= maxHeight) && (maxWidth > 0 && width <= maxWidth)) {
        return bitmap;
    }

    int newHeight = height;
    int newWidth = width;

    float ratio;

    if (newHeight > maxHeight && maxHeight > 0) {
        ratio = (float) newWidth / (float) newHeight;
        newHeight = maxHeight;
        newWidth = Math.round(ratio * (float) newHeight);
    }

    if (newWidth > maxWidth && maxWidth > 0) {
        ratio = (float) newHeight / (float) newWidth;
        newWidth = maxWidth;
        newHeight = Math.round(ratio * (float) newWidth);
    }

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
}

From source file:com.wots.lutmaar.CustomView.imagechooser.threads.MediaProcessorThread.java

private String compressAndSaveImage(String fileImage, int scale) throws Exception {
    try {//w w w  .  java  2 s .c  o m
        ExifInterface exif = new ExifInterface(fileImage);
        String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        String length = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;
        if (BuildConfig.DEBUG) {
            Log.i(TAG, "Before: " + width + "x" + length);
        }

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = -90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

        int w = Integer.parseInt(width);
        int l = Integer.parseInt(length);

        int what = w > l ? w : l;

        Options options = new Options();
        if (what > 1500) {
            options.inSampleSize = scale * 4;
        } else if (what > 1000 && what <= 1500) {
            options.inSampleSize = scale * 3;
        } else if (what > 400 && what <= 1000) {
            options.inSampleSize = scale * 2;
        } else {
            options.inSampleSize = scale;
        }
        if (BuildConfig.DEBUG) {
            Log.i(TAG, "Scale: " + (what / options.inSampleSize));
            Log.i(TAG, "Rotate: " + rotate);
        }
        Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options);
        File original = new File(fileImage);
        File file = new File((original.getParent() + File.separator
                + original.getName().replace(".", "_fact_" + scale + ".")));
        FileOutputStream stream = new FileOutputStream(file);
        if (rotate != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(rotate);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);

        }
        /* if (scale == 1)
        bitmap = Bitmap.createScaledBitmap(bitmap, 240, 260, true);*/
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        if (BuildConfig.DEBUG) {
            ExifInterface exifAfter = new ExifInterface(file.getAbsolutePath());
            String widthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
            String lengthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
            if (BuildConfig.DEBUG) {
                Log.i(TAG, "After: " + widthAfter + "x" + lengthAfter);
            }
        }
        stream.flush();
        stream.close();
        return file.getAbsolutePath();

    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Corrupt or deleted file???");
    }
}