从相机获取低质量的图像

时间:2015-04-07 06:14:31

标签: android image android-intent camera

我在最大设备中获得低质量图片但在Redmi 1s中我从相机捕获后会出现黑色或黑色图像。
我想做3份工作

  1. 从相机中捕捉图像
  2. 捕捉原始或高质量的图像
  3. 将其设置为我的设备背景
  4. 我的代码是:

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

1 个答案:

答案 0 :(得分:0)

尝试高质量

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