2016-04-27 24 views
0

カメラから撮影した画像を表示したいが、画像ビューの画像のサイズは小さく、ギャラリーの画像を見る画像とは異なり、ギャラリーの画像サイズは適している画面で、誰も私がこれを解決するのを助けることができますか?ここでカメラで撮影した画像のサイズを変更する

は我々が適切にあなたのコードを判断することはできません、私のJavaコードは

Button btn_kamera = (Button) findViewById(R.id.btnKamera); 
    btn_kamera.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View view) { 
       new Handler().postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);      
         startActivityForResult(intent, REQUEST_CAMERA); 
        } 
       }, 150); 
        } 
      }); 

    Button btn_galeri = (Button) findViewById(R.id.btnGaleri); 
    btn_galeri.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View view) { 
       new Handler().postDelayed(new Runnable() { 
        @Override 
        public void run() { 

         Intent intent = new Intent(
           Intent.ACTION_PICK, 
           android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

         startActivityForResult(intent, RESULT_LOAD_IMAGE); 
        } 
       }, 150); 
        } 
      }); 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 

     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

     Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 

     this._image.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

    } 

    else if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK && null != data) { 

     this._image.setImageBitmap((Bitmap) data.getExtras().get("data")); 

    } 

} 

であり、ここで私のXMLコード

<?xml version="1.0" encoding="utf-8"?> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" > 

       <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_above="@+id/linearLayout1" 
        android:layout_centerHorizontal="true" 
        android:layout_gravity="center" 
        android:layout_marginBottom="30dp" 
        android:text="Ambil Gambar" 
        android:textAppearance="?android:attr/textAppearanceLarge" /> 

       <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/linearLayout1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:weightSum="1" 
        android:layout_marginBottom="30dp" 
        > 

        <com.indris.material.RippleView 
         android:id="@+id/btnKamera" 
         android:layout_weight="0.5" 
         android:layout_marginRight="5dp" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:background="@drawable/card_bk" 
         android:gravity="center" 
         android:padding="15dp" 
         android:text="Kamera" 
         android:textAppearance="?android:attr/textAppearanceMedium" 
         android:textSize="18sp" 
         ripple:alphaFactor="0.7" 
         ripple:hover="true" 
         ripple:rippleColor="#80D8FF" /> 

        <com.indris.material.RippleView 
         android:id="@+id/btnGaleri" 
         android:layout_weight="0.5" 
         android:layout_marginLeft="5dp" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:background="@drawable/card_bk" 
         android:gravity="center" 
         android:padding="15dp" 
         android:text="Galeri" 
         android:textAppearance="?android:attr/textAppearanceMedium" 
         android:textSize="18sp" 
         ripple:alphaFactor="0.7" 
         ripple:hover="true" 
         ripple:rippleColor="#80D8FF" /> 

       </LinearLayout> 

       <TextView 
        android:id="@+id/textView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:text="Gambar Belum Dipilih" 
        android:visibility="visible" 
        android:textAppearance="?android:attr/textAppearanceMedium" /> 

        <ImageView 
         android:id="@+id/imageView1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:layout_gravity="center" 
         android:adjustViewBounds="true" 
         android:scaleType="fitXY" 
         android:layout_marginBottom="30dp" /> 


       <com.indris.material.RippleView 
        android:id="@+id/btnProses" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_above="@+id/editText1" 
        android:layout_alignLeft="@+id/editText1" 
        android:layout_marginBottom="30dp" 
        android:background="@drawable/card_bk" 
        android:gravity="center" 
        android:padding="15dp" 
        android:text="Proses" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:textSize="18sp" 
        ripple:alphaFactor="0.7" 
        ripple:hover="true" 
        ripple:rippleColor="#80D8FF" /> 

       <EditText 
        android:id="@+id/editText1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:hint="Teks muncul di sini" 
        android:editable="false" 
        /> 


      </LinearLayout> 
+0

元のカメラ画像を求める必要があります。これを見てください:http://stackoverflow.com/questions/6448856/android-camera-intent-how-to-get-full-sized-photo –

答えて

0

です。

フルコードを掲載できますか?

ところで、あなたはアンドロイドハイブの非常に良い例と一緒に行くことができます。チェックhere

大きな画像のOutOfMemory例外を防ぐには、BitmapFactory.Optionsの機能があります。この画像を使用するには、サイズをサンプリングする必要があります。以下のコードスニペットを確認してください

BitmapFactory.Options options = new BitmapFactory.Options(); 

     options.inSampleSize = 8; 

     final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), 
       options); 

     imgPreview.setImageBitmap(bitmap); 

画像サイズが8番目に分割されていることを示します。

結論として、options.inSampleSizeを必ず使用してください。はいの場合は、変更してください。

0

コードの下にしよう: -

take_image_activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:orientation="vertical" > 

     <Button 
     android:id="@+id/buttonTakeImageActivity" 
     android:layout_width="170dp" 
     android:layout_height="60dp" 
     android:textSize="25sp" 
     android:text="Gallery"/> 

     <Button 
     android:id="@+id/buttonTakeImageCam" 
     android:layout_width="170dp" 
     android:layout_height="60dp" 
     android:layout_marginTop="10dp" 
     android:textSize="25sp" 
     android:text="Camera"/> 

    </LinearLayout> 

