简体   繁体   English

java flickr和flickrj下载用户图片

[英]java flickr and flickrj download user pictures

Hi I am new to flickrj library. 嗨,我是flickrj库的新手。
Have foundational java knowledge though. 虽然有基础的Java知识。
The project that I am working on requires me to authenticate into flickr and then download geo-tagged images into a folder in local hard drive. 我正在从事的项目要求我先对flickr进行身份验证,然后将带有地理标签的图像下载到本地硬盘驱动器的文件夹中。 The program will be Desktop application program. 该程序将是桌面应用程序。
I am approaching the program by breaking down into 3 steps. 我将程序分为3个步骤进行处理。



1.Proper authentication to be completed.(which i have succeeded) 1.完成正确的身份验证。(我已经成功了)
2.Try to download all the photos that user has when authenticated. 2.尝试下载用户通过身份验证后拥有的所有照片。
3.Try to alter the code a little so that it will only download geo-tagged images. 3.尝试稍微修改一下代码,使其仅下载带有地理标签的图像。

My problems is on step 2. I cant download logged-in user images let alone geo-tagged ones. 我的问题出在步骤2上。我无法下载已登录的用户图像,更不用说带有地理标签的用户图像了。 I am trying the code provided by Daniel Cukier here 我想丹尼尔Cukier提供的代码在这里
But I am running into problem. 但是我遇到了问题。 My netbeans simply strike off at the line 77 on .getOriginalAsStream() part, with the error "java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.io.ByteArrayOutputStream.write" 我的netbeans只是从.getOriginalAsStream()部分的第77行开始,出现错误“ java.lang.RuntimeException:无法编译的源代码-错误的符号类型:java.io.ByteArrayOutputStream.write”
From my understanding netbeans striking off a line means , it is depreciated but shouldnt it still work? 根据我的理解,netbeans下线意味着,它已经贬值了,但是它仍然可以正常工作吗? What is holding this whole problem back? 是什么阻碍了整个问题?

I have tried researching and basically I have to admit , it is beyond my capability to trouble shoot. 我已经尝试过研究,但基本上我必须承认,这是我无法解决问题的能力。 If anyone has any idea on what i am doing wrong , I would be so grateful. 如果有人对我做错了什么有任何想法,我将非常感激。
Ps: I am not looking to be spoon fed but please answer me in idiot-friendly way as I am still a student and my java isn't the greatest. 附:我不希望被人喂,但请以白痴友好的方式回答我,因为我仍然是学生,而我的Java并不是最好的。

This code is what I have so far. 到目前为止,这是我的代码。

import com.aetrion.flickr.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Properties;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.aetrion.flickr.auth.Auth;
import com.aetrion.flickr.auth.AuthInterface;
import com.aetrion.flickr.auth.Permission;
import com.aetrion.flickr.photos.Photo;
import com.aetrion.flickr.photos.PhotoList;
import com.aetrion.flickr.photos.PhotosInterface;
import com.aetrion.flickr.util.IOUtilities;
import java.io.*;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;


public class authenticate {
Flickr f;
RequestContext requestContext;
String frob = "";
String token = "";
Properties properties = null;

public authenticate() throws ParserConfigurationException, IOException, SAXException {
    InputStream in = null;
    try {
        in = getClass().getResourceAsStream("/setup.properties");
        properties = new Properties();

        properties.load(in);
    } finally {
        IOUtilities.close(in);
    }
    f = new Flickr(
        properties.getProperty("apiKey"),
        properties.getProperty("secret"),
        new REST()
    );
    Flickr.debugStream = false;
    requestContext = RequestContext.getRequestContext();
    AuthInterface authInterface = f.getAuthInterface();
    try {
        frob = authInterface.getFrob();
    } catch (FlickrException e) {
        e.printStackTrace();
    }
    System.out.println("frob: " + frob);
    URL url = authInterface.buildAuthenticationUrl(Permission.DELETE, frob);
    System.out.println("Press return after you granted access at this URL:");
    System.out.println(url.toExternalForm());
    BufferedReader infile =
      new BufferedReader ( new InputStreamReader (System.in) );
    String line = infile.readLine();
    try {
        Auth auth = authInterface.getToken(frob);
        System.out.println("Authentication success");
        // This token can be used until the user revokes it.
        System.out.println("Token: " + auth.getToken());
        System.out.println("nsid: " + auth.getUser().getId());
        System.out.println("Realname: " + auth.getUser().getRealName());
        System.out.println("Username: " + auth.getUser().getUsername());
        System.out.println("Permission: " + auth.getPermission().getType());

        PhotoList list = f.getPhotosetsInterface().getPhotos("72157629794698308", 100, 1);
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
        Photo photo = (Photo) iterator.next();
        File file = new File("/tmp/" + photo.getId() + ".jpg");
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        b.write(photo.getOriginalAsStream());
        FileUtils.writeByteArrayToFile(file, b.toByteArray());
}






    } catch (FlickrException e) {
        System.out.println("Authentication failed");
        e.printStackTrace();
    }
}




public static void main(String[] args) {
    try {
        authenticate t = new authenticate();
    } catch(Exception e) {
        e.printStackTrace();
    }
    System.exit(0);
}

} }

You are correct in your interpretation of the strikeout that getOriginalAsStream() is deprecated. 您对不赞成使用getOriginalAsStream()的删除规则的解释是正确的。 It looks like you might want to rework your code to use PhotosInterface.getImageAsStream() , passing the ORIGINAL size as one of the arguments. 看来您可能需要重新编写代码以使用PhotosInterface.getImageAsStream() ,并将ORIGINAL大小作为参数之一传递。

To adjust NetBeans' behavior with respect to deprecated methods, you can follow the link recommended by @AljoshaBre as well as this one . 要调整NetBeans的相对于不赞成的方法行为,可以遵循@AljoshaBre以及推荐的链接这一个

If you want download all your photos from Flickr, this is possible if you have a mac computer. 如果要从Flickr下载所有照片,则如果有Mac计算机,则可以这样做。
Download Aperture program on Apple Store and install it. 在Apple Store上下载并安装Aperture程序。
After to install, open the Aperture. 安装后,打开Aperture。
Go on preferences. 继续进行偏好设置。
Click on 'Accounts' tab. 点击“帐户”标签。
Click on plus sign (+) on bottom left to add a photo service. 单击左下角的加号(+)添加照片服务。
Add the Flicker option. 添加闪烁选项。
Follow the login and authorization instructions. 请遵循登录和授权说明。
Done! 做完了! All your photos will be synchronized in you aperture library locate on ~/images/ 您所有的照片将在〜/ images /中的光圈库中同步

I hope I have helped. 我希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM