android:在类中加载图像

标签 android image class load

在我的项目中,我有一个类,在这个类中我想访问图片的属性。但我不想通过这个类来展示它。在这门课中,我只想知道图像的宽度和高度,并执行一些数学函数来返回一些东西。

我的问题是我不知道该如何处理这张图片。我的图片在可绘制文件夹中。我写的代码是,问题是 image = (ImageView)findViewById(R.id.imViewRaw); :

import android.view.View;
import android.widget.ImageView;


public class Stego
{
    private ImageView image;
    private int imWidth, imHeight;

    public Stego()
    {
        // Instantiate an ImageView and define its properties
        image = (ImageView)findViewById(R.id.imViewRaw);
        image.setImageResource(R.drawable.tasnim);
        imWidth  = image.getWidth();
        imHeight = image.getHeight();
    }

    public int getImageWidth(){
        return imWidth;
    }

    public int getImageHeight(){
        return imHeight;
    }

    public int getMaxMessageChars(){
        int i = imWidth * imHeight; //Number of Pixels
        i *= 3; //Number of bytes. (Each pixel includes of three RGB bytes)
        i /= 4; //Maximum number of characters to store in the picture

        return i;
    }
}

最佳答案

图像存储在您的应用程序资源中,您可以通过 android.graphics.Bitmap 完全处理

    Bitmap image = BitmapFactory.decodeResource(this.getResources(), R.drawable.tasnim);
    imWidth = image.getWidth();
    imHeight = image.getHeight();

关于android:在类中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4301959/

相关文章:

Android RemoteViews,如何在小部件内设置 ImageView 的 ScaleType?

android - 在 Firebase 中为我的子 key 创建唯一 ID

JavaScript 在没有任何库的情况下获取元素类

python - 使用 Python 类作为数据容器

java - 无法将 Firebase 添加到我的 Android Studio

android - 如何从多项目目录的顶层禁用 Android Gradle 插件中的 lint abortOnError

java - byte[]转为图片下载

arrays - 数组中的特征项

ios - 知道照片是自拍还是人像

ios - 一个文件还是多个?创建多个 ViewController 类