2015-04-07 139 views
0

我在最大设备中获得低质量图片,但在红米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(); 
     } 
    } 
} 

回答

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

我试过,但结果是一样的 。你可以在我的代码中应用你的修改并发送给我 – Hunt