0

我一直在尝试改进这个简单的应用程序,它将文本(“hola”)添加到 Android 相机应用程序拍摄的照片中,并将图像保存到 sd 卡。

但是,我只设法从返回的数据中将文本添加到缩略图中,并将文件保存到 sd 卡。

谁能指出我正确的方向来做完全相同的事情,但使用全尺寸照片?

非常感谢!!

到目前为止我所拥有的:(使用教程中的代码和stackoverflow上类似问题的答案)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button captureBtn = (Button)findViewById(R.id.capture_btn);
    captureBtn.setOnClickListener(this);

}

public void onClick(View v) {
    if (v.getId() == R.id.capture_btn) {

        try {
            Intent tomaFotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(tomaFotoIntent, CAMERA_CAPTURE);
        } 
        catch(ActivityNotFoundException anfe){
            String errorMessage = "Device doesn't support capturing images";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        //user is returning from capturing an image using the camera
        if(requestCode == CAMERA_CAPTURE){
            //get the Uri for the captured image
            picUri = data.getData();
            //get the returned data
            Bundle extras = data.getExtras();
            //get the cropped bitmap
            Bitmap thePic = extras.getParcelable("data");
            //agregamos texto
            bmConTexto = writeTextOnDrawable(thePic, "hola");

            //guardamos la nueva imagen
            saveBitmap(bmConTexto.getBitmap());

            //retrieve a reference to the ImageView
            ImageView picView = (ImageView)findViewById(R.id.picture);
            //display the returned cropped image
            picView.setImageBitmap(bmConTexto.getBitmap());
        }
    }
}

private BitmapDrawable writeTextOnDrawable(Bitmap thePic, String text) {

    Bitmap bm = thePic.copy(Bitmap.Config.ARGB_8888, true);
    Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);

    Paint paint = new Paint();
    paint.setStyle(Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTypeface(tf);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(20);

    Rect textRect = new Rect();
    paint.getTextBounds(text, 0, text.length(), textRect);

    Canvas canvas = new Canvas(bm);
    //Calculate the positions
    int xPos = (canvas.getWidth() / 2) - 2;     //-2 is for regulating the x position offset
    //"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;  
    canvas.drawText(text, xPos, yPos, paint);
    return new BitmapDrawable(getResources(), bm);


}

public void saveBitmap(Bitmap bm)
{
    try
    {
        String mBaseFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/";
        String mFilePath = mBaseFolderPath + "abcd.jpg";

        FileOutputStream stream = new FileOutputStream(mFilePath);
        bm.compress(CompressFormat.JPEG, 100, stream);
        stream.flush();
        stream.close();
    }
    catch(Exception e)
    {
        Log.e("Could not save", e.toString());
    }
}
4

2 回答 2

1

听起来您正在尝试拍照并在其上写下“Hola”,如果是这种情况,您是否尝试过查看takePicture()android camera api?如果不是,你能详细说明这个问题吗?

编辑:从上面链接的页面顶部粘贴:

要使用此类拍照,请使用以下步骤:

  1. 从 open(int) 获取 Camera 的实例。
  2. 使用 getParameters() 获取现有(默认)设置。
  3. 如有必要,修改返回的 Camera.Parameters 对象并调用 setParameters(Camera.Parameters)。
  4. 如果需要,请调用 setDisplayOrientation(int)。
  5. 重要提示:将完全初始化的 SurfaceHolder 传递给 setPreviewDisplay(SurfaceHolder)。没有表面,相机将无法开始预览。
  6. 重要提示:调用 startPreview() 开始更新预览图面。必须先开始预览,然后才能拍照。
  7. 如果需要,请调用 takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback) 来拍摄照片。等待回调提供实际的图像数据。
  8. 拍照后,预览显示将停止。要拍摄更多照片,请先再次调用 startPreview()。
  9. 调用 stopPreview() 以停止更新预览图面。
  10. 重要提示:调用 release() 以释放相机以供其他应用程序使用。应用程序应立即在 onPause() 中释放相机(并在 onResume() 中重新打开()它)。
于 2012-12-25T17:27:25.593 回答
0

您的代码似乎只是正确的?您面临的问题是什么?您从相机缩略图中获得的图像大小是多少?你可以尝试使用

Bitmap thePic = (Bitmap) data.getExtras().get("data"); 

代替

Bitmap thePic = extras.getParcelable("data");

更新:
您可以直接返回位图本身并检查,而不是返回 BitmapDrawable。只需将您的最后一行更改为

return bm;
于 2012-12-25T18:02:37.743 回答