Skip to content

Commit

Permalink
CameraPreview
Browse files Browse the repository at this point in the history
  • Loading branch information
gnemade51 committed Jul 24, 2020
1 parent b02e62a commit 2848827
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 52 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.instagramclone_android">

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
package com.example.instagramclone_android.Fragments;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;

import com.example.instagramclone_android.R;
import com.example.instagramclone_android.Utils.ShowCamera;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* A simple {@link Fragment} subclass.
* Use the {@link PhotoFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class PhotoFragment extends Fragment {
Camera camera;
private Camera mCamera;
FrameLayout frameLayout;
ShowCamera showCamera;

Expand All @@ -44,11 +56,61 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_photo, container, false);
frameLayout = (FrameLayout) view.findViewById(R.id.camera_container);
frameLayout = (FrameLayout) view.findViewById(R.id.photo_camera_container);
//open the camera
camera = Camera.open();
showCamera = new ShowCamera(getActivity(),camera);
mCamera = getCameraInstance();
showCamera = new ShowCamera(getActivity(),mCamera);
frameLayout.addView(showCamera);
Button captureButton = (Button) view.findViewById(R.id.photo_button);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, mPicture);
}
});
return view;
}
private Camera getCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();
} catch (Exception e) {
// cannot get camera or does not exist
}
return camera;
}
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {

} catch (IOException e) {
}
}
};
private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "InstagramClone");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("InstagramClone", "failed to create directory");
return null;
}
}
// Create a media file name
@SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");

return mediaFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
import java.util.List;

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
Camera camera;
SurfaceHolder holder;
private Camera mCamera;
private SurfaceHolder mSurfaceHolder;

public ShowCamera(Context context, Camera camera) {
super(context);
this.camera = camera;
holder = getHolder();
holder.addCallback(this);

this.mCamera = camera;
this.mSurfaceHolder = this.getHolder();
this.mSurfaceHolder.addCallback(this);
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
Camera.Parameters params = camera.getParameters();
Camera.Parameters params = mCamera.getParameters();

List<Camera.Size> sizes = params.getSupportedPictureSizes();
Camera.Size mSize = null;
Expand All @@ -34,32 +35,32 @@ public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
//change orientation of camera
if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
params.set("orientation","portrait");
camera.setDisplayOrientation(90);
mCamera.setDisplayOrientation(90);
params.setRotation(90);
}else {
params.set("orientation","landscape");
camera.setDisplayOrientation(0);
mCamera.setDisplayOrientation(0);
params.setRotation(0);
}
params.setPictureSize(mSize.width,mSize.height);
camera.setParameters(params);
mCamera.setParameters(params);
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
}catch (IOException e){
e.printStackTrace();
}

}

@Override
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
camera.stopPreview();
camera.release();
mCamera.stopPreview();
mCamera.release();
}
}
62 changes: 46 additions & 16 deletions app/src/main/res/layout/fragment_photo.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.VideoFragment"
android:orientation="vertical"
android:background="#fff">
<FrameLayout
android:id="@+id/photo_camera_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
</FrameLayout>

<LinearLayout
android:id="@+id/toolbar_new_post_screen"
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top"
android:background="#e7e7e7">
android:background="#e7e7e7"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
Expand All @@ -31,8 +38,8 @@
android:layout_weight="8"
android:textColor="@color/black"/>

</LinearLayout>
<FrameLayout
</androidx.appcompat.widget.LinearLayoutCompat>
<!--<FrameLayout
android:id="@+id/camera_container"
android:layout_width="match_parent"
android:layout_height="400dp"
Expand All @@ -52,19 +59,42 @@
android:src="@drawable/flash_on">
</ImageView>
</FrameLayout>
<RelativeLayout
</FrameLayout>-->

<androidx.appcompat.widget.AppCompatImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="10dp"
android:src="@drawable/flip_camera"
android:layout_above="@+id/button_container"
android:layout_alignParentStart="true">

</androidx.appcompat.widget.AppCompatImageView>
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="10dp"
android:src="@drawable/flash_on"
android:layout_above="@+id/button_container"
android:layout_alignParentEnd="true">

</androidx.appcompat.widget.AppCompatImageView>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/button_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:background="#fff">
<Button
android:id="@+id/capture_button"
android:id="@+id/photo_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/photo_button"
android:layout_centerInParent="true"/>
android:background="@drawable/photo_button"/>

</androidx.appcompat.widget.LinearLayoutCompat>

</RelativeLayout>


</LinearLayout>
</RelativeLayout>
62 changes: 46 additions & 16 deletions app/src/main/res/layout/fragment_video.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.VideoFragment"
android:orientation="vertical"
android:background="#fff">
<FrameLayout
android:id="@+id/video_camera_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
</FrameLayout>

<LinearLayout
android:id="@+id/toolbar_new_post_screen"
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top"
android:background="#e7e7e7">
android:background="#e7e7e7"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
Expand All @@ -31,8 +38,8 @@
android:layout_weight="8"
android:textColor="@color/black"/>

</LinearLayout>
<FrameLayout
</androidx.appcompat.widget.LinearLayoutCompat>
<!--<FrameLayout
android:id="@+id/camera_container"
android:layout_width="match_parent"
android:layout_height="400dp"
Expand All @@ -52,19 +59,42 @@
android:src="@drawable/flash_on">
</ImageView>
</FrameLayout>
<RelativeLayout
</FrameLayout>-->

<androidx.appcompat.widget.AppCompatImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="10dp"
android:src="@drawable/flip_camera"
android:layout_above="@+id/button_container"
android:layout_alignParentStart="true">

</androidx.appcompat.widget.AppCompatImageView>
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="10dp"
android:src="@drawable/flash_on"
android:layout_above="@+id/button_container"
android:layout_alignParentEnd="true">

</androidx.appcompat.widget.AppCompatImageView>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/button_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:background="#fff">
<Button
android:id="@+id/capture_button"
android:id="@+id/video_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/photo_button"
android:layout_centerInParent="true"/>
android:background="@drawable/photo_button"/>

</androidx.appcompat.widget.LinearLayoutCompat>

</RelativeLayout>


</LinearLayout>
</RelativeLayout>

0 comments on commit 2848827

Please sign in to comment.