来自相机的android位图只是缩略图大小?

时间:2013-02-09 12:17:12

标签: android android-intent bitmap camera

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

编码:

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);
    }
}

doodleView:

   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的屏幕,因为如果它然后作为原始照片尺寸导入,则照片尺寸大于屏幕,则需要按比例缩小并通过不同的宽高比率变形以适合完整屏幕)

谢谢!

2 个答案:

答案 0 :(得分:2)

这对于默认的相机应用程序来说是正常的。获取完整大小图像的方法是告诉相机活动将结果放入文件中。首先创建一个文件然后启动相机应用程序,如下所示:

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中将导致在相机应用程序返回时它为null,除非您记得在活动终止时保存它。无论如何这样做是很好的做法,但这是我之前犯过的错误所以我想我会提到它。

修改

关于你的评论,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;
}    

答案 1 :(得分:0)

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