2011-12-23 73 views
1

我有合併兩個image並將它們一起顯示在canvas中的代碼如下。你能否說storesingle image結合兩個圖像並保存到SD卡

public class ChoosePictureComposite extends Activity implements OnClickListener { 

    static final int PICKED_ONE = 0; 
    static final int PICKED_TWO = 1; 

    boolean onePicked = false; 
    boolean twoPicked = false; 

    Button choosePicture1, choosePicture2; 
    ImageView compositeImageView; 

    Bitmap bmp1, bmp2; 
    Canvas canvas; 
    Paint paint; 

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

     compositeImageView = (ImageView) this 
       .findViewById(R.id.CompositeImageView); 

     choosePicture1 = (Button) this.findViewById(R.id.ChoosePictureButton1); 
     choosePicture2 = (Button) this.findViewById(R.id.ChoosePictureButton2); 

     choosePicture1.setOnClickListener(this); 
     choosePicture2.setOnClickListener(this); 
    } 

    public void onClick(View v) { 

     int which = -1; 

     if (v == choosePicture1) { 
      which = PICKED_ONE; 
     } else if (v == choosePicture2) { 
      which = PICKED_TWO; 
     } 

     Intent choosePictureIntent = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(choosePictureIntent, which); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, 
      Intent intent) { 
     super.onActivityResult(requestCode, resultCode, intent); 

     if (resultCode == RESULT_OK) { 
      Uri imageFileUri = intent.getData(); 

      if (requestCode == PICKED_ONE) { 
       bmp1 = loadBitmap(imageFileUri); 
       onePicked = true; 
      } else if (requestCode == PICKED_TWO) { 
       bmp2 = loadBitmap(imageFileUri); 
       twoPicked = true; 
      } 

      if (onePicked && twoPicked) { 
       Bitmap drawingBitmap = Bitmap.createBitmap(bmp1.getWidth(), 
         bmp1.getHeight(), bmp1.getConfig()); 
       canvas = new Canvas(drawingBitmap); 
       paint = new Paint(); 
       canvas.drawBitmap(bmp1, 90, 0, paint); 
      // paint.setXfermode(new PorterDuffXfermode(
        // android.graphics.PorterDuff.Mode.MULTIPLY)); 
       canvas.drawBitmap(bmp2, 30, 40, paint); 


       compositeImageView.setImageBitmap(drawingBitmap); 
      } 
     } 
    } 

    private Bitmap loadBitmap(Uri imageFileUri) { 
     Display currentDisplay = getWindowManager().getDefaultDisplay(); 

     float dw = currentDisplay.getWidth(); 
     float dh = currentDisplay.getHeight(); 

     Bitmap returnBmp = Bitmap.createBitmap((int) dw, (int) dh, 
       Bitmap.Config.ARGB_4444); 

     try { 
      // Load up the image's dimensions not the image itself 
      BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
      bmpFactoryOptions.inJustDecodeBounds = true; 
      returnBmp = BitmapFactory.decodeStream(getContentResolver() 
        .openInputStream(imageFileUri), null, bmpFactoryOptions); 

      int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/dh); 
      int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/dw); 

      Log.v("HEIGHTRATIO", "" + heightRatio); 
      Log.v("WIDTHRATIO", "" + widthRatio); 

      // If both of the ratios are greater than 1, one of the sides of the 
      // image is greater than the screen 
      if (heightRatio > 1 && widthRatio > 1) { 
       if (heightRatio > widthRatio) { 
        // Height ratio is larger, scale according to it 
        bmpFactoryOptions.inSampleSize = heightRatio; 
       } else { 
        // Width ratio is larger, scale according to it 
        bmpFactoryOptions.inSampleSize = widthRatio; 
       } 
      } 

      // Decode it for real 
      bmpFactoryOptions.inJustDecodeBounds = false; 
      returnBmp = BitmapFactory.decodeStream(getContentResolver() 
        .openInputStream(imageFileUri), null, bmpFactoryOptions); 
     } catch (FileNotFoundException e) { 
      Log.v("ERROR", e.toString()); 
     } 

     return returnBmp; 
    } 
} 
+0

'一起'在?你想要他們並排嗎?或覆蓋? – st0le 2011-12-23 06:01:10

回答

3

試試這個:

