【问题标题】:Getting low quality image from camera从相机获取低质量图像
【发布时间】:2015-04-07 06:14:31
【问题描述】:

我在最大设备中得到低质量的图片,但在 Redmi 1s 中,我在从相机拍摄后得到暗或黑色的图像。

我想做 3 份工作

  1. 从相机捕捉图像
  2. 捕获原始或高质量图像
  3. 将其设置为我设备的背景

我的代码是:

   final int CAMERA_CAPTURE = 1;
    final int CROP_PIC = 2;
    private Uri picUri;
    Bitmap setphoto;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);

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


    ((Button) findViewById(R.id.setimage))
    .setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast var;
            if(setphoto != null) 
            { 
                    Bitmap image = setphoto;                            

                    WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
                    try {
                        myWallpaperManager.setBitmap(image);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block 
                        var = Toast.makeText(Camera.this, "Home Screen Not Changed",Toast.LENGTH_SHORT);
                           var.show();
                        e.printStackTrace();
                    }

                var = Toast.makeText(Camera.this, "Home Screen Changed",Toast.LENGTH_SHORT);
                   var.show();
           }
            else {
                var = Toast.makeText(Camera.this, "Pick Image From Gallery",Toast.LENGTH_SHORT);
                   var.show();
            }

        }});
        };



    public void onClick(View v) {
        if (v.getId() == R.id.camera) {
            try {
                // use standard intent to capture an image
                Intent captureIntent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
                // we will handle the returned data in onActivityResult
                startActivityForResult(captureIntent, CAMERA_CAPTURE);
            } catch (ActivityNotFoundException anfe) {
               // Toast toast = Toast.makeText(this, "This device doesn't support the crop action!",
                 //       Toast.LENGTH_SHORT);
                //toast.show();
            }
        } }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == CAMERA_CAPTURE) {
                // get the Uri for the captured image
                picUri = data.getData();

                Intent intent = new Intent("com.android.camera.action.CROP");
                intent.setType("image/*");
                List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
                int size = list.size();
                if (size != 0) {            
                    performCrop();
                    //Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
                    }
                else
                {
                    Bundle extras = data.getExtras();
                    // get the cropped bitmap
                    Bitmap thePic = extras.getParcelable("data");
                    setphoto = thePic;
                    ImageView picView = (ImageView) findViewById(R.id.image);
                    picView.setImageBitmap(thePic);
                }


                }
            // user is returning from cropping the image
            else if (requestCode == CROP_PIC) {
                // get the returned data
                Bundle extras = data.getExtras();
                // get the cropped bitmap
                Bitmap thePic = extras.getParcelable("data");
                setphoto = thePic;
                ImageView picView = (ImageView) findViewById(R.id.image);
                picView.setImageBitmap(thePic);
            }
        }
    }

    /**
     * this function does the crop operation.
     */
    private void performCrop() { 
        // take care of exceptions
        try {
            // call the standard crop action intent (the user device may not
            // support it)
            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            // indicate image type and Uri
            cropIntent.setDataAndType(picUri, "image/*");
            // set crop properties
            cropIntent.putExtra("crop", "true");
            // indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 3);
            cropIntent.putExtra("aspectY", 4);
            // indicate output X and Y
            cropIntent.putExtra("outputX", 800);
            cropIntent.putExtra("outputY", 800);
            // retrieve data on return
            cropIntent.putExtra("return-data", true);
            // start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, CROP_PIC);
        }
        // respond to users whose devices do not support the crop action
        catch (ActivityNotFoundException anfe) {
            Toast toast = Toast
                    .makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}

【问题讨论】:

    标签: android image android-intent camera


    【解决方案1】:

    试试这个以获得高质量

     Uri mHighQualityImageUri = generateTimeStampPhotoFileUri();
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, mHighQualityImageUri);
                    startActivityForResult(intent, REQUEST_CODE_HIGH_QUALITY_IMAGE);
    

    还有这个:

    private Uri generateTimeStampPhotoFileUri() {
    
                    Uri photoFileUri = null;
                    File outputDir = getPhotoDirectory();
                    if (outputDir != null) {
                            Time t = new Time();
                            t.setToNow();
                            File photoFile = new File(outputDir, System.currentTimeMillis()
                                            + ".jpg");
                            photoFileUri = Uri.fromFile(photoFile);
                    }
                    return photoFileUri;
            }
    

    注意: 你需要添加的简单的东西

     intent.putExtra(MediaStore.EXTRA_OUTPUT, mHighQualityImageUri);
    

    在您的代码以下部分:

    Uri mHighQualityImageUri = generateTimeStampPhotoFileUri();
     // use standard intent to capture an image
                    Intent captureIntent = new Intent(
                            MediaStore.ACTION_IMAGE_CAPTURE);
     captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mHighQualityImageUri);
                    // we will handle the returned data in onActivityResult
                    startActivityForResult(captureIntent, CAMERA_CAPTURE);
    

    【讨论】:

    • 我试过但结果是一样的。你能在我的代码中应用你的更改并发送给我吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多