顯示具有 Image 標籤的文章。 顯示所有文章
顯示具有 Image 標籤的文章。 顯示所有文章

2012-08-07

Android Image應用(五) - 縮放、指定圖片大小

此篇要介紹的是如何縮放圖片,我們都知道圖片大小有時在呈現上讓我們不是那麼的滿意

或是讀取進來的圖片尺寸可能都有各式各樣的,這樣很容易破壞我們原來的版型,怎麼辦呢?

今天就介紹怎麼縮放圖片,讓圖片變大、變小、變成我們想要的。

  為了讓讀者比較容易了解及讀取方便,所以我會故意把三個按鈕的處理程序都拆開來寫

,而不是寫在一起,目的是為了易於讀取,不然用程序 + switch case寫在一起就好了


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/ic_launcher" />
    </RelativeLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >




        <Button
            android:id="@+id/btn_Center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="@string/button_center" />



        <Button
            android:id="@+id/btn_Right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="30dp"
            android:layout_toRightOf="@+id/btn_Center"
            android:text="@string/button_right" />


        <Button
            android:id="@+id/btn_Left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginRight="30dp"
            android:layout_toLeftOf="@+id/btn_Center"
            android:text="@string/button_left" />

    </RelativeLayout>

</LinearLayout>



strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">圖片縮放</string>
    <string name="button_left">放大</string>
    <string name="button_right">縮小</string>
    <string name="button_center">變為480*480</string>

</resources>


DemoZoomActivity.java
package jim.demo.zoom;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class DemoZoomActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
         //圖片
        final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
        
        //放大
        Button btnLeft = (Button)findViewById(R.id.btn_Left);
        //指定大小
        Button btnCenter = (Button)findViewById(R.id.btn_Center);
        //縮小
        Button btnRight = (Button)findViewById(R.id.btn_Right);
        
        
        //放大按鈕事件
        View.OnClickListener listener_left = new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				
				//imageView轉Bitmap
				  imageView.buildDrawingCache();
		          Bitmap bmp=imageView.getDrawingCache();
		        		
		        //轉換為圖片指定大小
	      	    //獲得圖片的寬高
	      	    int width = bmp.getWidth();
	      	    int height = bmp.getHeight();
	      	   
	      	    //放大為1.2倍
	      	    float scaleWidth = (float) 1.2;
	      	    float scaleHeight = (float) 1.2;
	      	    
	      	    // 取得想要缩放的matrix參數
	      	    Matrix matrix = new Matrix();
	      	    matrix.postScale(scaleWidth, scaleHeight);
	      	    // 得到新的圖片
	      	    Bitmap newbm = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix,true);
	      	    
	      	  //重新載入 imageView
	      	  imageView.setImageBitmap(newbm);
	          
			} 
        	
        };
        btnLeft.setOnClickListener(listener_left); 
        
        //指定大小按鈕事件
        View.OnClickListener listener_center = new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				
				//imageView轉Bitmap
				  imageView.buildDrawingCache();
		          Bitmap bmp=imageView.getDrawingCache();
		        		
		        //轉換為圖片指定大小
	      	    //獲得圖片的寬高
	      	    int width = bmp.getWidth();
	      	    int height = bmp.getHeight();
	      	    // 設置想要的大小
	      	    int newWidth = 480;
	      	    int newHeight = 480;
	      	    // 計算缩放比例
	      	    float scaleWidth = ((float) newWidth) / width;
	      	    float scaleHeight = ((float) newHeight) / height;
	      	    // 取得想要缩放的matrix參數
	      	    Matrix matrix = new Matrix();
	      	    matrix.postScale(scaleWidth, scaleHeight);
	      	    // 得到新的圖片
	      	    Bitmap newbm = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix,true);
	      	    
	      	  //重新載入 imageView
	      	  imageView.setImageBitmap(newbm);
	      	  
			} 
        	
        };
        btnCenter.setOnClickListener(listener_center); 
        
        //縮小按鈕事件
        View.OnClickListener listener_right = new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				
				//imageView轉Bitmap
				  imageView.buildDrawingCache();
		          Bitmap bmp=imageView.getDrawingCache();
		        		
		        //轉換為圖片指定大小
	      	    //獲得圖片的寬高
	      	    int width = bmp.getWidth();
	      	    int height = bmp.getHeight();
	      	   
	      	    //縮小為0.8倍
	      	    float scaleWidth = (float) 0.8;
	      	    float scaleHeight = (float) 0.8;
	      	    
	      	    // 取得想要缩放的matrix參數
	      	    Matrix matrix = new Matrix();
	      	    matrix.postScale(scaleWidth, scaleHeight);
	      	    // 得到新的圖片
	      	    Bitmap newbm = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix,true);
	      	    
	      	  //重新載入 imageView
	      	  imageView.setImageBitmap(newbm);
			} 
        	
        };
        btnRight.setOnClickListener(listener_right); 
    }
}


