我在amy应用程序中实现了android Camera功能。
现在我想将任何transperent图像与之前捕获的相机Image相结合,将图像保存到图库中,然后如何完成?
任何代码都会得到真正的回复。 感谢。
编辑: 我正在使用此代码捕获并保存图像所以,我必须如何实施这些事情。
takePhotoBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) { // <5>
ImageCaptureCallback iccb = null;
try {
String filename = timeStampFormat.format(new Date());
ContentValues values = new ContentValues();
values.put(Media.TITLE, filename);
values.put(Media.DESCRIPTION, "Image capture by camera");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
iccb = new ImageCaptureCallback( getContentResolver().openOutputStream(uri));
// to put the Image on captured image.
Canvas canvas = new Canvas();
Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);
canvas.drawColor(Color.argb(160, 21, 140, 21));
canvas.drawBitmap(kangoo, 130, 10, null);
} catch(Exception ex ){
ex.printStackTrace();
Log.e(getClass().getSimpleName(), ex.getMessage(), ex);
}
camera.takePicture(mShutterCallback, mPictureCallbackRaw, iccb);
}
});
Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
System.out.println("\n\n\n\nThe Data in mPictureCallbackRaw is :"+data);
//camera.startPreview(); // Added StastPreview();
}
};
Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = " + data);
System.out.println("\n\n\n\nThe Data in mPictureCallbackJPEG is :"+data);
camera.startPreview();
}
};
Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
public void onShutter() {
Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
}
};
public class ImageCaptureCallback implements PictureCallback {
private OutputStream filoutputStream;
public ImageCaptureCallback(OutputStream filoutputStream) {
this.filoutputStream = filoutputStream;
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
Log.v(getClass().getSimpleName(), "onPictureTaken=" + data + " length = " + data.length);
filoutputStream.write(data);
filoutputStream.flush();
filoutputStream.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
答案 0 :(得分:3)
尝试将两个位图组合成相机图像和透明图像。这是SDCard中图像和存储的结合。
public Bitmap combineImages(Bitmap c, Bitmap s) {
Bitmap cs = null;
int width, height = 0;
if (c.getWidth() > s.getWidth()) {
width = c.getWidth();
height = c.getHeight();
} else {
width = s.getWidth() + s.getWidth();
height = c.getHeight();
}
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
comboImage.drawBitmap(c, 0, 0, null);
comboImage.drawBitmap(s, 100, 300, null);
/******
*
* Write file to SDCard
*
* ****/
String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
OutputStream os = null;
try {
os = new FileOutputStream(Environment.getExternalStorageDirectory()
+ "/"+tmpImg);
cs.compress(CompressFormat.PNG, 100, os);
} catch (IOException e) {
Log.e("combineImages", "problem combining images", e);
}
return cs;
}
答案 1 :(得分:1)
Android实现了多种图像合成算法。这是一种使用它们的简单方法。我没有在这台计算机上进行eclipse,因此以下代码未经测试(但应该可以工作或至少接近工作)。
img1和img2都是位图,其中一个是从相机中捕获的。
1)创建一个新的空位图。我假设img1和img2的大小相同。如果没有,您可以调整它们的大小,或者使这个新的位图具有最大的位图大小。
Bitmap compositeImage = Bitmap.createBitmap(img1.getWidth(),
img1.getWidth(), img1.getContig());
2)创建一个用于在位图上绘图的画布
Canvas canvas = new Canvas(compositeImage);
3)将第一张图片绘制到新位图
上Paint paint = new Paint();
canvas.drawBitmap(img1, 0.0f, 0.0f, paint);
4)Android在SDK中有一组合成算法。这些方法以Porter和Duff命名,他写了一篇描述合成图像算法的论文。 Android将此设置称为“transfermode”。要查看可用的Porter-Duff传输模式,请转到http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html或谷歌搜索。根据您的需要,我建议您选择Multiply,Darken和Lighten。
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.MULTIPLY));
5)现在设置了transfermode,只需绘制第二张图像即可获得想要的效果
canvas.drawBitmap(img2, 0.0f, 0.0f, paint);
现在,compositeImage Bitmap包含组合图像。
编辑。好的,这里是代码放在一起,也许你可以把它放在你的PictureCallback(jpeg一个,而不是原始的)。同样,它没有经过测试,但应该是好的或接近好的:
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap img1 = BitmapFactory.decodeResource(getResources(),
R.drawable.icon); // assuming you really want to use the icon
Bitmap img2 = BitmapFactory.decodeByteArray(data, 0, data.length);
Bitmap composite = Bitmap.createBitmap(img2.getWidth(), img2.getHeight(),
img2.getConfig());
Canvas canvas = new Canvas(composite);
Paint paint = new Paint();
canvas.drawBitmap(img2, 0.0f, 0.0f, paint);
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.MULTIPLY));
canvas.drawBitmap(img1, 0.0f, 0.0f, paint);
// It seems you want to insert the new image into the gallery
ContentValues values = ...
try {
Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
composite.compress(Bitmap.CompressFormat.JPEG, 90,
getContentResolver().openOutputStream(imageFileUri));
} catch(Exception) {
...
}
}