Hesam
Hesam

Reputation: 53610

android: Loading Image in a class

I my project I have a class that in this class I want to have access to properties of a picture. But I don't want to show it through this class. In this class I just want to know width and height of image and do some mathematical functions to return something.

My problem is that I don't know how should I address this picture. My picture is in drawable folder. The code that I have written is, the problem is 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;
    }
}

Upvotes: 3

Views: 13932

Answers (1)

Zelimir
Zelimir

Reputation: 11038

Image that is stored in your application resources you can fully process via android.graphics.Bitmap

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

Upvotes: 7

Related Questions