Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

PEMROGRAMAN LANJUT

“CAPTURE”

Chusnun Nidhom
D3 PJJ Teknik Informatika 2018
2103187019

POLITEKNIK ELEKTRONIKA NEGERI SURABAYA


Pertama buka res ⇒ values ⇒ strings.xml dan tambahkan nilai string di bawah ini.
Ini adalah beberapa string yang akan kita gunakan dalam proyek.
<resources>
<string name="app_name">Kalkulator</string>
<string name="share_title">Share Screenshot</string>
<string name="screenshot_take_failed">Failed to take screenshot!!</string>
<string name="wait">Please wait…</string>
<string name="full_page_screenshot">Full Page Screenshot</string>
<string name="hidden_text">Hidden Text</string>
<string name="custom_page_screenshot">Custom Page Screenshot</string>
<string name="sharing_text">Your sharing text goes here..</string>
</resources>

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">

<!-- Button which will take full page screenshot -->


<Button
android:id="@+id/full_page_screenshot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/full_page_screenshot"
android:textColor="@android:color/black"
android:textSize="14sp" />

<!-- Hidden Text which will shown when taking screenshot from below
Button -->
<TextView
android:id="@+id/hidden_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/hidden_text"
android:textColor="@android:color/black"
android:textSize="14sp"
android:visibility="invisible" />

<!-- Button which will take screenshot after hiding some view and showing
some view -->
<Button
android:id="@+id/custom_page_screenshot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/custom_page_screenshot"
android:textColor="@android:color/black"
android:textSize="14sp" />

<!-- ImageView to show taken Screenshot -->


<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="@drawable/logo_pens" />

</LinearLayout>

Buat java class dengan nama enum ScreenshotType. kelas ini akan membantu
untuk membedakan antara jenis tangkapan layar yang perlu saya ambil.
package com.example.kalkulator;

public enum ScreenshotType {


FULL, CUSTOM;
}

MainActivity.java
package com.example.kalkulator;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {

private Button fullPageScreenshot, customPageScreenshot;


private LinearLayout rootContent;
private ImageView imageView;
private TextView hiddenText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
implementClickEvents();
}

/* Find all views Ids */


private void findViews() {
fullPageScreenshot = (Button) findViewById(R.id.full_page_screenshot);
customPageScreenshot = (Button)
findViewById(R.id.custom_page_screenshot);

rootContent = (LinearLayout) findViewById(R.id.root_content);

imageView = (ImageView) findViewById(R.id.image_view);

hiddenText = (TextView) findViewById(R.id.hidden_text);


}

/* Implement Click events over Buttons */


private void implementClickEvents() {
fullPageScreenshot.setOnClickListener(this);
customPageScreenshot.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.full_page_screenshot:
takeScreenshot(ScreenshotType.FULL);
break;
case R.id.custom_page_screenshot:
takeScreenshot(ScreenshotType.CUSTOM);
break;
}
}

/* Method which will take screenshot on Basis of Screenshot Type ENUM */


private void takeScreenshot(ScreenshotType screenshotType) {
Bitmap b = null;
switch (screenshotType) {
case FULL:
//If Screenshot type is FULL take full page screenshot i.e our
root content.
b = ScreenshotUtils.getScreenShot(rootContent);
break;
case CUSTOM:
//If Screenshot type is CUSTOM

fullPageScreenshot.setVisibility(View.INVISIBLE);//set the
visibility to INVISIBLE of first button
hiddenText.setVisibility(View.VISIBLE);//set the visibility to
VISIBLE of hidden text

b = ScreenshotUtils.getScreenShot(rootContent);

//After taking screenshot reset the button and view again


fullPageScreenshot.setVisibility(View.VISIBLE);//set the
visibility to VISIBLE of first button again
hiddenText.setVisibility(View.INVISIBLE);//set the visibility
to INVISIBLE of hidden text

//NOTE: You need to use visibility INVISIBLE instead of GONE


to remove the view from frame else it wont consider the view in frame and you
will not get screenshot as you required.
break;
}

//If bitmap is not null


if (b != null) {
showScreenShotImage(b);//show bitmap over imageview

File saveFile = ScreenshotUtils.getMainDirectoryName(this);//get


the path to save screenshot
File file = ScreenshotUtils.store(b, "screenshot" + screenshotType
+ ".jpg", saveFile);//save the screenshot to selected path
shareScreenshot(file);//finally share screenshot
} else
//If bitmap is null show toast message
Toast.makeText(this, R.string.screenshot_take_failed,
Toast.LENGTH_SHORT).show();

/* Show screenshot Bitmap */


private void showScreenShotImage(Bitmap b) {
imageView.setImageBitmap(b);
}

/* Share Screenshot */
private void shareScreenshot(File file) {
Uri uri = Uri.fromFile(file);//Convert file path into Uri for sharing
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT,
getString(R.string.sharing_text));
intent.putExtra(Intent.EXTRA_STREAM, uri);//pass uri here
startActivity(Intent.createChooser(intent,
getString(R.string.share_title)));
}

}
Selanjutnya buat class dengan nama ScreenshotUtils.java, kelas ini berfungsi untuk
menambahkan beberapa metode dtatis yang membantu untuk mengambil
tangkapan layar,membuat direktori dan menyimpan tangkapan layar yang diambil.
package com.example.kalkulator;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import java.io.File;
import java.io.FileOutputStream;

public class ScreenshotUtils {


public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
public static File getMainDirectoryName(Context context) {
//Here we will use getExternalFilesDir and inside that we will make
our Demo folder
//benefit of getExternalFilesDir is that whenever the app uninstalls
the images will get deleted automatically.
File mainDir = new File(
context.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
"Demo");
//If File is not present create directory
if (!mainDir.exists()) {
if (mainDir.mkdir())
Log.e("Create Directory", "Main Directory Created : " +
mainDir);
}
return mainDir;
}
public static File store(Bitmap bm, String fileName, File saveFilePath) {
File dir = new File(saveFilePath.getAbsolutePath());
if (!dir.exists())
dir.mkdirs();
File file = new File(saveFilePath.getAbsolutePath(), fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
}
Hasil Output :

You might also like