当前位置: 首页 > 知识库问答 >
问题:

检测来自URL的图像方向

齐意致
2023-03-14

我知道如何检测和更改图像的方向,这是使用ExifInterface从设备捕获的。现在我在想,我该如何检测来自URL而非设备的图像方向?

例如,我有一个ImageLoader类,该类具有DisplayImage函数,可以将位图从URL设置为ImageView:

保存图像类

public class ImageLoader {

    MemoryCache memoryCache = new MemoryCache();
    FileCache fileCache;
    private Map<ImageView, String> imageViews = Collections
            .synchronizedMap(new WeakHashMap<ImageView, String>());
    ExecutorService executorService;

    public ImageLoader(Context context) {
        fileCache = new FileCache(context);
        executorService = Executors.newFixedThreadPool(5);
    }

    int stub_id;

    public void DisplayImage(String url,
    // int loader,
            ImageView imageView) {
        // stub_id = loader;
        imageViews.put(imageView, url);
        Bitmap bitmap = memoryCache.get(url);
        if (bitmap != null)
            imageView.setImageBitmap(bitmap);
        else {
            queuePhoto(url, imageView);
            // imageView.setImageResource(loader);
        }
    }

    private void queuePhoto(String url, ImageView imageView) {
        PhotoToLoad p = new PhotoToLoad(url, imageView);
        executorService.submit(new PhotosLoader(p));
    }

    private Bitmap getBitmap(String url) {
        File f = fileCache.getFile(url);

        // from SD cache
        Bitmap b = decodeFile(f);
        if (b != null)
            return b;

        // from web
        try {
            Bitmap bitmap = null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imageUrl
                    .openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is = conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            bitmap = decodeFile(f);

            return bitmap;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    // decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f) {
        try {
            // decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE = 70;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE
                        || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {
        }
        return null;
    }

    // Task for the queue
    private class PhotoToLoad {
        public String url;
        public ImageView imageView;

        public PhotoToLoad(String u, ImageView i) {
            url = u;
            imageView = i;
        }
    }

    class PhotosLoader implements Runnable {
        PhotoToLoad photoToLoad;

        PhotosLoader(PhotoToLoad photoToLoad) {
            this.photoToLoad = photoToLoad;
        }

        @Override
        public void run() {
            if (imageViewReused(photoToLoad))
                return;
            Bitmap bmp = getBitmap(photoToLoad.url);
            memoryCache.put(photoToLoad.url, bmp);
            if (imageViewReused(photoToLoad))
                return;
            BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
            Activity a = (Activity) photoToLoad.imageView.getContext();
            a.runOnUiThread(bd);
        }
    }

    boolean imageViewReused(PhotoToLoad photoToLoad) {
        String tag = imageViews.get(photoToLoad.imageView);
        if (tag == null || !tag.equals(photoToLoad.url))
            return true;
        return false;
    }

    // Used to display bitmap in the UI thread
    class BitmapDisplayer implements Runnable {
        Bitmap bitmap;
        PhotoToLoad photoToLoad;

        public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
            bitmap = b;
            photoToLoad = p;
        }

        public void run() {
            if (imageViewReused(photoToLoad))
                return;
            if (bitmap != null)
                photoToLoad.imageView.setImageBitmap(bitmap);
            else
                photoToLoad.imageView.setImageResource(stub_id);
        }
    }

    public void clearCache() {
        memoryCache.clear();
        fileCache.clear();
    }

}

在活动中调用的DisplayFunction,用于从URL在ImageView中显示图像:

                        image_url = http://xxxx/bla.png;
                        image = (ImageView) findViewById(R.id.image);

                       ImageLoader imgLoader = new ImageLoader(this);

                        imgLoader.DisplayImage(image_url, image);

这就是我迄今为止所尝试的:

在DisplayImage方法内的ImageLoader类中:

public void DisplayImage(String url,
    // int loader,
            ImageView imageView) {
        // stub_id = loader;
        imageViews.put(imageView, url);
        Bitmap bitmap = memoryCache.get(url);
        if (bitmap != null) {

            int w = bitmap.getWidth();
            int h = bitmap.getHeight();

            if (w > h) {
                m = 90;

                matrix = new Matrix();

                matrix.postRotate(m);
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                        bitmap.getHeight(), matrix, true);

                imageView.setImageBitmap(bitmap);
            } else {

                imageView.setImageBitmap(bitmap);
            }

        } else {
            queuePhoto(url, imageView);
            // imageView.setImageResource(loader);
        }
    }

共有1个答案

岳承悦
2023-03-14

获取宽度和高度,并进行相应检查。

 int w = bitmap.getWidth(); 
 int h = bitmap.getHeight();

if (w > h) then it's landscape
else is portrait.
 类似资料:
  • 本文向大家介绍C#图像边缘检测(Roberts)的方法,包括了C#图像边缘检测(Roberts)的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#图像边缘检测(Roberts)的方法。分享给大家供大家参考。具体如下: 希望本文所述对大家的C#程序设计有所帮助。

  • 我想知道如何使用OpenCV在我的摄像机上检测图像。该图像可以是500个图像中的一个。 我此刻正在做的事: 我想要检测的图像是2-5KB小的。很少有人在上面发短信,但其他的只是一些迹象。这里有一个例子: 你们知道我怎么做吗?

  • 我正在为Waze开发开源JavaScript插件——著名的免费GPS导航器——专门为在线编辑器开发。这个用户脚本的想法是可以快速选择大的统一颜色的地图区域,将它们转换为地标。 到目前为止,我已经成功地在Photoshop这样的图形编辑器中实现了一个你称之为“魔杖”的工具:用户点击地图上的某个地方(比如,在湖泊或森林上),脚本选择相同颜色覆盖的整个区域,并为地标创建一个多边形。 一切都很好,除了我使

  • 在相机意图返回时获得文件路径的最佳方法是什么。当然,我可以在启动意图之前将文件路径保存到成员变量,但这似乎不好,我应该从OnActivityResult中获得路径。 我已经试过了(从相机意图获取图像路径): 唯一不同的是,我在尝试内部存储pic时使用了INTERNAL_CONTENT_URI。如果我这样做,我会得到一个异常:

  • 问题内容: 嗨,我的数据库中有一个图像表。这些与细节一起存储为Blob,例如图像类型和名称。 我在显示图像时遇到问题,我得到的只是一个带有红色十字的白框。码: 谢谢 问题答案: 好吧,这是一个简短的答案。 检查您的Blob类型是否至少为MEDIUMBLOB,它能够存储高达16M的数据

  • 本文向大家介绍C#图像处理之头发检测的方法,包括了C#图像处理之头发检测的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#图像处理之头发检测的方法。分享给大家供大家参考。具体如下: 结果图像效果: 希望本文所述对大家的C#程序设计有所帮助。