How to get resource ID from Image Path?(如何从图像路径中获取资源 ID?)
问题描述
我在 android 中有一个图像路径.该图像存在于设备的 sdcard 上.我怎样才能找到它的resourceID?我需要将它发送到 decodeResource 函数.我正在尝试检测用户从图库中选择的图像上的面孔.我写的代码是
I have an imagepath in android .This image exits on the sdcard of the device. How can i find resourceID of it ? I need to send it to decodeResource function. I am trying to detect faces on the image user selects from the gallery. The code that I have written is
Intent intent = new Intent(this, DetectFaces.class);
intent.putExtra(PICTURE_PATH,picturePath);//the path of the image
startActivity(intent);
在detectFaces类中
In detectFaces class
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detect_faces);
// getActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
picturePath = intent.getStringExtra(GetFaceActivity.PICTURE_PATH);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
//imageView.setOnTouchListener(this);
}
有一个按钮,其onClick事件关联
There is a button whose onClick event is associated with
public void DetectFacesInImage(View view)
{
BitmapFactory.Options bitmapFactoryOptions=new BitmapFactory.Options();
bitmapFactoryOptions.inPreferredConfig= Bitmap.Config.RGB_565;
myBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.faceswapping,bitmapFactoryOptions);
int width=myBitmap.getWidth();
int height=myBitmap.getHeight();
detectedFaces=new FaceDetector.Face[number_of_faces];
FaceDetector faceDetector=new FaceDetector(width,height,number_of_faces);
number_of_faces_detected = faceDetector.findFaces(myBitmap, detectedFaces);
}
我需要 decodeResource 函数中的 ID.有什么提示吗?
I need ID in the decodeResource function. Any hints ?
推荐答案
试试这个,也许对你有帮助
Try this, it may help you
File fileObj = new File("/sdcard/Images/test_image.jpg");
if(fileObj .exists()){
Bitmap bitMapObj= BitmapFactory.decodeFile(fileObj .getAbsolutePath());
ImageView imgView= (ImageView) findViewById(R.id.imageviewTest);
imgView.setImageBitmap(bitMapObj);
}
这篇关于如何从图像路径中获取资源 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从图像路径中获取资源 ID?
基础教程推荐
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
