由於臉部檢測器僅適用於jpg文件,您如何使它適用於所有格式?我讓用戶從SD卡中選擇任何圖像,以便圖像可以是任何格式。比我用decodefile方法,在這篇文章中提到的縮小圖片: Android: Resize a large bitmap file to scaled output file使臉部檢測器適用於任何文件格式
我的問題是:
我們可以添加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");
}
}
}
解碼方法:
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;
}
請發佈您現在使用的代碼。 –
@MichaelPerrenoud - 上面的代碼。 – NoviceMe
所有格式與Java(GIF,PNG,BMP,WBMP)或其他格式(ICO,TIFF等)支持的格式一樣嗎?另外,你可以更具體地瞭解你正在尋找哪種圖像格式,或者它是從字面上來看所有的圖像?我假設這些會是光柵? –