【问题标题】:resizing images in gallery for image processing app. android为图像处理应用调整图库中的图像大小。安卓
【发布时间】:2011-10-27 19:12:47
【问题描述】:

我有一个图像处理应用程序。如果我使用捕获图像的应用程序部分拍照,图像最终会正确显示在屏幕上。这是因为我使用硬件屏幕尺寸指定了相机参数。如果我尝试加载从手机相机(而不是应用程序的相机)拍摄的图像,则图像在屏幕上太大,有时从字节的角度来看太大。如何调整或压缩图库中的图像,以便我的应用可以使用它们?谢谢

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();
        }

        if(mbitmap == null){
            Log.e(TAG,"mbitmap is null");
        }else{
            Log.e(TAG,"mbitmap is not null");
        }

.

public class DisplayUndistortedBitmapFromGallery extends View{





    public DisplayUndistortedBitmapFromGallery(Context context) {
        super(context);

    }





    public DisplayUndistortedBitmapFromGallery(Context context, AttributeSet attr) {
        super(context,attr);
        Log.e(TAG, "++++++++++ inside dubfgview constructor");





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

        if(bm == null){
            Log.e(TAG, "bm = null");
        }else{
            Log.e(TAG, "bm =  not null");
        }


        bgr = bm.copy(bm.getConfig(), true);

【问题讨论】:

    标签: android image


    【解决方案1】:

    如果我正确理解您的问题,则有两个问题。第一个是ImageView 没有缩放图像以适应屏幕,第二个是位图需要太多 RAM。

    要解决第一个问题,只需告诉图像视图您希望如何缩放图像。只需执行以下操作:

    imgView.setScaleType(ImageView.ScaleType.INSERT_SCALE_TYPE);
    

    Here is a list of the different scale types.

    如果这不能解决问题,请确保您的 ImageView 的范围不要太大。尝试将 ImageView 的大小硬编码为特定的大小,看看是否能解决问题。

    第二个问题很容易解决。您只需要告诉BitmapFactory 在加载图像时对其进行下采样。这是一个例子:

    int inSample = 4; // specifies how many 1/N samples to keep, in this case 1/4 off all samples are kept 
    
    opts = new BitmapFactory.Options();
    opts.inSampleSize = inSample;
    
    Bitmap b = BitmapFactory.decodeResource(c.getResources(), imgResId, opts);
    

    有关更详细的解释,请参阅我对这个问题的回答:Android GalleryView Recycling

    【讨论】:

    • 嗨,我没有图像视图,而是自定义视图类。这对您提供的帮助有影响吗?
    • @turtleboy 仅适用于 imgView.setScaleType() 方法。其他的东西还是一样的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 2010-09-11
    • 1970-01-01
    • 2010-11-27
    • 2020-05-10
    相关资源
    最近更新 更多