2015-09-23 21 views
0

我在android開發新的,我想打一個應用程序這樣的應用程序https://play.google.com/store/apps/details?id=com.MenFashionPhotoSuit.FashionPhotoMontage&hl=en當相機打開時,我如何在相機上追加圖像網格視圖。

,我已經我的自定義相機形式的相機現在能正常使用,我想展現圖像網格視圖上的相機時,相機的啓動創建。我在相機上添加了圖像視圖,當相機打開時,圖像也顯示在屏幕上,但是當我在那時拍攝圖像時,只有拍攝的圖像在屏幕上顯示,我還需要顯示該圖像是放在相機上的。請幫我解決這個問題....

public class MainActivity extends Activity implements Callback, 
    OnClickListener { 

private SurfaceView surfaceView; 
private SurfaceHolder surfaceHolder; 
private Camera camera; 
private Button flipCamera; 
private Button flashCameraButton; 
private Button captureImage; 
private int cameraId; 
private boolean flashmode = false; 
private int rotation; 
ImageView img; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // camera surface view created 
    cameraId = CameraInfo.CAMERA_FACING_BACK; 
    flipCamera = (Button) findViewById(R.id.flipCamera); 
    flashCameraButton = (Button) findViewById(R.id.flash); 
    captureImage = (Button) findViewById(R.id.captureImage); 
    surfaceView = (SurfaceView) findViewById(R.id.surfaceView); 
    surfaceHolder = surfaceView.getHolder(); 
    surfaceHolder.addCallback(this); 
    flipCamera.setOnClickListener(this); 
    captureImage.setOnClickListener(this); 
    img=(ImageView)findViewById(R.id.image1); 
    flashCameraButton.setOnClickListener(this); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

    if (Camera.getNumberOfCameras() > 1) { 
     flipCamera.setVisibility(View.VISIBLE); 
    } 
    if (!getBaseContext().getPackageManager().hasSystemFeature( 
      PackageManager.FEATURE_CAMERA_FLASH)) { 
     flashCameraButton.setVisibility(View.GONE); 
    } 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    if (!openCamera(CameraInfo.CAMERA_FACING_BACK)) { 
     alertCameraDialog(); 
    } 

} 

private boolean openCamera(int id) { 
    boolean result = false; 
    cameraId = id; 
    releaseCamera(); 
    img.setImageResource(R.drawable.close_unpresed1); 
    try { 
     camera = Camera.open(cameraId); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    if (camera != null) { 
     try { 
      setUpCamera(camera); 
      camera.setErrorCallback(new ErrorCallback() { 

       @Override 
       public void onError(int error, Camera camera) { 

       } 
      }); 
      camera.setPreviewDisplay(surfaceHolder); 
      camera.startPreview(); 
      result = true; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      result = false; 
      releaseCamera(); 
     } 
    } 
    return result; 
} 

private void setUpCamera(Camera c) { 
    Camera.CameraInfo info = new Camera.CameraInfo(); 
    Camera.getCameraInfo(cameraId, info); 
    rotation = getWindowManager().getDefaultDisplay().getRotation(); 
    int degree = 0; 
    switch (rotation) { 
    case Surface.ROTATION_0: 
     degree = 0; 
     break; 
    case Surface.ROTATION_90: 
     degree = 90; 
     break; 
    case Surface.ROTATION_180: 
     degree = 180; 
     break; 
    case Surface.ROTATION_270: 
     degree = 270; 
     break; 

    default: 
     break; 
    } 

    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
     // frontFacing 
     rotation = (info.orientation + degree) % 330; 
     rotation = (360 - rotation) % 360; 
    } else { 
     // Back-facing 
     rotation = (info.orientation - degree + 360) % 360; 
    } 
    c.setDisplayOrientation(rotation); 
    Parameters params = c.getParameters(); 

    showFlashButton(params); 

    List<String> focusModes = params.getSupportedFlashModes(); 
    if (focusModes != null) { 
     if (focusModes 
       .contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { 
      params.setFlashMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); 
     } 
    } 

    params.setRotation(rotation); 
} 

private void showFlashButton(Parameters params) { 
    boolean showFlash = (getPackageManager().hasSystemFeature( 
      PackageManager.FEATURE_CAMERA_FLASH) && params.getFlashMode() != null) 
      && params.getSupportedFlashModes() != null 
      && params.getSupportedFocusModes().size() > 1; 

    flashCameraButton.setVisibility(showFlash ? View.VISIBLE 
      : View.INVISIBLE); 

} 

private void releaseCamera() { 
    try { 
     if (camera != null) { 
      camera.setPreviewCallback(null); 
      camera.setErrorCallback(null); 
      camera.stopPreview(); 
      camera.release(); 
      camera = null; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     Log.e("error", e.toString()); 
     camera = null; 
    } 
} 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 

} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 

} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.flash: 
     flashOnButton(); 
     break; 
    case R.id.flipCamera: 
     flipCamera(); 
     break; 
    case R.id.captureImage: 
     takeImage(); 
     break; 

    default: 
     break; 
    } 
} 

