问题
I'm using Google's CameraPreview sample demo. Here, I have the cameraPreview, and an overlayed image over cameraPreview.
When I take the photo, only the cameraPreview is saved, but I need the overlay image to be saved too over the cameraPreview.
This is the way I save cameraPreview's photo, but I need to know what would be the best way for overlaying booth images and save them into one.
public void onPictureTaken(byte[] data, Camera camera) {
String photoPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/miFoto.jpg";
try {
FileOutputStream fos = new FileOutputStream(photoPath);
fos.write(data);
fos.close();
} catch (IOException e) {
Toast.makeText(this, "Pic not saved", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "Pic saved in: " + photoPath, Toast.LENGTH_SHORT).show();
camera.startPreview();
}
回答1:
I finally made it this way:
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Bitmap cameraScaledBitmap = Bitmap.createScaledBitmap(cameraBitmap, 1280, 720, true);
int wid = cameraScaledBitmap.getWidth();
int hgt = cameraScaledBitmap.getHeight();
Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);
Bitmap overlayScaledBitmap = Bitmap.createScaledBitmap(overlayBitmap, wid, hgt, true);
Canvas canvas = new Canvas(newImage);
canvas.drawBitmap(cameraScaledBitmap , 0, 0, null);
canvas.drawBitmap(overlayScaledBitmap , 0, 0, null);
File storagePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
storagePath.mkdirs();
String finalName = Long.toString(System.currentTimeMillis());
File myImage = new File(storagePath, finalName + ".jpg");
String photoPath = Environment.getExternalStorageDirectory().getAbsolutePath() +"/" + finalName + ".jpg";
try {
FileOutputStream fos = new FileOutputStream(myImage);
newImage.compress(Bitmap.CompressFormat.JPEG, 80, fos);
fos.close();
} catch (IOException e) {
Toast.makeText(this, "Pic not saved", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "Pic saved in: " + photoPath, Toast.LENGTH_SHORT).show();
camera.startPreview();
newImage.recycle();
newImage = null;
cameraBitmap.recycle();
cameraBitmap = null;
}
来源:https://stackoverflow.com/questions/22476683/save-camerapreview-with-overlay-image