center裁剪图像以适当的大小在ImageView上设置 [英] center Crop Image In proper size to set on ImageView

查看:325
本文介绍了center裁剪图像以适当的大小在ImageView上设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用相机API拍照,我必须根据我的图像视图大小打开不同大小的相机。我在下面的示例项目,我们在Android sdk / sample / adroid-18里面的名字ApiDemo我改变的东西没有设置camera setcontentview。我已经在相框布局上设置了相机。首先我的相机预览被饥饿,所以我得到了相机OptimalPreviewSize,并使FrameLayout参数宽度和高度作为wrap-content.Now相机预览比ImageView(我想要的大小)。如果我将FrameLayout参数的大小设置为match-parent,那么相机视图是stretch.How来解决这个问题。

I am using camera API to take picture i have to open camera in different sizes according to my Image view size. I am following the sample project which we get inside Android sdk/sample/adroid-18 at the name "ApiDemo" the thing i have changed is not set camera on setcontentview. I have set the camera on Frame Layout. at first my camera preview was starched so i got the camera OptimalPreviewSize and make FrameLayout parameter width and height as wrap-content.Now the camera preview is smaller then ImageView (The size i want). If i make the size of FrameLayout parameter as match-parent then camera View is stretch.How to resolve this issue.

找到这个链接更多的规格。 Android相机预览看起来很奇怪

find this link for more specification. Android camera preview look strange

UPDATE

我的相机预览大小很好,现在我使用的布局方法的想法是我有更大的布局,然后我的ImageView和现在的相机预览看起来不错。
现在我面对的问题是设置适当的大小的图像,我必须中心裁剪和缩放在相同的大小像我的ImageView.this图像我通过TakePicture方法并保存在SD卡。

My camera preview size is fine now i use the on Layout method the idea was i have the bigger layout then my ImageView and now camera preview is looking good. Now the Problem I am facing is set the image of proper size for this I have to center crop and scale in same size in like my ImageView.this Image i get by TakePicture method and saved in sdcard.

对于这个,我使用这个方法: -

For this I am using this method:-

    public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();

    // Compute the scaling factors to fit the new height and width, respectively.
    // To cover the final image, the final scaling will be the bigger 
    // of these two.
    float xScale = (float) newWidth / sourceWidth;
    float yScale = (float) newHeight / sourceHeight;
    float scale = Math.max(xScale, yScale);

    // Now get the size of the source bitmap when scaled
    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;

    // Let's find out the upper left coordinates if the scaled bitmap
    // should be centered in the new size give by the parameters
    float left = (newWidth - scaledWidth) / 2;
    float top = (newHeight - scaledHeight) / 2;

        // The target rectangle for the new, scaled version of the source bitmap will now
        // be
        RectF targetRect = new RectF(left+50, top, left + scaledWidth, top + scaledHeight+50);
//      RectF targetRect = new RectF(0, 0, newWidth, newHeight/2);
        // Finally, we create a new bitmap of the specified size and draw our new,
        // scaled bitmap onto it.
        Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
        Canvas canvas = new Canvas(dest);
        canvas.drawBitmap(source, null, targetRect, null);

        return dest;
}

但结果图像质量不好。底部,结果图像质量不佳。像素正在拉伸。

But the result image quality is not good.Height Corners are cutting from top and bottom, and result image quality is not good.Pixels are stretching.

不要告诉我使用scaleType = Center_crop我不能在我的情况下使用它,

Don't tell me to use scaleType=Center_crop i can't use it in my case,and don't want to show cropping frame to user,this all process should not show on UI.

UPDATE

我使用blow方法从中心裁剪图像,并根据我的imageView大小缩放

I used blow method for crop image from center and scale according to my imageView size

Bitmap dstBmp = ThumbnailUtils.extractThumbnail(source, newWidth, newHeight);

