2015-04-07 177 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