</RelativeLayout> 

TakeImageActivity.java

public class TakeImageActivity extends Activity implements OnClickListener { 

    File file,mFileTemp; 
    Button buttonTakeImage, buttonTakeImageCam; 
    ImageView imageView; 
    Uri mPhotoUri; 
    Bitmap thumbnail; 
    private Uri fileUri; 
    private static final String IMAGE_DIRECTORY_NAME = "PhotoEditor Camera"; 
    public static final int MEDIA_TYPE_IMAGE = 1; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.take_image_activity); 

     file = new File(Environment.getExternalStorageDirectory() 
       .getAbsolutePath() + "/FrameImages"); 

     initialization(); 
    } 

    private void initialization() { 
     // TODO Auto-generated method stub 
     buttonTakeImage = (Button) findViewById(R.id.buttonTakeImageActivity); 

     buttonTakeImageCam = (Button) findViewById(R.id.buttonTakeImageCam); 

     buttonTakeImage.setOnClickListener(this); 
     buttonTakeImageCam.setOnClickListener(this); 

     if (!isDeviceSupportCamera()) { 
      Toast.makeText(getApplicationContext(), 
        "Sorry! Your device doesn't support camera", 
        Toast.LENGTH_LONG).show(); 
      // will close the app if the device does't have camera 
      finish(); 
     } 
    } 

    private boolean isDeviceSupportCamera() { 
     if (getApplicationContext().getPackageManager().hasSystemFeature(
       PackageManager.FEATURE_CAMERA)) { 
      // this device has a camera 
      return true; 
     } else { 
      // no camera on this device 
      return false; 
     } 
    } 

    public Uri getOutputMediaFileUri(int type) { 
     return Uri.fromFile(getOutputMediaFile(type)); 
    } 

    private static File getOutputMediaFile(int type) { 

     // External sdcard location 
     File mediaStorageDir = new File(
       Environment 
         .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
       IMAGE_DIRECTORY_NAME); 

     // Create the storage directory if it does not exist 
     if (!mediaStorageDir.exists()) { 
      if (!mediaStorageDir.mkdirs()) { 
       Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " 
         + IMAGE_DIRECTORY_NAME + " directory"); 
       return null; 
      } 
     } 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
       Locale.getDefault()).format(new Date()); 
     File mediaFile; 
     if (type == MEDIA_TYPE_IMAGE) { 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator 
        + "IMG_" + timeStamp + ".jpg"); 
     } else { 
      return null; 
     } 

     return mediaFile; 
    } 


    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     switch (v.getId()) { 
     case R.id.buttonTakeImageActivity: 

      if (Environment.getExternalStorageState().equals("mounted")) { 
       Intent intent = new Intent(); 
       intent.setType("image/*"); 
       intent.setAction(Intent.ACTION_PICK); 
       startActivityForResult(
         Intent.createChooser(intent, "Select Picture:"), 
         1); 
      } 
      break; 

     case R.id.buttonTakeImageCam: 
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
      // start the image capture Intent 
      startActivityForResult(intent, 2); 
      break; 

     default: 
      break; 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     if (resultCode != RESULT_OK) 
      return; 

     switch (requestCode) { 
     case 1: 
      Uri selectedImageUri = data.getData(); 
      String selectedImagePath = getPath(selectedImageUri); 
      Bitmap photo = getPreview(selectedImagePath); 
      yourimageview.setImageBitmap(photo); 
      break; 

     case 2: 
       if (resultCode == RESULT_OK) { 
        // successfully captured the image 
        // display it in image view 
        previewCapturedImage(); 
       } else if (resultCode == RESULT_CANCELED) { 
        // user cancelled Image capture 
        Toast.makeText(getApplicationContext(), 
          "User cancelled image capture", Toast.LENGTH_SHORT) 
          .show(); 
       } else { 
        // failed to capture image 
        Toast.makeText(getApplicationContext(), 
          "Sorry! Failed to capture image", Toast.LENGTH_SHORT) 
          .show(); 
       } 
      break; 
     } 
    } 
     private void previewCapturedImage() { 
      try { 
       // bimatp factory 
       BitmapFactory.Options options = new BitmapFactory.Options(); 

       // downsizing image as it throws OutOfMemory Exception for larger 
       // images 
       options.inSampleSize = 2; 

       final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), 
         options); 

       yourimageview.setImageBitmap(bitmap); 
      } catch (NullPointerException e) { 
       e.printStackTrace(); 
      } 
     } 

    public Bitmap getPreview(String fileName) { 
     File image = new File(fileName); 

     BitmapFactory.Options bounds = new BitmapFactory.Options(); 
     bounds.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(image.getPath(), bounds); 
     if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) { 
      return null; 
     } 
     int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight 
       : bounds.outWidth; 
     BitmapFactory.Options opts = new BitmapFactory.Options(); 
     opts.inSampleSize = originalSize/64; 
     // opts.inSampleSize = originalSize; 
     return BitmapFactory.decodeFile(image.getPath()); 
    } 

    public String getPath(Uri uri) { 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 
} 
+0

ok、私はそれを試してみます –

+0

@DidikSazali何か問題があれば実装時間とping。 –

関連する問題