private void takeImage() { 
    camera.takePicture(null, null, new PictureCallback() { 

     private File imageFile; 

     @Override 
     public void onPictureTaken(byte[] data, Camera camera) { 
      try { 
       // convert byte array into bitmap 
       Bitmap loadedImage = null; 
       Bitmap rotatedBitmap = null; 
       loadedImage = BitmapFactory.decodeByteArray(data, 0, 
         data.length); 

       // rotate Image 
       Matrix rotateMatrix = new Matrix(); 
       rotateMatrix.postRotate(rotation); 
       rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0, 
         loadedImage.getWidth(), loadedImage.getHeight(), 
         rotateMatrix, false); 
       String state = Environment.getExternalStorageState(); 
       File folder = null; 
       if (state.contains(Environment.MEDIA_MOUNTED)) { 
        folder = new File(Environment 
          .getExternalStorageDirectory() + "/Demo"); 
       } else { 
        folder = new File(Environment 
          .getExternalStorageDirectory() + "/Demo"); 
       } 

       boolean success = true; 
       if (!folder.exists()) { 
        success = folder.mkdirs(); 
       } 
       if (success) { 
        java.util.Date date = new java.util.Date(); 
        imageFile = new File(folder.getAbsolutePath() 
          + File.separator 
          + new Timestamp(date.getTime()).toString() 
          + "Image.jpg"); 

        imageFile.createNewFile(); 
       } else { 
        Toast.makeText(getBaseContext(), "Image Not saved", 
          Toast.LENGTH_SHORT).show(); 
        return; 
       } 

       ByteArrayOutputStream ostream = new ByteArrayOutputStream(); 

       // save image into gallery 
       rotatedBitmap.compress(CompressFormat.JPEG, 100, ostream); 

       FileOutputStream fout = new FileOutputStream(imageFile); 
       fout.write(ostream.toByteArray()); 
       fout.close(); 
       ContentValues values = new ContentValues(); 

       values.put(Images.Media.DATE_TAKEN, 
         System.currentTimeMillis()); 
       values.put(Images.Media.MIME_TYPE, "image/jpeg"); 
       values.put(MediaStore.MediaColumns.DATA, 
         imageFile.getAbsolutePath()); 

       MainActivity.this.getContentResolver().insert( 
         Images.Media.EXTERNAL_CONTENT_URI, values); 

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

     } 
    }); 
} 

private void flipCamera() { 
    int id = (cameraId == CameraInfo.CAMERA_FACING_BACK ? CameraInfo.CAMERA_FACING_FRONT 
      : CameraInfo.CAMERA_FACING_BACK); 
    if (!openCamera(id)) { 
     alertCameraDialog(); 
    } 
} 

private void alertCameraDialog() { 
    AlertDialog.Builder dialog = createAlert(MainActivity.this, 
      "Camera info", "error to open camera"); 
    dialog.setNegativeButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 

     } 
    }); 

    dialog.show(); 
} 

private Builder createAlert(Context context, String title, String message) { 

    AlertDialog.Builder dialog = new AlertDialog.Builder( 
      new ContextThemeWrapper(context, 
        android.R.style.Theme_Holo_Light_Dialog)); 
    dialog.setIcon(R.drawable.ic_launcher); 
    if (title != null) 
     dialog.setTitle(title); 
    else 
     dialog.setTitle("Information"); 
    dialog.setMessage(message); 
    dialog.setCancelable(false); 
    return dialog; 

} 

private void flashOnButton() { 
    if (camera != null) { 
     try { 
      Parameters param = camera.getParameters(); 
      param.setFlashMode(!flashmode ? Parameters.FLASH_MODE_TORCH 
        : Parameters.FLASH_MODE_OFF); 
      camera.setParameters(param); 
      flashmode = !flashmode; 
     } catch (Exception e) { 
      // TODO: handle exception 
     } 

    } 
} 

}

xml文件

<SurfaceView 
    android:id="@+id/surfaceView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

<ImageView 
    android:id="@+id/image1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:text="Camera Demo application\nDeveloped by Ravi Sharma" 
    android:textColor="@android:color/black" /> 

<Button 
    android:id="@+id/captureImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="@dimen/activity_vertical_margin" 
    android:background="@drawable/back_unpresed2" /> 

<Button 
    android:id="@+id/flash" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="@dimen/activity_vertical_margin" 
    android:layout_marginRight="25dp" 
    android:layout_toLeftOf="@id/captureImage" 
    android:background="@drawable/check_unpresed1" /> 

<Button 
    android:id="@+id/flipCamera" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="@dimen/activity_vertical_margin" 
    android:layout_marginLeft="25dp" 
    android:layout_toRightOf="@id/captureImage" 
    android:background="@drawable/close_unpresed1" /> 

清單文件

  <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

回答

0

HI使用CircularImageView圖書館和根據您的需要進行定製。

+0

我試過但它不工作:( –