public class Aura extends Activity { 
    protected static final String TAG = Aura.class.getName(); 
    private static String mTempDir; 
    Bitmap mBackImage, mTopImage, mBackground, mInnerImage, mNewSaving; 
    Canvas mComboImage; 
    FileOutputStream mFileOutputStream; 
    BitmapDrawable mBitmapDrawable; 
    private String mCurrent = null; 

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

    mTempDir = Environment.getExternalStorageDirectory() + "/" + "Aura" + "/"; 
    mCurrent = "Aura.png"; 
    prepareDirectory(); 

    mBackground = Bitmap.createBitmap(604, 1024, Bitmap.Config.ARGB_8888); 
    mBackImage = BitmapFactory.decodeResource(getResources(), R.drawable.aura); 
    mTopImage = BitmapFactory.decodeResource(getResources(), R.drawable.test); 
    mInnerImage = BitmapFactory.decodeResource(getResources(), R.drawable.anothertest); 

    mComboImage = new Canvas(mBackground); 
    mComboImage.drawBitmap(mBackImage, 0f, 0f, null); 
    mComboImage.drawBitmap(mTopImage, 0f, 0f, null); 
    mComboImage.drawBitmap(mInnerImage, 0f, 0f, null); 
    mFileOutputStream = null; 

    mSave.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
     Log.v(TAG, "Save Tab Clicked"); 
     try { 
      mBitmapDrawable = new BitmapDrawable(mBackground); 
      mNewSaving = ((BitmapDrawable) mBitmapDrawable).getBitmap(); 
      String FtoSave = mTempDir + mCurrent; 
      File mFile = new File(FtoSave); 
      mFileOutputStream = new FileOutputStream(mFile); 
      mNewSaving.compress(CompressFormat.PNG, 95, mFileOutputStream); 
      mFileOutputStream.flush(); 
      mFileOutputStream.close(); 
     } catch (FileNotFoundException e) { 
      Log.v(TAG, "FileNotFoundExceptionError " + e.toString()); 
     } catch (IOException e) { 
      Log.v(TAG, "IOExceptionError " + e.toString()); 
     } 
     } 
    }); 
    }//onCreate 

    private boolean prepareDirectory() { 
    try { 
     if (makeDirectory()) { 
     return true; 
     } else { 
     return false; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     Toast.makeText(this, getString(R.string.sdcard_error), 1000).show(); 
     return false; 
    } 
    } 

    private boolean makeDirectory() { 
    File mTempFile = new File(mTempDir); 
    if (!mTempFile.exists()) { 
     mTempFile.mkdirs(); 
    } 

    if (mTempFile.isDirectory()) { 
     File[] mFiles = mTempFile.listFiles(); 
     for (File mEveryFile : mFiles) { 
     if (!mEveryFile.delete()) { 
      System.out.println(getString(R.string.failed_to_delete) + mEveryFile); 
     } 
     } 
    } 
    return (mTempFile.isDirectory()); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((!(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT) 
     && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)) { 
     onBackPressed(); 
    } 
    return super.onKeyDown(keyCode, event); 
    } 

    public void onBackPressed() { 
    finish(); 
    } 
} 
+0

是的,它完美的工作。謝謝!我也想把一個圖像拖到另一個圖像上並放在某個地方然後保存。你可以幫忙嗎.. – Matthew 2011-12-23 06:36:29

+1

你的意思是你想把小圖像放在那幅圖像上,然後想保存整幅圖像。 。對 ? – 2011-12-23 07:20:31

+1

如果可能,請創建另一個答案並根據需要放置一些圖像。謝謝。 – 2011-12-23 07:22:43

0

創建一個新的Bitmap與您的圖像的各個高度的高度總和。

 Bitmap newBitmap = Bitmap.createBitmap(widht 
       , totalHeight, Config.ARGB_8888); 
Canvas canvas = new Canvas(newBitmap); 
canvas.drawBitmap(image11, 0, 0, null); 
canvas.drawBitmap(image2, 0, image1Height, null); 

這應該繪製2個圖像進入newBitmap,一個在另一個之下。如果你想讓它們並排放置,請將高度參數更改爲widht。

這是你在找什麼?

+0

我也想將一個圖像拖放到另一個圖像上,然後將其保存。請問您可以幫助 – Matthew 2011-12-23 06:37:04

+0

考慮iDroidExplorer的答案,並應用相同的邏輯來創建組合圖像的位圖。 – rDroid 2011-12-26 04:37:10