<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS80UFZMcS5qcGcifQ==” alt =“在此处输入图像描述”>
如何显示时间和日期,如下图所示。我有一个捕获图像的代码。但是我如何在捕获图像时显示时间和日期。这是启动相机应用程序的代码
我的代码:
public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
这里将您拥有的图像拍摄到画布上。现在,可以使用[>]之类的东西在画布上写一些东西来修改图像。
Paint paint = new Paint(); paint.setColor(Color.WHITE); paint.setStyle(Style.FILL); canvas.drawPaint(paint); paint.setColor(Color.BLACK); paint.setTextSize(20); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); String currentDateandTime = sdf.format(new Date()); canvas.drawText(currentDateandTime , 10, 25, paint);
这可以帮助您将文本绘制到图像上。试试看。
imagename = String.valueOf(System.currentTimeMillis());
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String path = Environment.getExternalStorageDirectory() + "/Pictures/" + "pic_" + imagename + ".jpg";
File file = new File(path);
imageUri = Uri.fromFile(file);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
//on activity Result
Canvas canvas = new Canvas(drawableBitmap);
canvas.drawBitmap(drawableBitmap, 0, 0, null);
Paint paint = new Paint();
paint.setColor(getResources().getColor(R.color.Orange));
paint.setTextSize(22);
DateFormat dateFormatter1 = new SimpleDateFormat("dd-MM-yyyy");
DateFormat dateFormatter2 = new SimpleDateFormat("hh:mm:ss");
dateFormatter1.setLenient(false);
dateFormatter2.setLenient(false);
java.util.Date today = new java.util.Date();
// java.util.Timer;
String d = dateFormatter1.format(today);
String t = dateFormatter2.format(today);
canvas.drawText("" + d + ", " + t, 20f , loadedBitmap.getHeight() - 24, paint);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
drawableBitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
String path = Environment.getExternalStorageDirectory()+ "/Pictures/" + "pic_" + imagename + ".jpg";
File file = new File(path);
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
尝试此代码。为我节省了很多精力。