執行畫面





後記:

            如果試著一直放大縮小你會發現一種現象:"失真了",因為把小圖放的太大本來

就會失真,所以最常用的是在讀取圖片後改變圖片比例到我們指定的大小,要不失真也

行,請準備一張大圖,這樣就比較不會失真了,不然系統內的圖片就如同啟始畫面一樣

小小一張而已,放大就會失真,這是圖片一定會有的狀況,謝謝收看。

2012-08-03

Bitmap、Drawable、byte的轉換

當寫到圖片的處理時,比較常會遇到的是這三種圖片型態的轉換

所以就來介紹一下這三種資料型態該怎麼轉換。


Bitmap 轉 Drawable
Drawable drawable = new BitmapDrawable(Bitmap);




Drawable 轉 Bitmap
Drawable oDrawable = xxx; //xxx根據自己的情況獲取drawable
BitmapDrawable BD = (BitmapDrawable) oDrawable;
Bitmap BM = BD.getBitmap();




實體路徑圖片檔 轉 Drawable
//這句一定要在onCreate()里面調用
File tempFile = = new File("/sdcard/a.jpg");

Drawable drawable = Drawable.createFromPath(tempFile.getAbsolutePath());




從res\drawable中取出內存圖片為Bitmap
Resources res=getResources();

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.ic_launcher); 
 



imageView 轉 Bitmap
  //圖片
  ImageView imageView_save = (ImageView) findViewById(R.id.img_newphoto);

  //imageView轉Bitmap
  imageView_save.buildDrawingCache();
  Bitmap bmp=imageView_save.getDrawingCache();




bitmap 轉 byte
  Bitmap newbm;

  // 把 bitmap 轉成 byte
  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  newbm.compress(Bitmap.CompressFormat.JPEG, 100, stream );
  byte bytes[] = stream.toByteArray();



後記:

這是我常用到的轉換,還蠻實用的,所以PO出來給需要的人參考,謝謝。

2012-08-01

Android 手機資料庫(四) - 將圖片存入資料庫的方法

我們都知道一般照片都是放在手機的圖片資料夾下,很少會放到資料庫內

不過為了怕應用程式需要的照片不小心被刪了而讓應用程式出錯,所以

就需要把圖片存進資料庫內,這裡我就來介紹怎麼樣把照片存進資料庫和取出

注意:這部份因為有關係到資料庫,所以為了不讓程式碼太多,所以我只show出
如何取出和存入圖片的程式碼,所以對資料庫需要有一定的認識,這部份請參考
前三篇(第一篇、第二篇、第三篇)的介紹。


