小智..
7
将您的蒙版图像放在drawable-nodpi文件夹中.
否则,缩放将是错误的.
这是来自应用程序的一些示例代码.在相机之后它添加了一个面具.
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_IMAGE_CAPTURE) // && resultCode == RESULT_OK )
{
try
{
Bitmap cameraBmp = MediaStore.Images.Media.getBitmap(
State.mainActivity.getContentResolver(),
Uri.fromFile(Utils.tempFileForAnImage())
);
cameraBmp = ThumbnailUtils.extractThumbnail(cameraBmp, 256, 256);
Matrix m = new Matrix();
m.postRotate(Utils.neededRotation(Utils.tempFileForAnImage()));
// NOTE incredibly useful trick for cropping/resizing square
// http://stackoverflow.com/a/17733530/294884
cameraBmp = Bitmap.createBitmap(cameraBmp,
0, 0, cameraBmp.getWidth(), cameraBmp.getHeight(),
m, true);
// so, cameraBmp is now a Bitmap. Let's add the mask!!
// see Shiomi Schwartz's original!! http://stackoverflow.com/questions/8630365
Bitmap mask = BitmapFactory.decodeResource(
getResources(),
R.drawable.mask_android_256);
// NOTE THE MASK ** MUST ** BE IN YOUR nodpi folder
Bitmap result = Bitmap.createBitmap( 256,256, Bitmap.Config.ARGB_8888);
Canvas cc = new Canvas();
cc.setBitmap(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
cc.drawBitmap(cameraBmp, 0, 0, null);
cc.drawBitmap(mask, 0,0, paint);
// so, cameraBmp is now a Bitmap but it has been masked
yourImageViewForTheUser.setImageBitmap(result);
// make a "baos" ... we want PNG in this case ..
ByteArrayOutputStream baos = new ByteArrayOutputStream();
result.compress(Bitmap.CompressFormat.PNG, 0, baos);
imageBytesRESULT = baos.toByteArray();
// typically you want the result as image bytes, example to send to Parse
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return;
}
}