Nov*_*eMe 2 java android bitmap
由于面部检测器仅适用于jpg文件,如何使其适用于所有格式?我让用户从SD卡中选择任何图像,因此图像可以是任何格式.比我在本文中提到的使用decodefile方法缩小图像: Android:将大型位图文件调整为缩放输出文件
我的问题是:
我们可以在decodefile方法中添加一些方法来首先将图像转换为jpg吗?
将图像转换为jpg的最佳和最有效的方法是什么,以便面部检测器类检测面部?
我的代码到目前为止:
public FaceView(Context context) {
super(context);
File photo = new File(selectedImagePath);
sourceImage = decodeFile(photo);
picWidth = sourceImage.getWidth();
picHeight = sourceImage.getHeight();
sourceImage = Bitmap.createScaledBitmap (sourceImage, picWidth, picHeight, false);
arrayFaces = new FaceDetector( picWidth, picHeight, NUM_FACES );
arrayFaces.findFaces(sourceImage, getAllFaces);
for (int i = 0; i < getAllFaces.length; i++)
{
getFace = getAllFaces[i];
try {
PointF eyesMP = new PointF();
getFace.getMidPoint(eyesMP);
eyesDistance[i] = getFace.eyesDistance();
Log.d("1st eye distance", "" + getFace.eyesDistance());
eyesMidPts[i] = eyesMP;
}
catch (Exception e)
{
if (DEBUG) Log.e("Face", i + " is null");
}
}
}
Run Code Online (Sandbox Code Playgroud)
解码方法:
private Bitmap decodeFile(File f){
Bitmap b = null;
int screenWidth = 0;
int screenHeight = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
{
w.getDefaultDisplay().getSize(size);
screenWidth = size.x;
screenHeight = size.y;
}
else
{
Display d = w.getDefaultDisplay();
screenWidth = d.getWidth();
screenHeight = d.getHeight();
}
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = null;
try{
fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
// was > IMAGE_MAX_SIZE for both i changed it****
int scale = 1;
if (o.outHeight > screenHeight || o.outWidth > screenWidth) {
scale = (int)Math.pow(2, (int) Math.round(Math.log(screenHeight /
(double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return b;
}
Run Code Online (Sandbox Code Playgroud)
在decodeFile()方法结束时,您有一个位图.您可以轻松地依赖Android的将其转换成一个JPEG使用
Bitmap.compress(Bitmap.CompressFormat format, int quality, OutputStream stream)
Run Code Online (Sandbox Code Playgroud)
同 Bitmap.CompressFormat.JPEG
这里有更多信息:在android中将位图转换为jpeg
您应该能够将其保存到文件中,或将其压缩到内存中(可能使用ByteArrayOutputStream?)
| 归档时间: |
|
| 查看次数: |
321 次 |
| 最近记录: |