安卓:高品质的图像大小调整/缩放 [英] Android: high quality image resizing / scaling

查看:126
本文介绍了安卓:高品质的图像大小调整/缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要缩小图片来自网络数据流来了,而不会失去质量。

我知道这个解决方案的Andr​​oid:奇怪的内存不足问题但它太粗 - inSampleSize 是一个整数,并且不允许更好地控制所产生的尺寸。也就是说,我需要缩放图像以特定的H / W尺寸(并保持纵横比)。

我不介意在我的code进行DIY双立方/ lancoz算法,但我不能找到,将工作在Android上,因为它们都依赖于Java2D的(JavaSE的)任何例子。

编辑: 香港专业教育学院附快速源。原来是720x402高清屏幕捕获。请忽略前2缩略图。顶部大图是由机器人(如布局的一部分)会自动调整为约130x72。这是好的和清晰。底部调整图像大小与API和有严重伪影

我也试着使用BitmapFactory,正如我刚才所说,它有两个问题 - 没办法扩展到确切大小和缩放图像模糊

在如何解决artifcating任何想法?

谢谢,S.O!

 包qp.test;

进口android.app.Activity;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Matrix;
进口android.os.Bundle;
进口android.widget.ImageView;

公共类imgview延伸活动{

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

        位图原= BitmapFactory.de codeResource(getResources(),R.drawable.a000001570402);
        位图大小= getResizedBitmap(原130);
        //位图大小= getResizedBitmap2(原件,0.3f);
        通信System.err.println(resized.getWidth()+x表示+ resized.getHeight());

        ImageView的形象=(ImageView的)findViewById(R.id.ImageViewFullManual);
        image.setImageBitmap(调整);

    }

    私人位图getResizedBitmap(位图BM,诠释newWidth){

        INT宽度= bm.getWidth();

        INT高= bm.getHeight();

    浮动方面=(浮点)宽/高;

    浮scaleWidth = newWidth;

    浮scaleHeight = scaleWidth /方面; //耶!

    //创建矩阵用于操作

    字模=新的Matrix();

    //调整位图

    matrix.postScale(scaleWidth /宽,scaleHeight /高);

    //重新创建新的位图

    位图resizedBitmap = Bitmap.createBitmap(宽多重峰,0,0,宽度,高度,矩阵,真);

        bm.recycle();

        返回resizedBitmap;
    }

    私人位图getResizedBitmap2(位图BM,浮动比例){

    / *浮动方面= bm.getWidth()/ bm.getHeight();

        INT scaleWidth =(INT)(bm.getWidth()*标度);
        INT scaleHeight =(INT)(bm.getHeight()*标度);
* /
        //原始图像是720x402和采样大小= 4产生180x102,这是
        //仍然过大

        BitmapFactory.Options BFO =新BitmapFactory.Options();
        bfo.inSampleSize = 4;

        返回BitmapFactory.de codeResource(getResources(),R.drawable.a000001570402,BFO);
    }

}
 

和布局

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    >
<! - <的TextView
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=你好机器人:背景=#00FF00
    />
      - >
    < ImageView的机器人:ID =@ + ID / ImageViewThumbAuto
            机器人:layout_width =130dip机器人:layout_height =72dip
            机器人:SRC =@可绘制/ a000001570402/>

    < ImageView的机器人:ID =@ + ID / ImageViewThumbManual
            机器人:layout_width =130dip机器人:layout_height =72dip
            机器人:SRC =@可绘制/ a000001570402
            机器人:layout_toRightOf =@ ID / ImageViewThumbAuto
            />

< ImageView的机器人:ID =@ + ID / ImageViewFullAuto机器人:layout_width =300dip
            机器人:layout_height =169dip
            机器人:scaleType =fitXY
            机器人:SRC =@可绘制/ a000001570402
            机器人:layout_below =@ ID / ImageViewThumbAuto
            />

< ImageView的机器人:ID =@ + ID / ImageViewFullManual机器人:layout_width =300dip
            机器人:layout_height =169dip
            机器人:scaleType =fitXY
            机器人:SRC =@可绘制/ a000001570402
            机器人:layout_below =@ ID / ImageViewFullAuto
            />

