百度云上传图片

/*百度云上传图片,其实看它的列子就可以,百度云上传图片,第一步可以看我的百度下载图片,第二的步的话很简单,其实就是把要上传的内容写到文件里面去,然后通过BaiduBCSResponse<ObjectMetadata> response = baiduBCS.putObject(request);把数据给带上去。
 * 
 */


private static final org.apache.commons.logging.Log logg = LogFactory

.getLog(MemberoneActivity.class);
// ----------------------------------------
static String host = "bcs.duapp.com";
static String accessKey = "s2p8B6U0iITUIuLltn4EcaTa";//百度云自己弄
static String secretKey = "sZNGKPeeRsqXoBcLMLkr2s7GoBBpEaZq";/百度云自己弄

static String bucket = "test-pictures";

static String object = "/testimage";
private static String putimage = "";

private void init() {
// TODO Auto-generated method stub


if (android.os.Build.VERSION.SDK_INT > 9) {
   StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
   StrictMode.setThreadPolicy(policy);
}

Time t = new Time();
t.setToNow();
int year = t.year;
int month = t.month;
int date = t.monthDay;
int hour = t.hour; // 0-23
int minute = t.minute;
int second = t.second;

object = "/" + muserId + "" + t.year + "" + (t.month + 1) + ""
+ t.monthDay + "" + t.hour + "" + t.minute + "" + t.second;

addbcs() ;//把内容写到文件里面去,然后上传到百度云

}

private void addbcs() {
// TODO Auto-generated method stub

BCSCredentials credentials = new BCSCredentials(accessKey, secretKey);

final BaiduBCS baiduBCS = new BaiduBCS(credentials, host);

baiduBCS.setDefaultEncoding("GBK");
// /baiduBCS.setDefaultEncoding("UTF-8");

String strimage=readFileByLines();

// Log.i("i", "image"+strimage);
PutObjectRequest request = new PutObjectRequest(bucket, object,
createSampleFile(strimage));
ObjectMetadata metadata = new ObjectMetadata();
// metadata.setContentType("text/html");
request.setMetadata(metadata);
BaiduBCSResponse<ObjectMetadata> response = baiduBCS.putObject(request);
ObjectMetadata objectMetadata = response.getResult();
// information.setText(""+objectMetadata+"----response"+response);
logg.info("x-bs-request-id: " + response.getRequestId());
logg.info(objectMetadata);

}

private File createSampleFile(String str) {
// TODO Auto-generated method stub
try {
// String image = "";
/*

*/
// image=new String( ins ,"ISO-8859-1");
// android.util.Log.i("i", image+"i");
/* String image = "";
for (int i = 0; i < ins.length; i++)
{ image=image+ins[i]; }*/
File file = File.createTempFile("java-sdk-", ".txt");
file.deleteOnExit();


Writer writer = new OutputStreamWriter(new FileOutputStream(file));
writer.write(str + "\n");


writer.close();


return file;
} catch (IOException e) {
logg.error("tmp file create failed.");
e.printStackTrace();             
return null;
}


}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现微信小程序中的百度云图片识别,你需要完成以下步骤: 1. 在百度AI开放平台申请一个账号,并创建一个图片识别应用。 2. 在微信小程序中引入百度AI的JavaScript SDK。 3. 在小程序页面中编写相应的代码,调用百度AI的图片识别API实现图片识别的功能。例如,你可以在小程序的某个页面上添加一个按钮,当用户点击按钮后,可以让用户上传一张图片,然后通过百度AI的图片识别API进行识别,并将结果显示在页面上。 下面是一个简单的实现图片识别的代码示例: 1. 在小程序的页面中引入百度AI的JavaScript SDK: ``` import BaiduAI from 'baiduai.js'; const baiduAI = new BaiduAI({ appId: 'your_app_id', apiKey: 'your_api_key', secretKey: 'your_secret_key' }); ``` 2. 在按钮的点击事件中,调用百度AI的图片识别API实现图片识别: ``` onImageUpload: function() { wx.chooseImage({ success: function(res) { wx.showLoading({ title: '正在上传图片...', }) wx.uploadFile({ url: 'https://your_api_service_url', filePath: res.tempFilePaths[0], name: 'image', success: function(res) { const result = JSON.parse(res.data).result; baiduAI.imageClassify(result.image, result.imageType).then((res) => { wx.hideLoading(); wx.showToast({ title: '识别成功!', }) console.log(res); }).catch((err) => { wx.hideLoading(); wx.showToast({ title: '识别失败!', icon: 'none' }) console.log(err); }); } }) } }) } ``` 在这个示例中,我们首先使用微信的chooseImage API让用户选择一张图片,然后使用微信的uploadFile API将图片上传到自己的API服务中。在API服务中,我们调用百度AI的图片识别API,获取识别结果。最后,我们将识别结果显示在页面上。 需要注意的是,在使用百度AI的API时,需要传入正确的参数和密钥,以确保API能够正常工作。同时,需要注意百度AI的API调用次数和频率限制,不要超过限制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值