【问题标题】:How can i download some image libraries to android?如何将一些图像库下载到 android?
【发布时间】:2014-09-07 07:38:29
【问题描述】:

我想做的是在 android 中读取一些图像并将图像中的每个像素转换为 RGB 值,这是我找到的代码:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button test=(Button)findViewById(R.id.button1);
        test.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                BufferedImage bi=ImageIO.read(new File("C:\\images\\Sunset.jpg"));
                int[] pixel;

                for (int y = 0; y < bi.getHeight(); y++) {
                    for (int x = 0; x < bi.getWidth(); x++) {
                        pixel = bi.getRaster().getPixel(x, y, new int[3]);
                        System.out.println(pixel[0] + " - " + pixel[1] + " - " + pixel[2] + " - " + (bi.getWidth() * y + x));
                    }
                }
            }
        });

    }
}

但是我在导入一些这样的包时遇到了问题:

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

而且我不知道如何下载它们或解决此问题的最佳方法.. 有帮助吗??

【问题讨论】:

标签: android awt javax.imageio


【解决方案1】:

第一个从文件创建位图。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);

接下来你可以从你的位图中getPixel()

for (int y = 0; y < bitmap.getHeight(); y++) {
    for (int x = 0; x < bitmap.getWidth(); x++) {
        android.util.Log.v("TAG", bitmap.getPixel(x,y));
    }
}

【讨论】:

  • 但是我应该如何配置 photopath?
  • photoPath 是一个字符串,与我的答案中的 filePath 相同。另请注意,在我的示例中,使用 readPixels 一次读取所有像素会更有效。
  • String photopath = android.os.Environment.getExternalStorageDirectory() + "your_photo.jpg";
  • 好的,但我想在模拟器上运行它,我的图像存在于 C 驱动器中,如下所示: C:\\images\\download.jpg ,它会工作吗? @user2038145
  • 在 Eclipse 中使用 adb push 或 DDMS
【解决方案2】:

BufferedImage 或 ImageIO 在 android 上不可用。

使用Bitmap代替BufferedImage,看看getPixels函数。

代替ImageIO使用BitmapFactory,你需要的函数是decodeFile


BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, opts);

int pixels[] = new int[bitmap.getWidth()*bitmap.getHeight()];

bitmap.getPixels(pixels,0,0,0,0,bitmap.getWidth(),bitmap.getHeight());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多