在我的项目中,我有一个类,在这个类中我想访问图片的属性。但我不想通过这个类来展示它。在这门课中,我只想知道图像的宽度和高度,并执行一些数学函数来返回一些东西。
我的问题是我不知道该如何处理这张图片。我的图片在可绘制文件夹中。我写的代码是,问题是 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/