存入的程式碼
//圖片
 ImageView imageView_save = (ImageView) findViewById(R.id.img_newphoto);
               
        //imageView轉Bitmap
        //imageView_save.setDrawingCacheEnabled(true);
        //建立圖片的緩存,圖片的緩存本身就是一個Bitmap
        imageView_save.buildDrawingCache();
        //取得緩存圖片的Bitmap檔
        Bitmap bmp=imageView_save.getDrawingCache();
          
            //轉換為圖片指定大小
            //獲得圖片的寬高
            int width = bmp.getWidth();
            int height = bmp.getHeight();
            // 設置想要的大小
            int newWidth = 480;
            int newHeight = 525;
            // 計算缩放比例
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // 取得想要缩放的matrix參數
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // 得到新的圖片
            Bitmap newbm = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix,true);
             
          
            // 先把 bitmap 轉成 byte
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            newbm.compress(Bitmap.CompressFormat.JPEG, 100, stream );
            byte bytes[] = stream.toByteArray();
            // Android 2.2以上才有內建Base64,其他要自已找Libary或是用Blob存入SQLite
            // 把byte變成base64
            String base64 = Base64.encodeToString(bytes, Base64.DEFAULT); 
           
            //放img進去SQLite
            ContentValues cv = new ContentValues();
            cv.put("image", base64);
           //添加方法
           long long1 = db.insert(table_name, "", cv)



取出的程式碼
//取出資料
        Cursor cursor = db.rawQuery("select * from newimg where season = '秋'", null);
        
        //用陣列存圖片,陣列大小 = 取出資料的筆數
 Drawable[] Drawablelist = new Drawable[cursor.getCount()];
 Bitmap[] bmplist = new Bitmap[cursor.getCount()];
        
        //取得資料表筆數
        int rows_num = cursor.getCount(); 
  if(rows_num != 0) {

          //將指標移至第一筆資料
   cursor.moveToFirst(); 
            
   for(int i=0; i<rows_num; i++) {
            // 把Base64變回bytes
     byte bytes[] = Base64.decode(cursor.getString(2), Base64.DEFAULT);

            //用BitmapFactory生成bitmap
     bmplist[i] = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
       
     //轉換Bitmap為Drawable
            Drawable drawable = new BitmapDrawable(bmplist[i]);

            //存入Drawable陣列中
     Drawablelist[i]=drawable;

            //將指標移至下一筆資料
            cursor.moveToNext();  
          }
        }

        //關閉Cursor
 cursor.close(); 
        //關閉資料庫,釋放記憶體 
 dbHelper.close(); 



後記:

          在存入時我有多用了縮放圖片,而在取出時因程式需要,所以有把Bitmap

再轉換為Drawable,這個可以看個人程式上的需求去做改變,不一定要照copy

程式是死的,人是活的,希望大家都能寫出一支好的程式,感謝。

Android Image應用(四) - 旋轉圖片

如果您有用到拍照或取得圖片功能時應該會知道,得到的照片或圖片會剛好跟我們想要的

差了90度,怎麼辦呢?這裡就為您介紹怎麼把圖片轉向90度的方法


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/ic_launcher" />

    </RelativeLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="60dp"
            android:text="@string/right" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="60dp"
            android:text="@string/left" />

    </RelativeLayout>

</LinearLayout>



strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">DemoImageRotation</string>
    <string name="left">左旋90度</string>
    <string name="right">右旋90度</string>
</resources>


