在android质量和速度中旋转缩放位图

时间:2016-10-01 03:24:46

标签: android rotation scale android-bitmap

我发现当我使用

  

createbitmap()

仅用于旋转位图,在此之后

  

createscaledbitmap()

仅适用于Android SDK中的缩放,在旋转图像的情况下,位图的值(在我的情况下为JPEG coeff。)会发生变化! (质量较低)

所以我可能需要在一个调用函数createbitmap中使用对象Matrix进行旋转和缩放。 (在这种情况下,我可以告诉更多关于数学的信息吗?我找不到任何想法)

在这段代码中我尝试了它,但它在rotateBitmap中返回不同的维度而不是dstWidth,dstHeight; pixelIn:¨

注意:我只需要1d数据令牌向量,:OUTPUT

public static int[] getPixels(String pathOfInputImage, int dstWidth,
        int dstHeight) {

    Bitmap resizedBitmap = null;
    InputStream in = null;
    int[] pixelIn;
    boolean filter = false;
    int inWidth = 0;
    int inHeight = 0;
    // calculate the scale - in this case = 0.4f

    try {
        in = new FileInputStream(pathOfInputImage);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (in == null) {
        Log.v("--------Image ERROR--------",
                "Image ERROR PIXEL FileInputStream( ");

    }
    // decode image size (decode metadata only, not the whole image)
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;
    // options.inDither = true;
    // options.inPreferQualityOverSpeed = true;

    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(in, null, options);

    try {
        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    in = null;

    // save width and height
    inWidth = options.outWidth;
    inHeight = options.outHeight;

    FileInputStream in2 = null;
    // decode full image pre-resized
    try {
        in2 = new FileInputStream(pathOfInputImage);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Options options2 = new BitmapFactory.Options();
    // options.inDither = true;
    // options2.inPreferQualityOverSpeed = true;

    options2.inPreferredConfig = Bitmap.Config.ARGB_8888;

    // calc rought re-size (this is no exact resize)
    options2.inSampleSize = Math.max(inWidth / dstWidth, inHeight
            / dstHeight);
    // decode full

    System.gc();// /????????????????????????????????
    Bitmap roughBitmap = BitmapFactory.decodeStream(in2, null, options2);

    float scaleWidth = ((float) dstWidth) / inWidth;
    float scaleHeight = ((float) dstHeight) / inHeight;

    if (roughBitmap == null) {
        pixelIn = getPixelsZadniVratka(pathOfInputImage, dstWidth,
                dstHeight);

    } else {
        // calc exact destination size

        PhotoRecord p9 = new PhotoRecord();
        int rot9 = p9.loadRotation(pathOfInputImage).roatate;

        Matrix matrix = new Matrix();
        matrix.postRotate(rot9);
        matrix.postScale(scaleWidth, scaleHeight);

        // Bitmap resizedBitmap2 = Bitmap.createScaledBitmap(roughBitmap,
        // dstWidth, dstHeight, filter);
        // /------------------------------------
        Bitmap rotatedBitmap = Bitmap.createBitmap(roughBitmap, 0, 0,
                roughBitmap.getWidth(), roughBitmap.getHeight(), matrix,
                filter);

        Log.v("--------TAG--------", "JE inWidth PROCESS?????( " + inWidth);
        Log.v("--------TAG--------", "JE getHeight PROCESS?????( "
                + rotatedBitmap.getHeight());
        Log.v("--------TAG--------", "JE getWidth PROCESS?????( "
                + rotatedBitmap.getWidth());
        pixelIn = new int[dstWidth * dstHeight];

        if (rotatedBitmap != null) {
            rotatedBitmap.getPixels(pixelIn, 0, dstWidth, 0, 0, dstWidth,
                    dstHeight);
            rotatedBitmap.recycle();

        } else {
            pixelIn = null;

        }

    }

    return pixelIn;
}

0 个答案:

没有答案