1

我正在制作一个可以初始化相机的应用程序,然后在拍照后,可以导入照片并让用户进一步使用它。

编码:

A类:

   public OnClickListener cameraButtonListener = new OnClickListener()   
   {
      @Override
      public void onClick(View v) 
          {  
               vibrate();
               Toast.makeText(Doodlz.this, R.string.message_initalize_camera, Toast.LENGTH_SHORT).show();
               Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
               startActivityForResult(cameraIntent, CAMERA_REQUEST);               
           }      
   }; 

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

    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) 
    {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        Bitmap photocopy = photo.copy(Bitmap.Config.ARGB_8888, true);
        doodleView.get_camera_pic(photocopy);
    }
}

涂鸦视图:

   public void get_camera_pic (Bitmap photocopy)
   {
    // get screen dimension first
      WindowManager wm = (WindowManager) context_new.getSystemService(Context.WINDOW_SERVICE);
      Display display = wm.getDefaultDisplay();
      final int screenWidth = display.getWidth();
      final int screenHeight = display.getHeight(); 
      bitmap = photocopy;
      bitmap = Bitmap.createScaledBitmap(bitmap, screenWidth, screenHeight, true);
      bitmapCanvas = new Canvas(bitmap);
      invalidate(); // refresh the screen      
   }

问题:

使用相机可以成功拍摄照片并返回给doodleView用户。然而,由于导入的图像尺寸非常小,只有缩略图大小!(不知道为什么),所以我厌倦了放大它,然后分辨率很差。

我的问题是,如何修改上面的代码,以便将拍摄的照片尺寸设置为适合屏幕的尺寸,并且返回的照片是屏幕的 1:1,而不是像缩略图一样?(最好适合 1:1 的屏幕,因为如果是作为原始照片尺寸导入,则照片尺寸会大于屏幕,然后需要按比例缩小并按不同的宽度和高度比扭曲以适合完整屏幕)

谢谢!!

4

2 回答 2

3

这是默认相机应用程序的正常现象。获取完整尺寸图像的方法是告诉相机活动将结果放入文件中。首先创建一个文件,然后启动相机应用程序如下:

outputFileName = createImageFile(".tmp");
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outputFileName));
startActivityForResult(takePictureIntent, takePhotoActionCode);

然后在您的 onActivityResult 中,您可以取回此图像文件并对其进行操作。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == takePhotoActionCode)
    {
        if (resultCode == RESULT_OK)
        {
            // NOTE: The intent returned might be NULL if the default camera app was used.
            // This is because the image returned is in the file that was passed to the intent.
                processPhoto(data);
        }
    }
}

processPhoto 看起来有点像这样:

    protected void processPhoto(Intent i)
    {
        int imageExifOrientation = 0;

// Samsung Galaxy Note 2 and S III doesn't return the image in the correct orientation, therefore rotate it based on the data held in the exif.

        try
        {


    ExifInterface exif;
        exif = new ExifInterface(outputFileName.getAbsolutePath());
        imageExifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                                    ExifInterface.ORIENTATION_NORMAL);
    }
    catch (IOException e1)
    {
        e1.printStackTrace();
    }

    int rotationAmount = 0;

    if (imageExifOrientation == ExifInterface.ORIENTATION_ROTATE_270)
    {
        // Need to do some rotating here...
        rotationAmount = 270;
    }
    if (imageExifOrientation == ExifInterface.ORIENTATION_ROTATE_90)
    {
        // Need to do some rotating here...
        rotationAmount = 90;
    }
    if (imageExifOrientation == ExifInterface.ORIENTATION_ROTATE_180)
    {
        // Need to do some rotating here...
        rotationAmount = 180;
    }       

    int targetW = 240;
    int targetH = 320; 

    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(outputFileName.getAbsolutePath(), bmOptions);
    int photoWidth = bmOptions.outWidth;
    int photoHeight = bmOptions.outHeight;

    int scaleFactor = Math.min(photoWidth/targetW, photoHeight/targetH);

    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap scaledDownBitmap = BitmapFactory.decodeFile(outputFileName.getAbsolutePath(), bmOptions);

    if (rotationAmount != 0)
    {
        Matrix mat = new Matrix();
        mat.postRotate(rotationAmount);
        scaledDownBitmap = Bitmap.createBitmap(scaledDownBitmap, 0, 0, scaledDownBitmap.getWidth(), scaledDownBitmap.getHeight(), mat, true);
    }       

    ImageView iv2 = (ImageView) findViewById(R.id.photoImageView);
    iv2.setImageBitmap(scaledDownBitmap);

    FileOutputStream outFileStream = null;
    try
    {
        mLastTakenImageAsJPEGFile = createImageFile(".jpg");
        outFileStream = new FileOutputStream(mLastTakenImageAsJPEGFile);
        scaledDownBitmap.compress(Bitmap.CompressFormat.JPEG, 75, outFileStream);
    }
    catch (Exception e)
    {
        e.printStackTrace();
            }
    }

需要注意的一件事是,在 Nexus 设备上,调用活动通常不会被破坏。然而,在三星 Galaxy S III 和 Note 2 设备上,呼叫活动被破坏。因此,仅将 outputFileName 作为成员变量存储在 Activity 中将导致相机应用返回时它为空,除非您记得在 Activity 终止时保存它。无论如何,这样做是一个好习惯,但这是我以前犯过的一个错误,所以我想我会提到它。

编辑:

关于您的评论,createImageFile 不在标准 API 中,它是我写的(或者我可能借用了 :-),我不记得了),这里是 createImageFile() 的方法:

    private File createImageFile(String fileExtensionToUse) throws IOException 
{

    File storageDir = new File(
            Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES
            ), 
            "MyImages"
        );      

    if(!storageDir.exists())
    {
        if (!storageDir.mkdir())
        {
            Log.d(TAG,"was not able to create it");
        }
    }
    if (!storageDir.isDirectory())
    {
        Log.d(TAG,"Don't think there is a dir there.");
    }

    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "FOO_" + timeStamp + "_image";

    File image = File.createTempFile(
        imageFileName, 
        fileExtensionToUse, 
        storageDir
    );

    return image;
}    
于 2013-02-09T14:40:13.133 回答
1

要访问完整图像,您需要通过data.getData()在 doodleView 中使用来访问意图 URI,或者(更好)通过额外提供 URI 将图像传递给意图来提供您自己的 URI 来存储图像EXTRA_OUTPUT

于 2013-02-09T12:28:27.270 回答