DemoImageRotationActivity.java
package jim.demo.ImageRotation;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class DemoImageRotationActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
      //圖片左旋90
        Button button_left = (Button)findViewById(R.id.button1);
        View.OnClickListener listener_left = new View.OnClickListener() {  
         
         public void onClick(View view) { 
         //取得圖片
         ImageView imageView_left = (ImageView) findViewById(R.id.imageView1);
         //imageView_left.setDrawingCacheEnabled(true);
         //建立圖片的緩存,圖片的緩存本身就是一個Bitmap
         imageView_left.buildDrawingCache();
         //取得緩存圖片的Bitmap檔
         Bitmap bmp=imageView_left.getDrawingCache();
         //定義一個矩陣圖
         Matrix m=new Matrix();
         //取得圖片的寬度
                int width=bmp.getWidth();
                //取得圖片的長度
                int height=bmp.getHeight();
                //逆時針旋轉90度
                m.setRotate(-90);
              //產生新的旋轉後Bitmap檔
                Bitmap b=Bitmap.createBitmap(bmp, 0, 0, width, height, m, true);
                
                //顯示圖片
                imageView_left.setImageBitmap(b);
            }  
        };  
        button_left.setOnClickListener(listener_left); 
        
        
      //圖片右旋90
        Button button_right = (Button)findViewById(R.id.button2);
        View.OnClickListener listener_right = new View.OnClickListener() {  
         public void onClick(View view) {  
         //取得圖片
         ImageView imageView_right = (ImageView) findViewById(R.id.imageView1);
         //imageView_right.setDrawingCacheEnabled(true);
         //建立圖片的緩存,圖片的緩存本身就是一個Bitmap
         imageView_right.buildDrawingCache();
         //取得緩存圖片的Bitmap檔
         Bitmap bmp=imageView_right.getDrawingCache();
         //定義一個矩陣圖
         Matrix m=new Matrix();
         //取得圖片的寬度
                int width=bmp.getWidth();
                //取得圖片的長度
                int height=bmp.getHeight();
                //順時針旋轉90度
                m.postRotate(90); 
                //產生新的旋轉後Bitmap檔
                Bitmap b=Bitmap.createBitmap(bmp, 0, 0, width, height, m, true);
                
                //顯示圖片
                imageView_right.setImageBitmap(b);
            
            }  
        };  
        button_right.setOnClickListener(listener_right);
    }   
}


執行畫面



後記:

            旋轉圖片對有用到取得手機內圖片及拍照功能的人應該還蠻需要的

但有一個缺點,就是旋轉必須長寬都一樣時用,如果長和寬是不一樣時,比如說是2:1

會出現一種現象,就是會愈轉愈小-.-",原因是當比較大的長轉到比較小的寬時會因為

放不進去,所以會自動縮小,然後你愈轉它就縮的愈多,所以就會出現愈轉愈小的現象

這是我後來再處理照片時發現的,好了!就為您介紹到這了,感謝。

2012-07-27

Android Image應用(三) - 裁剪圖片 for 拍照之後

上篇介紹了取得圖片後的裁剪,此篇再為您介紹拍照後的裁剪,可能您會覺得

這兩種不一樣嗎?其實是一樣的,差別在於取得圖片後的資料流不一樣

處理的方式也不同,因此把它分開成兩篇來介紹,其實寫法是可以寫在一起的

因為拍照和從手機內取得圖檔在取得圖片後都是會回到onActivityResult內的

下面開始來看看怎麼做吧。


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<Button
   android:id="@+id/captureimage"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="使用其他服務幫忙拍照 "
   />
<ImageView
   android:id="@+id/imagecaptured"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   />
</LinearLayout>

DemoCameraActivity.java
package jim.demo.camera;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class DemoCameraActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //調用其他服務拍照按鈕
        Button buttonCamera = (Button)findViewById(R.id.captureimage);
               
        buttonCamera.setOnClickListener(new Button.OnClickListener(){
   public void onClick(View v) {
    //使用Intent調用其他服務幫忙拍照
     Intent intent_camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
     startActivityForResult(intent_camera, 0);
   }
        });
    }
    
    @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      //拍照後顯示圖片
      ImageView iv = (ImageView)findViewById(R.id.imagecaptured);
   if (resultCode == RESULT_OK)
     {
    switch (requestCode) {  
          case 0:
    //取出拍照後回傳資料
    Bundle extras = data.getExtras();
    //將資料轉換為圖像格式
          Bitmap bmp = (Bitmap) extras.get("data");
           //如果有資料就丟doCropPhoto進行裁剪
           if(bmp!=null){
          doCropPhoto(bmp);
           }
           break; 
          case 1: 
           Bitmap photo1 = data.getParcelableExtra("data");
           //載入ImageView
              iv.setImageBitmap(photo1);
           break;   
    }
     }
   
     //覆蓋原來的Activity
     super.onActivityResult(requestCode, resultCode, data);
  }
    
    protected void doCropPhoto(Bitmap data){
     //進行照片裁剪
        Intent intent = getCropImageIntent(data);
        startActivityForResult(intent, 1);
    }
   
    public static Intent getCropImageIntent(Bitmap data) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setType("image/*");
        intent.putExtra("data", data);
        intent.putExtra("crop", "true");// crop=true 有這句才能叫出裁剪頁面.
        intent.putExtra("aspectX", 1);// 这兩項為裁剪框的比例.
        intent.putExtra("aspectY", 1);// x:y=1:1
        intent.putExtra("outputX", 300);//回傳照片比例X
        intent.putExtra("outputY", 300);//回傳照片比例Y
        intent.putExtra("return-data", true);
        return intent;
    }
}