< / RelativeLayout的>
 

解决方案

您可以使用 BitmapFactory.Options BitmapFactory.de code * 功能(S) ,
使用强度气体放inTargetDensity
例如:你有1600×1200的图像,并希望调整为640 * 480
然后'强度气体放'= 5和'inTargetDensity'= 2(1600×2等于640 * 5)。

希望这帮助。

I need to scale down images coming from a Network stream without losing quality.

I am aware of this solution Android: Strange out of memory issue but it is too coarse - inSampleSize is an integer and does not allow finer control over the resulting dimensions. That is, I need to scale images to specific h/w dimensions (and keeping aspect ratio).

I dont mind having a DIY bicubic/lancoz algorithm in my code but I cant find any examples that would work on Android as they all rely on Java2D (JavaSE).

EDIT: Ive attached a quick source. The original is 720x402 HD screen capture. Please ignore the top 2 thumbnails. The top large image is resized automatically by android (as part of layout) to about 130x72. It is nice and crisp. The bottom image is resized with API and has severe artifacting

I've also tried using the BitmapFactory and, as I said earlier, it has two problems - no way to scale to exact size and the scaled image is blurry.

Any ideas on how to fix the artifcating?

Thanks, S.O.!

    package qp.test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;

public class imgview extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.a000001570402);
        Bitmap resized = getResizedBitmap(original, 130);
        //Bitmap resized = getResizedBitmap2(original, 0.3f);
        System.err.println(resized.getWidth() + "x" + resized.getHeight());

        ImageView image = (ImageView) findViewById(R.id.ImageViewFullManual);
        image.setImageBitmap(resized);

    }

    private Bitmap getResizedBitmap(Bitmap bm, int newWidth) {

        int width = bm.getWidth();

        int height = bm.getHeight();

    float aspect = (float)width / height;

    float scaleWidth = newWidth;

    float scaleHeight = scaleWidth / aspect;        // yeah!

    // create a matrix for the manipulation

    Matrix matrix = new Matrix();

    // resize the bit map

    matrix.postScale(scaleWidth / width, scaleHeight / height);

    // recreate the new Bitmap

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);

        bm.recycle();

        return resizedBitmap;
    }

    private Bitmap getResizedBitmap2(Bitmap bm, float scale) {

    /*    float aspect = bm.getWidth() / bm.getHeight();

        int scaleWidth = (int) (bm.getWidth() * scale);
        int scaleHeight = (int) (bm.getHeight() * scale);
*/
        // original image is 720x402 and SampleSize=4 produces 180x102, which is
        // still too large

        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inSampleSize = 4;

        return BitmapFactory.decodeResource(getResources(), R.drawable.a000001570402, bfo);
    }

}

And the layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
<!-- <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="hullo" android:background="#00ff00"
    />
     -->
    <ImageView android:id="@+id/ImageViewThumbAuto"
            android:layout_width="130dip" android:layout_height="72dip"
            android:src="@drawable/a000001570402"  />

    <ImageView android:id="@+id/ImageViewThumbManual"
            android:layout_width="130dip" android:layout_height="72dip"
            android:src="@drawable/a000001570402"  
            android:layout_toRightOf="@id/ImageViewThumbAuto"
            />

<ImageView android:id="@+id/ImageViewFullAuto" android:layout_width="300dip"
            android:layout_height="169dip"
            android:scaleType="fitXY"
            android:src="@drawable/a000001570402"
            android:layout_below="@id/ImageViewThumbAuto"
            />

<ImageView android:id="@+id/ImageViewFullManual" android:layout_width="300dip"
            android:layout_height="169dip"
            android:scaleType="fitXY"
            android:src="@drawable/a000001570402"
            android:layout_below="@id/ImageViewFullAuto"
            />

</RelativeLayout>

解决方案

You can use BitmapFactory.Options with BitmapFactory.decode* function(s),
using 'inDensity' and 'inTargetDensity'
Example: you have 1600*1200 image and want to resize to 640*480
then 'inDensity' = 5 and 'inTargetDensity' = 2 (1600*2 equal to 640*5).

Hoping this help.

这篇关于安卓:高品质的图像大小调整/缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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