如何在捕获的图像上获取当前日期和时间

时间:2016-01-08 11:03:31

标签: android datetime simpledateformat

我正在尝试在捕获的图像上显示日期和时间。我有一个类作为Capturesignature。我们可以在捕获的图像上绘制。如何在捕获的图像上显示日期和时间。

    public class CaptureSignature extends Activity {
       signature mSignature;
       Paint paint;
       LinearLayout mContent;
       Button clear, save,captureImage;   

      @Override
     protected void onCreate(Bundle savedInstanceState) {
     // TODO Auto-generated method stub
     super.onCreate(savedInstanceState);
     setContentView(R.layout.capturesignature);

     save = (Button) findViewById(R.id.save);
     save.setEnabled(false);
     clear = (Button) findViewById(R.id.clear);
     mContent = (LinearLayout) findViewById(R.id.mysignature);
    captureImage = (Button) findViewById(R.id.photo);

    mSignature = new signature(this, null);
    mContent.addView(mSignature,
      LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);

    save.setOnClickListener(onButtonClick);
    clear.setOnClickListener(onButtonClick);


    //Image capture
    captureImage.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (currentImageUri == null) {
                currentImageUri = getImageFileUri();
                System.out.println("imagePath :" + currentImageUri);
             }

            Intent intent = new   
            Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, currentImageUri); 

            // start the image capture Intent
            startActivityForResult(intent, 1);
      }

      });
     }
    Button.OnClickListener onButtonClick = new 
    Button.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == clear) {
            mSignature.clear();
        } else if (v == save) {
            mSignature.save();
        }
       }
   };

   public class signature extends View {
        static final float STROKE_WIDTH = 2f;
        static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
        Paint paint = new Paint();
        Path path = new Path();

    float lastTouchX;
    float lastTouchY;
    final RectF dirtyRect = new RectF();
    private float y;

    public signature(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(STROKE_WIDTH);



    }

    public void clear() {
        path.reset();
        invalidate();
        save.setEnabled(false);
    }



  public void save() {
        Bitmap returnedBitmap = 
        Bitmap.createBitmap(mContent.getWidth(),
                mContent.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);


        try {
            FileOutputStream mFileOutStream = new 
            FileOutputStream(mypath);

            returnedBitmap.compress(Bitmap.CompressFormat.PNG, 90, 
            mFileOutStream); 
            mFileOutStream.flush();
            mFileOutStream.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        Drawable bgDrawable = mContent.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(canvas);
        else
            canvas.drawColor(Color.WHITE);
        mContent.draw(canvas);

        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        returnedBitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
        Intent intent = new Intent();
        intent.putExtra("byteArray", bs.toByteArray());
        setResult(1, intent);
        finish();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        canvas.drawPath(path, paint);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();
        save.setEnabled(true);

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            lastTouchX = eventX;
            lastTouchY = eventY;
            return true;

        case MotionEvent.ACTION_MOVE:

        case MotionEvent.ACTION_UP:

            resetDirtyRect(eventX, eventY);
            int historySize = event.getHistorySize();
            for (int i = 0; i < historySize; i++) {
                float historicalX = event.getHistoricalX(i);
                float historicalY = event.getHistoricalY(i);
                path.lineTo(historicalX, historicalY);
            }
            path.lineTo(eventX, eventY);
            break;
        }

        invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
                (int) (dirtyRect.top - HALF_STROKE_WIDTH),
                (int) (dirtyRect.right + HALF_STROKE_WIDTH),
                (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));

        lastTouchX = eventX;
        lastTouchY = eventY;

        return true;
    }

    private void resetDirtyRect(float eventX, float eventY) {
        dirtyRect.left = Math.min(lastTouchX, eventX);
        dirtyRect.right = Math.max(lastTouchX, eventX);
        dirtyRect.top = Math.min(lastTouchY, eventY);
        dirtyRect.bottom = Math.max(lastTouchY, eventY);
      }
  }



 //code for taking picture taking picture

 //taking picture
            private static Uri getImageFileUri() {
                imagePath = new File(


       Environment.getExternalStoragePublicDirectory
       (Environment.DIRECTORY_PICTURES),
                        "Tuxuri");

                if (!imagePath.exists()) {
                    if (!imagePath.mkdirs()) {
                        Log.d("CameraTestIntent", "failed to create 
                         directory");
                        return null;
                    }
                }

                // Create an image file name
                String timeStamp = new 
                SimpleDateFormat("yyyyMMdd_HHmmss")
                        .format(new Date());
                File image = new File(imagePath, "TUX_" + timeStamp + 
                  ".jpg");

                if (!image.exists()) {
                    try {
                        image.createNewFile();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                 System.out.println("uriPath :" + image + " " + 
                  Uri.fromFile(image));
                // Create an File Uri
                return Uri.fromFile(image);

            }

            protected void onActivityResult(int requestCode, int 
          resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                try {

                    if (requestCode == 1) {

                        // System.out.println("Step-2222 :  " + iview);
                        String img = currentImageUri.toString();
                        String s[] = img.split("/");
                        String s1 = s[s.length - 1];
                        String s2 = "/" + "mnt" + "/" + "sdcard" + "/"
                         + "pictures"
                                + "/" + "Tuxuri" + "/" + s1;
                         System.out.println("Step:" + s2);
                        picturePath = s2;

                        bitampimage = BitmapFactory.decodeFile(s2);
                        bit = Bitmap.createScaledBitmap(bitampimage, 
                        80, 80, true);
                         System.out.println("Step" + picturePath);
                        currentImageUri = null;
                    //settung image 
                        if(bitampimage != null){
                            Drawable d = new 
               BitmapDrawable(getResources(),bitampimage);
                            mSignature.setBackgroundDrawable(d);

                        }
             mContent.addView(mSignature,LayoutParams.FILL_PARENT,  
             LayoutParams.FILL_PARENT);

                                }

                } catch (Exception er) {
                    er.printStackTrace();
                }
            }
        }

我已经尝试过这些代码行,用于获取图像上的日期和时间,但是没有用。所以我该怎么做?

    @Override
    public void onDraw(Canvas canvas){

    canvas.drawBitmap(cameraImage, 0, 0, paint);

   // draw the date
   canvas.drawText("Date string", x, y, paint);

   }

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); 
String currentDateandTime = sdf.format(new Date());   
canvas.drawText(currentDateandTime , 10, 25, paint);

问候。