執行後畫面




後記:

         看完程式碼應該就可以看出其實大同小異,主要都是用intent.putExtra("crop", "true")


叫出裁剪頁面進行裁剪,不同在拍照後圖檔的不同,所以需要進行不同的轉換


我在使用上其實是會把從手機內取得圖檔和拍照放在一起供使用者選擇,此處是因為介紹

所以把它分開,小弟我就介紹到這裡,有問題請留言或來信,感謝。



Android Image應用(二) - 裁剪圖片 for 取得手機內圖檔時

有介紹過取得手機內圖檔及使用拍照功能,拍出來的大多都是1:2或2:1的長寬

如果我們想要的是1:1的圖片呢?怎麼辦?拿剪刀?從手機讀出來用圖片編輯軟體

裁剪後再放回去?當然不是這樣的,這樣太麻煩了,如果能直接裁剪當然是最好的

所以下面就為您介紹直接在取得圖檔時就執行裁剪圖片的動作。


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
   <Button   
        android:id="@+id/b01"  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content&
    />  
    <ImageView  
        android:id="@+id/iv01"  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
     />  
</LinearLayout>  


GetPicturesActivity.java
package jim.demo.getpictures;

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class GetPicturesActivity extends Activity {
 private File tempFile;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        this.tempFile = new File("/sdcard/a.jpg");// 這句一定要在onCreate()里面調用
        
        //找尋Button按鈕
        Button button = (Button)findViewById(R.id.b01);
        //設定按鈕內文字
        button.setText("選擇圖片");
        //設定按鈕監聽式
        button.setOnClickListener(new Button.OnClickListener(){
   public void onClick(View v) {
     Intent intent = new Intent();
          // 開啟Pictures畫面Type設定為image
          intent.setType("image/*");
          // 使用Intent.ACTION_GET_CONTENT這個Action
          // 會開啟選取圖檔視窗讓您選取手機內圖檔 
          intent.setAction(Intent.ACTION_GET_CONTENT); 
          intent.setType("image/*");
          intent.putExtra("crop", "true");// crop=true 有這句才能叫出裁剪頁面.
          intent.putExtra("aspectX", 1);// 这兩項為裁剪框的比例.
          intent.putExtra("aspectY", 1);// x:y=1:1
          intent.putExtra("output", Uri.fromFile(tempFile));
          intent.putExtra("outputFormat", "JPEG");//返回格式
                    
          // 取得相片後返回本畫面
           startActivityForResult(Intent.createChooser(intent, "選擇圖片"),1);
   }
         
        });
    }
    
    //取得相片後返回的監聽式
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  //取得圖片控制項ImageView
  ImageView imageView = (ImageView) findViewById(R.id.iv01);
  
  //當使用者按下確定後
  if (resultCode == RESULT_OK) {
       
   // 設定到ImageView 
   imageView.setImageDrawable(Drawable.createFromPath(tempFile.getAbsolutePath()));
     
  }
  super.onActivityResult(requestCode, resultCode, data);
 }
}

執行畫面




後記:

          切割其實是使用intent.setAction(Intent.ACTION_GET_CONTENT)抓取圖片後

再多加一個動作使用intent.putExtra("crop", "true")叫出裁剪頁面達到裁剪效果