但是我得到的位图看起来不像FrameLayout上显示的相机预览。因为相机预览很大。我认为这些代码裁剪了大面积。
我试图减少宽度和改变高度,但没有得到相同的裁剪图像在我想要的比率。

But the bitmap i got is not looking same the camera preview shown on FrameLayout. because camera preview is big.I think these code cropped the large area. I tried to reduce the width and change the height but not getting the same cropped image in which ratio i want.

图片裁剪后的另一个想法在FrameLayout上自动设置的最后一个图像帧。我们可以从帧布局获得设置帧。这是怎么回事?

One more idea i have after picture crop a last image frame set automatically on FrameLayout. can we get that set frame from Frame Layout. How is this possible?

这里有类似这样的问题

Here is question like this How to retrieve the visible part of a SurfaceView in Android do any one have solution.

在此行中更改的内容究竟是什么????

What to change in this line exactly ????

推荐答案

@Akanksha请使用以下代码,你只需要传递保存的图像的路径,以及我们的imageview的高度和宽度。这个代码适合我。

@Akanksha Please use this below code, you just need to pass the path of the saved image, and the hight and width of our imageview. This code works perfectly for me.


    import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ImageHandler {
    /**
     * Decode and sample down a bitmap from a file to the requested width and
     * height.
     * 
     * @param filename
     *            The full path of the file to decode
     * @param reqWidth
     *            The requested width of the resulting bitmap
     * @param reqHeight
     *            The requested height of the resulting bitmap
     * @return A bitmap sampled down from the original with the same aspect
     *         ratio and dimensions that are equal to or greater than the
     *         requested width and height
     */


public static Bitmap decodeSampledBitmapFromFile(String filename,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filename, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filename, options);
    }



public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }

            // This offers some additional logic in case the image has a
            // strange
            // aspect ratio. For example, a panorama may have a much larger
            // width than height. In these cases the total pixels might
            // still
            // end up being too large to fit comfortably in memory, so we
            // should
            // be more aggressive with sample down the image (=larger
            // inSampleSize).

            final float totalPixels = width * height;

            // Anything more than 2x the requested pixels we'll sample down
            // further.
            final float totalReqPixelsCap = reqWidth * reqHeight * 2;

            while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
                inSampleSize++;
            }
        }
        return inSampleSize;
    }
}



我在异步任务中调用此方法,太多的UImemory和时间
这是我如何调用它。

I call this method inside async task because it may take too much UImemory and time Here is how I call it.


class Asyncing extends AsyncTask {

        private int reqWidth;
        private int reqHeight;
        private ImageView iv;
        private String fileName;
        private ProgressDialog pd;

        public Asyncing(int reqWidth, int reqHeight, ImageView iv,
                String fileName) {
            super();
            this.reqWidth = reqWidth;
            this.reqHeight = reqHeight;
            this.fileName = fileName;
            this.iv = iv;
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            return ImageHandler.decodeSampledBitmapFromFile(params[0],
                    reqWidth, reqHeight);

        }

        @Override
        protected void onPostExecute(Bitmap result) {
            iv.setImageBitmap(result);
            if (pd.isShowing()) {
                pd.setMessage(getString(R.string.completed));
                pd.dismiss();
            }

            super.onPostExecute(result);
        }

        @Override
        protected void onProgressUpdate(Void... values) {

            super.onProgressUpdate(values);
        }

        @Override
        protected void onPreExecute() {
            pd = ProgressDialog.show(CustomerDetailsActivity.this, "",
                    getString(R.string.processing_signature));
            super.onPreExecute();
        }

    }

asynctask

This is how you need to call the asynctask


signedImagePath = data.getExtras().getString("imagePath");

            new Asyncing(signature_img.getWidth(), signature_img.getHeight(),
                    signature_img, "spenTest.png").execute(signedImagePath);



above code is written according to my requirements,you modify it according to yours.

这篇关于center裁剪图像以适当的大小在ImageView上设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