如何从库调整图像? [英] How resize image from gallery?

查看:130
本文介绍了如何从库调整图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的第一个文件允许用户挑选从库中的照片,并将其发送到将处理它的活性。问题是,该图像是太大,手机的屏幕,所以你只能看到图像的右上角(好像它已经被放大了)。

 按钮useGallery =(按钮)findViewById(R.id.loadfromgallery);
        useGallery.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(查看为arg0){

                意图photoPickerIntent =新的意图(Intent.ACTION_PICK);
                photoPickerIntent.setType(图像/ *);
                startActivityForResult(photoPickerIntent,SELECT_PHOTO);
            }});

 @覆盖
    保护无效onActivityResult(INT申请code,INT结果code,意图imageReturnedIntent){
        super.onActivityResult(要求code,因此code,imageReturnedIntent);

        开关(要求code){
        案例SELECT_PHOTO:
            如果(结果code == RESULT_OK){
                乌里selectedImage = imageReturnedIntent.getData();
                的InputStream的ImageStream = NULL;
                尝试 {
                    的ImageStream = getContentResolver()openInputStream(selectedImage)。
                }赶上(FileNotFoundException异常E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }
                位图yourSelectedImage = BitmapFactory.de codeStream(的ImageStream);
                意向意图=新的意图(mContext,DisplayUndistortedBitmapFromGalleryActivity.class);
                intent.setData(selectedImage);
                startActivity(意向);

                如果(yourSelectedImage!= NULL){
                Log.e(TAG,PIC OK);
                }其他{
                     Log.e(TAG,事先知情同意也不行);
                }
            }
        }
 

。该第二文件是从意图临危的图像数据,并把它在一个URI一个位图是来自于活性

 公共类DisplayUndistortedBitmapFromGalleryActivity延伸活动{

    私有静态最后字符串变量=********* DUBFGActivity;
    民营背景mContext =这一点;
    乌里URI;
    私人位图mbitmap = NULL;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);


         URI = getIntent()的getData()。
        如果(URI!= NULL){
            Log.e(TAG,URI OK);
        }其他 {
            Log.e(TAG,URI也不行);
        }


        尝试 {
              mbitmap = Media.getBitmap(getContentResolver(),URI);
             // setMbitmap(位);
        }赶上(FileNotFoundException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
 

我这是customview的活动第3档。线下检索位图从它的活性,并显示它。是否有裁员的位图,以便在屏幕上适合的方法吗?谢谢。

 位图BM =((DisplayUndistortedBitmapFromGalleryActivity)的getContext())getMbitmap()。
 

解决方案

下面是我通常调整位图,是最简单的方法。

 公共类bitmaptest延伸活动{
@覆盖
公共无效的onCreate(包冰柱){
    super.onCreate(冰柱);
    的LinearLayout linLayout =新的LinearLayout(本);

    //加载origial位图(500×500像素)
    位图bitmapOrg = BitmapFactory.de codeResource(getResources()
           R.drawable.android);

    INT宽度= bitmapOrg.width();
    INT高= bitmapOrg.height();
    INT newWidth = 200;
    INT newHeight = 200;

    //计算比例 - 在这种情况下= 0.4f
    浮动scaleWidth =((浮点)newWidth)/宽度;
    浮动scaleHeight =((浮点)newHeight)/身高;

    // createa矩阵用于操作
    字模=新的Matrix();
    //调整位图
    matrix.postScale(scaleWidth,scaleHeight);
    //旋转位图


    //重新创建新的位图
    位图resizedBitmap = Bitmap.createBitmap(bitmapOrg,0,0,
                      宽度,高度,矩阵,真);

    //使从位图被拉伸以允许设置位图
    //为ImageView的,ImageButton的或什么都
    BitmapDrawable BMD =新BitmapDrawable
 

编辑:

下面是另一种选择。

  INT targetWidth = bitmapOrg.getWidth() -  15; //更改为控制大小
  INT targetHeight = bitmapOrg.getHeight() -  15;
  字模=新的Matrix();
  matrix.postScale(1F,1F);
  位图resizedBitmap = Bitmap.createBitmap(BMP,0,0,targetWidth,targetHeight,矩阵,真);
 

The first file allow the user to pick a photo from the gallery and sends it to an activity which will process it. The problem is that the image is too large for the phone's screen, so you only see the top corner of the image(as if it has been magnified).

Button useGallery = (Button)findViewById(R.id.loadfromgallery);
        useGallery.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, SELECT_PHOTO); 
            }}) ;

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

        switch(requestCode) { 
        case SELECT_PHOTO:
            if(resultCode == RESULT_OK){  
                Uri selectedImage = imageReturnedIntent.getData();
                InputStream imageStream = null;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                Intent intent = new Intent(mContext, DisplayUndistortedBitmapFromGalleryActivity.class);
                intent.setData(selectedImage);
                startActivity(intent);

                if(yourSelectedImage != null){
                Log.e(TAG,"pic ok");
                }else{
                     Log.e(TAG,"pic not ok");
                }
            }
        }

. The 2nd file is the activity that recieves the image data from the intent and places it in a URI that a bitmap is the derived from.

public class DisplayUndistortedBitmapFromGalleryActivity extends Activity {

    private static final String TAG = "*********DUBFGActivity";
    private Context mContext = this;
    Uri uri;
    private Bitmap mbitmap = null;

    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);


         uri = getIntent().getData();
        if(uri != null){
            Log.e(TAG, "uri ok");
        }else {
            Log.e(TAG, "uri not ok");
        }


        try {
              mbitmap = Media.getBitmap(getContentResolver(), uri);
             //setMbitmap(bitmap);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

.

I have a 3rd file which is the customview for the activity. The line below retrieves the bitmap from it's activity and displays it. Is there a way of downsizing the bitmap so it fits on the screen? thanks.

Bitmap bm = ((DisplayUndistortedBitmapFromGalleryActivity)getContext()).getMbitmap();

解决方案

Here is how i usually resize a bitmap and is the easiest way.

public class bitmaptest extends Activity {
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    LinearLayout linLayout = new LinearLayout(this);

    // load the origial BitMap (500 x 500 px)
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
           R.drawable.android);

    int width = bitmapOrg.width();
    int height = bitmapOrg.height();
    int newWidth = 200;
    int newHeight = 200;

    // calculate the scale - in this case = 0.4f
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap


    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                      width, height, matrix, true);

    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable

EDIT:

Here is another option.

  int targetWidth  = bitmapOrg.getWidth() - 15; //change this to control the size
  int targetHeight = bitmapOrg.getHeight() - 15 ;
  Matrix matrix = new Matrix();
  matrix.postScale(1f, 1f);
  Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, targetWidth, targetHeight, matrix, true);

这篇关于如何从库调整图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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