還蠻簡單的,下篇再為您介紹在拍照後如果要裁剪照片該怎麼做,感謝收看。

2012-07-24

Android Image應用(一) - 取得手機內的圖檔

今天介紹多媒體應用中兩個重要的圖片、相機應用的其中一個:圖片

如果取得手機內的圖檔應該是要做有關多媒體應用程式常會遇到的事

下面就開始實作如果取得手機內的圖檔資料


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
   <Button   
        android:id="@+id/b01"  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content&
    />  
    <ImageView  
        android:id="@+id/iv01"  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
     />  
</LinearLayout>  



GetPicturesActivity.java
package jim.demo.getpictures;

import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class GetPicturesActivity extends Activity {
	 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //找尋Button按鈕
        Button button = (Button)findViewById(R.id.b01);
        //設定按鈕內文字
        button.setText("選擇圖片");
        //設定按鈕監聽式
        button.setOnClickListener(new Button.OnClickListener(){
	  public void onClick(View v) {
	 	Intent intent = new Intent();
	    //開啟Pictures畫面Type設定為image
	       intent.setType("image/*");
            //使用Intent.ACTION_GET_CONTENT這個Action                                            //會開啟選取圖檔視窗讓您選取手機內圖檔
	        intent.setAction(Intent.ACTION_GET_CONTENT); 
	    //取得相片後返回本畫面
	        startActivityForResult(intent, 1);
	  }
        	
        });
    }
    
    //取得相片後返回的監聽式
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	//當使用者按下確定後
	if (resultCode == RESULT_OK) {
	   //取得圖檔的路徑位置
	   Uri uri = data.getData();
	   //寫log
	   Log.e("uri", uri.toString());
	   //抽象資料的接口
	   ContentResolver cr = this.getContentResolver();
	 try {
	   //由抽象資料接口轉換圖檔路徑為Bitmap
  	   Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
	   //取得圖片控制項ImageView
	   ImageView imageView = (ImageView) findViewById(R.id.iv01);
	   // 將Bitmap設定到ImageView
	   imageView.setImageBitmap(bitmap);
	  } catch (FileNotFoundException e) {
	    Log.e("Exception", e.getMessage(),e);
	  }
	}
	  super.onActivityResult(requestCode, resultCode, data);
	}
}


執行畫面



後記:

          呼叫android取得手機內圖片很簡單,只要用Intent.ACTION_GET_CONTENT

重點在於取得圖檔後面的處理,因為取得只是取出該圖檔的存放路徑,所以必須經由

抽象接口ContentResolver再把該圖檔轉換為Bitmap,轉換為Bitmap主要是用來餵給imageView


下面是幾種常用可以設定imageView內圖片的型式
          imageView.setImageBitmap(bitmap) //bitmap圖檔型式
  imageView.setImageDrawable(drawable) //drawable圖檔型式
  imageView.setImageResource(resId) //存在應用程式中res/drawable內的圖片
  imageView.setImageURI(uri)//網路圖片路徑

bitmap和drawable是可以互轉,我再另外開一篇介紹bitmap和drawable轉換


有問題再留言或寫信給我,謝謝。

2012-07-12

ImageButton、Image使用內建Icon圖示

Android有內建圖示可以使用,我們就來看一下該怎麼使用這些圖示

1.在main.xml的設計Graphical Layout中,做用ImageButton或Image

2.拉進元件後出現圖片選取,選擇第二項

3.選擇需要的圖示後按下ok

4.ImageButton出現圖示,可以在ImageButton上按右鍵,選擇Edit Style再進入修改圖示

5.後面程式碼其實都相同就:@android:drawable/+你選擇的Icon檔名

6.也能在程式碼的地方直接加入內建Icon圖示

使用內建Icon圖示其實還蠻常會用到的,因為不用另外再畫圖示
這裡介紹給需要使用到的人,謝謝觀看。

2012-04-23

PDF轉縮圖 用 ASP.NET for VB

因之前平台開發時需要"PDF轉縮圖",所以就試著做出此功能

接下來先看使用的效果。