2013-07-19 1 views
1

나는 며칠 동안 안드로이드에서 사진을 찍으려고합니다. 아이디어는 이미지를 캡처 한 FTP에 파일을 업로드하는 것입니다. SurfaceView를 확장 한 클래스가 있습니다.이 뷰가 보이면, 장치의 후면 카메라 이미지를 재생합니다. 카메라의 이미지를 클릭하면 이미지를 JPEG 형식으로 유지하려고합니다.표면보기에서 JPG 파일을 얻으십시오 android

나는 수십개의 해결책을 시도했지만, 지금 당장 얻을 수있는 것은 JPEG입니다. 나는 옳은 방법으로 메신저를 생각하지만, 뭔가 빠져 있습니다. ,

try { 
     //create bitmap screen capture 
     Bitmap bitmap; 
     this.setDrawingCacheEnabled(true); 
     //Freezes the image 
     mCamera.stopPreview(); 
     bitmap = Bitmap.createBitmap(getDrawingCache(), 0, 0, this.getLayoutParams().width, this.getLayoutParams().height); 
     this.setDrawingCacheEnabled(false); 
     //Create temp file   
     file = File.createTempFile("prueba", ".JPG", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)); 
     //Copy bitmap content into file 
     FileOutputStream fos = new FileOutputStream(file); 
     bitmap.compress(CompressFormat.JPEG, 60, fos); 
     fos.flush(); 
     fos.close();    
     //Free camera 
     mCamera.release(); 
     mCamera = null; 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 

mCamera 잘 맞는 this.layoutParams로 선택 폭과 높이를 복사 특정 크기로, android.hardware.Camera입니다 : 여기

나는 사용자 정의 클래스에 onClick 이벤트에서 사용하는 코드입니다 이 모든 것을 surfaceCreated() 메소드에서.

누군가가 어떤 표시로 나를 도울 수 있기를 바랍니다.

대단히 감사합니다. 비트 맵은 유효한 데이터가 아닌지

답변

0

는 나도 몰라하지만 난 당신이 요구하는 것은 중 하나에 startActivityForResult 카메라를 실행하고 이미지 돌아 가야하는 것입니다 당신에게 일반적인 방법을 알 수 있습니다 :

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

는 나 카메라 미리보기에 대한 콜백을 연결하고 당신이 원하는 모든 사용자가 내가 전술 한 바와 같이 내가 의도를 시작 생각 다음 사진을 찍을 수 있도록하는 것입니다 경우가 http://developer.android.com/reference/android/hardware/Camera.html#setPreviewCallback(android.hardware.Camera.PreviewCallback)

을 이미지 프레임을 처리 할 수있는 것은 솔루션입니다 너를 위해서.

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

    // we want the camera image to be put in a specific place 
    File path; 
    try { 
     path = new File(getCameraStoragePath()); 
    } catch (Exception e1) { 
     e1.printStackTrace(); 
     return; 
    }      

    SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS"); 
    mFilename = mPeer + "." + timeStampFormat.format(new java.util.Date()) + ".jpg"; 

    try 
    { 
     mPath = path.getCanonicalPath(); 
    } catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 

    File out = new File(path, mFilename); 
    android.net.Uri uri = android.net.Uri.fromFile(out); 

    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri); 
    activity.startActivityForResult(intent, CAMERA); 

그런 이미지를 다루는 자신의 onActivityResult를 구현이 사용자가 걸렸다 후 :

protected void onActivityResult(int requestCode, int resultCode, Intent data); 
1

감사 여기에 내가 작성한 응용 프로그램 중 하나가 작동하는 코드이다 도움을 청합니다.

마지막으로 해결책을 찾았습니다. 카메라가 응용 프로그램에 통합되어 이미지의 회전과 미리보기의 해상도를 제어하기가 더 쉽기 때문에 첫 번째 옵션이 마음에 들었습니다.

지금까지 인 텐트에서 가져온 이미지의 크기가 조정되어 개체 ImagePreview (ImageView)에로드되었습니다.

CALL 카메라의 의도 :

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

if (getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size()>0) { 
    pathImagen = android.net.Uri.fromFile(File.createTempFile("prueba"+System.currentTimeMillis(), ".JPG", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES))); 
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, pathImagen); 
    startActivityForResult(intent, 94010); 
} 

이미지 파일을 GET 및 크기 조정 :

public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    switch (requestCode) { 

    case 94010: 

     if (resultCode == Activity.RESULT_OK) { 

      Bitmap bmp = null; 

      try { 

       //Obtenemos las dimensiones de la imagen (no se carga la imagen completa) 
       ContentResolver cr = getContentResolver(); 
       cr.notifyChange(pathImagen, null); 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inPreferredConfig = Config.RGB_565; 
       options.inJustDecodeBounds = true; 
       BitmapFactory.decodeFile(pathImagen.getEncodedPath(), options); 
       //bmp = android.provider.MediaStore.Images.Media.getBitmap(cr, pathImagen); 
       System.out.println("Tamaño imagen: "+options.outWidth+"x"+options.outHeight); 

       //Bitmap bmp = (Bitmap) data.getExtras().get("data"); 

       int anchoFinal = 480;  
       int altoFinal = (options.outHeight*480)/options.outWidth; 

       options.inJustDecodeBounds = false; 
       bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(pathImagen.getEncodedPath(), options), anchoFinal, altoFinal, false); 
       System.out.println("Tamaño imagen despues escalar: "+bmp.getWidth()+"x"+bmp.getHeight()); 

       this.imagePreView.setVisibility(View.VISIBLE); 
       this.imagePreView.getLayoutParams().width = bmp.getWidth(); 
       this.imagePreView.getLayoutParams().height = bmp.getHeight(); 
       this.imagePreView.setImageBitmap(bmp); 
       this.popupEditObject.setVisibility(View.VISIBLE); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

유일한 문제는 이미지가 항상 3264x1968의 해상도와 함께 제공한다는 것입니다 여기 내 솔루션입니다. 인 텐트가 어떤 정보를 반환하는지는 수평 적으로나 수직적으로 누가 가져 왔는지에 관계없이 보게 될 것입니다.

저는 이것이 더 많은 사람들을 돕고 실제로 답변이없는 많은 포럼을 보았기를 바랍니다.

코드를 수정하는 데 의견을 보내 주시면 감사하겠습니다.

다시 한번 감사드립니다!

관련 문제